/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.feesapi.bmgl.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.feesapi.bmgl.entity.BmYmjzConfig;
import com.tofly.feesapi.bmgl.service.BmYmjzConfigService;
import com.tofly.feesapi.common.util.GuidGenerator;
import lombok.AllArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;
import java.util.Date;

/**
 * 月末结转CONFIG
 *
 * @author x
 * @date Wed Mar 18 00:00:00 CST 2026
 */
@RestController
@AllArgsConstructor
@RequestMapping("/bmymjzconfig")
@Api(tags="月末结转CONFIG接口")
public class BmYmjzConfigController {

    private final  BmYmjzConfigService bmYmjzConfigService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param bmYmjzConfig 月末结转CONFIG
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getBmYmjzConfigPage(Page page, @ModelAttribute BmYmjzConfig bmYmjzConfig) {
      return ResultRespone.success(bmYmjzConfigService.page(page, Wrappers.query(bmYmjzConfig)));
    }


    /**
     * 通过id查询月末结转CONFIG
     * @param code id
     * @return ResultRespone
     */
    @GetMapping("/{code}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone getById(@PathVariable("code" ) String code) {
      return ResultRespone.success(bmYmjzConfigService.getById(code));
    }


    @ApiOperation(value = "新增月末结转CONFIG")
    @PostMapping
    public ResultRespone save(@RequestBody BmYmjzConfig bmYmjzConfig) {

        if(bmYmjzConfig==null){
            return ResultRespone.failed("参数不能为空");
        }
        bmYmjzConfig.setCode(GuidGenerator.getGuid());
        QueryWrapper<BmYmjzConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MONTH", bmYmjzConfig.getMonth())
                    .eq("DAY", bmYmjzConfig.getDay());

        if(bmYmjzConfigService.list(queryWrapper).size() > 0) {
            return ResultRespone.failed("该数据已存在");
        }

      return ResultRespone.success(bmYmjzConfigService.save(bmYmjzConfig));
    }

    @ApiOperation(value = "修改月末结转CONFIG")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmYmjzConfig bmYmjzConfig) {
      return ResultRespone.success(bmYmjzConfigService.updateById(bmYmjzConfig));
    }

    @ApiOperation(value = "通过ID删除月末结转CONFIG")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{code}" )
    public ResultRespone removeById(@PathVariable String code) {
      return ResultRespone.success(bmYmjzConfigService.removeById(code));
    }

    /**
     * 通过id批量删除月末结转CONFIG
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过 ID 批量删除月末结转 CONFIG")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个 id 以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeByIds(String ids) {
        return ResultRespone.success(bmYmjzConfigService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @GetMapping("/all" )
    @ApiOperation(value = "查询所有月末结转配置")
    public ResultRespone getAll() {
        return ResultRespone.success(bmYmjzConfigService.list());
    }

    @GetMapping("/getCwrq" )
    @ApiOperation(value = "根据日期获取财务日期")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startdate", value = "开始日期", required = true, dataType = "Date")
    })
    public ResultRespone getCwrq(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date startdate) {
        return ResultRespone.success(bmYmjzConfigService.getCwrq(startdate));
    }

}
