package com.xjrsoft.module.holiday.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.holiday.dto.AddHolidayDateDto;
import com.xjrsoft.module.holiday.dto.HolidayDateListDto;
import com.xjrsoft.module.holiday.dto.HolidayDatePageDto;
import com.xjrsoft.module.holiday.dto.InitYearDto;
import com.xjrsoft.module.holiday.dto.UpdateHolidayDateDto;
import com.xjrsoft.module.holiday.entity.HolidayDate;
import com.xjrsoft.module.holiday.service.IHolidayDateService;
import com.xjrsoft.module.holiday.vo.HolidayDateListVo;
import com.xjrsoft.module.holiday.vo.HolidayDatePageVo;
import com.xjrsoft.module.holiday.vo.HolidayDateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * @title:
 * @Author fanxp
 * @Date: 2024-03-26
 * @Version 1.0
 */
@RestController
@RequestMapping("/holiday" + "/holidayDate")
@Api(value = "/holiday" + "/holidayDate", tags = "节假日代码")
@AllArgsConstructor
public class HolidayDateController {


    private final IHolidayDateService holidayDateService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "列表(不分页)")
    @SaCheckPermission("holidaydate:detail")
    public RT<List<HolidayDateListVo>> list(@Valid HolidayDateListDto dto) {

        LambdaQueryWrapper<HolidayDate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(HolidayDate::getDeleteStatus,0)
                .eq(ObjectUtil.isNotNull(dto.getYear()), HolidayDate::getYear, dto.getYear())
                .eq(ObjectUtil.isNotNull(dto.getMonth()), HolidayDate::getYear, dto.getMonth())
                .eq(ObjectUtil.isNotNull(dto.getDay()), HolidayDate::getYear, dto.getDay())
                .orderByAsc(HolidayDate::getDate)
                .select(HolidayDate.class, x -> VoToColumnUtil.fieldsToColumns(HolidayDateListVo.class).contains(x.getProperty()));

        List<HolidayDate> list = holidayDateService.list(queryWrapper);
        List<HolidayDateListVo> listVos = BeanUtil.copyToList(list, HolidayDateListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "列表(分页)")
    @SaCheckPermission("holidaydate:detail")
    public RT<PageOutput<HolidayDatePageVo>> page(@Valid HolidayDatePageDto dto) {

        LambdaQueryWrapper<HolidayDate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(HolidayDate::getDeleteStatus,0)
                .orderByAsc(HolidayDate::getDate)
                .select(HolidayDate.class, x -> VoToColumnUtil.fieldsToColumns(HolidayDatePageVo.class).contains(x.getProperty()));
        IPage<HolidayDate> page = holidayDateService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<HolidayDatePageVo> pageOutput = ConventPage.getPageOutput(page, HolidayDatePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询信息")
    @SaCheckPermission("holidaydate:detail")
    public RT<HolidayDateVo> info(@RequestParam Long id) {
        HolidayDate holidayDate = holidayDateService.getById(id);
        if (holidayDate == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(holidayDate, HolidayDateVo.class));
    }

    @PostMapping(value = "init")
    @ApiOperation(value = "初始化年节假日数据")
    @SaCheckPermission("holidaydate:add")
    public RT<Boolean> initYear(@Valid @RequestBody InitYearDto dto) {
        boolean isSuccess = holidayDateService.initHoliday(dto.getYear());
        return RT.ok(isSuccess);
    }


    @PostMapping
    @ApiOperation(value = "新增")
    @SaCheckPermission("holidaydate:add")
    public RT<Boolean> add(@Valid @RequestBody AddHolidayDateDto dto) throws ParseException {
        boolean isSuccess = holidayDateService.add(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @SaCheckPermission("holidaydate:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateHolidayDateDto dto) {
        HolidayDate holidayDate = BeanUtil.toBean(dto, HolidayDate.class);
        holidayDate.setWay(2);
        return RT.ok(holidayDateService.updateById(holidayDate));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("holidaydate:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids) {
        return RT.ok(holidayDateService.delete(ids));
    }
}