package com.nantian.cloud.dbcapp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.SettlementDataConfig;
import com.nantian.cloud.dbcapp.service.ISettlementDataConfigService;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.nantian.sc.cloud.common.core.util.R;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * <p>
 * 企业数据结算周期配置 前端控制器
 * </p>
 *
 * @author qqm
 * @since 2024-01-25
 */

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/settlementDataConfig")
@Api(value = "settlementDataConfig",tags = "企业数据结算周期配置模块")
public class SettlementDataConfigController {

    @Autowired
    private ISettlementDataConfigService settlementDataConfigService;


    @GetMapping("/getSettlementDataConfigInfo")
    @SysLog("查询企业数据结算周期配置")
    @ApiOperation(value = "查询企业数据结算周期配置", notes = "查询企业数据结算周期配置")
    public R getSettlementDataConfigInfo(Page page, SettlementDataConfig settlementDataConfig){
        return R.ok(settlementDataConfigService.getSettlementDataConfigInfo(page,settlementDataConfig));
    }

    @PostMapping("/saveSettlementDataConfigInfo")
    @SysLog("保存企业数据结算周期配置")
    @ApiOperation(value = "保存企业数据结算周期配置", notes = "保存企业数据结算周期配置")
    public R saveSettlementDataConfigInfo(@RequestBody @Valid SettlementDataConfig settlementDataConfig){
        ScUser user = SecurityUtils.getUser();
        settlementDataConfig.setCreatedBy(user.getUsername());
        settlementDataConfig.setCreatedTime(new Date());
        boolean result = false;
        if (StringUtils.isBlank(settlementDataConfig.getUseEndDate())){
            settlementDataConfig.setUseEndDate("2099-12-31");
        }
        if (null != settlementDataConfig.getId() && settlementDataConfig.getId() > 0){
            result = settlementDataConfigService.saveOrUpdate(settlementDataConfig);
        }else{
            try {
                QueryWrapper<SettlementDataConfig> param = new QueryWrapper<>();
                param.eq("enterprise_code", settlementDataConfig.getEnterpriseCode());
                param.eq("formul_name", settlementDataConfig.getFormulName());
                param.eq("is_user", "1");
                param.like("month",settlementDataConfig.getMonth());
                SettlementDataConfig ordData = settlementDataConfigService.getOne(param);
                //更新历史数据，状态设置为禁用
                if (ordData != null){
                    ordData.setIsUser("0");
                    settlementDataConfigService.update(ordData,param);
                }
                result = settlementDataConfigService.save(settlementDataConfig);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (result) {
            return R.ok("添加成功！");
        }
        return R.failed("添加失败！");
    }

    @GetMapping("/deleteSettlementDataConfigInfo")
    @SysLog("删除企业数据结算周期配置")
    @ApiOperation(value = "删除企业数据结算周期配置", notes = "删除企业数据结算周期配置")
    public R deleteSettlementDataConfigInfo(@NotNull(message = "请选择需要删除的数据！") @RequestParam("id") Integer id){
        if (settlementDataConfigService.removeById(id)) {
            return R.ok("删除成功！");
        }
        return R.ok("删除失败！");
    }


    @GetMapping("/getSettlementDataConfigInfoByOne")
    @SysLog("查询企业数据结算周期配置")
    @ApiOperation(value = "查询企业数据结算周期配置", notes = "查询企业数据结算周期配置")
    public R getSettlementDataConfigInfoByOne(@NotNull(message = "企业信用代码不能为空！") @RequestParam("enterpriseCode") String enterpriseCode,
                                              @NotNull(message = "月份不能为空！！") @RequestParam("month") String month,
                                              @NotNull(message = "公式名称不能为空！") @RequestParam("formulName") String formulName){
        String[] months = month.split("-");
        if (months.length == 2){
            return R.ok(settlementDataConfigService.getSettlementDataConfigInfoByOne(enterpriseCode, month, months[1],formulName));
        }
        return R.ok(null);
    }




}
