package com.chinaweal.aiccs.aiccs.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.blacklist.entity.TPyBlacklist;
import com.chinaweal.aiccs.common.util.HolidaysUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.chinaweal.aiccs.aiccs.system.entity.HolidayInfo;
import com.chinaweal.aiccs.aiccs.system.service.IHolidayInfoService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.Map;

/**
 * <p>
 * 节假日信息 控制器
 * </p>
 *
 * @author chinaweal
 * @since 2024-07-23
 */
@Api(tags = "节假日信息")
@ApiSort(1)
@RestController
@RequestMapping("/system/holidayInfo")
public class HolidayInfoController {

    @Resource
    private IHolidayInfoService iHolidayInfoService;

    @ApiOperation(value = "1.新增节假日信息", position = 1)
    @PostMapping("/save")
    public RestResult<HolidayInfo> saveHolidayInfo(@RequestBody @Valid HolidayInfo holidayInfo, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        iHolidayInfoService.saveHolidayInfo(holidayInfo);
        HolidaysUtil.flushCache();
        return RestResult.ok();
    }

    @ApiOperation(value = "2.删除节假日信息", position = 2)
    @GetMapping("/delete")
    public RestResult<String> removeHolidayInfo(@RequestParam @ApiParam("id") Serializable id) {
        HolidayInfo holidayInfo = iHolidayInfoService.getById(id);
        if (holidayInfo != null) {
            holidayInfo.setIsDisabled(true);
            iHolidayInfoService.updateHolidayInfo(holidayInfo);
            HolidaysUtil.flushCache();
        }
        return RestResult.ok();
    }

    @ApiOperation(value = "3.修改节假日信息", position = 3)
    @PostMapping("/update")
    public RestResult<String> updateHolidayInfo(@RequestBody @Valid HolidayInfo holidayInfo, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        iHolidayInfoService.updateHolidayInfo(holidayInfo);
        HolidaysUtil.flushCache();
        return RestResult.ok();
    }

    @ApiOperation(value = "4.查询节假日信息", position = 4)
    @GetMapping("/get")
    public RestResult<HolidayInfo> getHolidayInfo(@RequestParam @ApiParam("id") Serializable id) {
        HolidayInfo holidayInfo = iHolidayInfoService.getById(id);
        return RestResult.ok(holidayInfo);
    }

    @ApiOperation(value = "5.分页查询节假日信息", position = 5)
    @PostMapping("/list")
    public RestResult<IPage<HolidayInfo>> listHolidayInfo(@RequestBody PageRequestDto pageRequestDto) {
        IPage<HolidayInfo> data = iHolidayInfoService.defaultPaging(pageRequestDto, wrapper -> {
            Map<String, Object> customParamMap = pageRequestDto.getCustomParamMap();
            wrapper.lambda().eq(HolidayInfo::getIsDisabled, false)
                    .like(customParamMap.containsKey("holidayName") && StringUtils.isNotBlank(customParamMap.get("holidayName").toString()),
                            HolidayInfo::getHolidayName, customParamMap.get("holidayName"));
        }, null);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "6.刷新节假日信息", position = 6)
    @GetMapping("/refresh")
    public RestResult<?> refreshHolidayInfo() {
        HolidaysUtil.flushCache();
        return RestResult.ok(HolidaysUtil.getHolidays());
    }
}
