package com.template.controller;


import com.template.api.SmartFreezeRecordControllerAPI;

import com.template.common.utils.TimeExchange;
import com.template.common.utils.paramUtils;
import com.template.model.enumModel.eFreezeRecord;
import com.template.model.pojo.SmartFreezeRecord;
import com.template.model.request.insertFreezeRecordRequest;
import com.template.model.request.manualFreezingRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.SmartFreezeRecordPageVo;
import com.template.services.SmartFreezeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@RestController
public class SmartFreezeRecordController implements SmartFreezeRecordControllerAPI {

    @Autowired
    private SmartFreezeRecordService smartFreezeRecordService;

    /**
     * 新增冻结记录
     * @param smartFreezeRequest 冻结记录数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartFreezeRecord(insertFreezeRecordRequest smartFreezeRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if(smartFreezeRequest.getType().intValue() ==  eFreezeRecord.SpecifyTime.getValue() && !StringUtils.hasText(smartFreezeRequest.getDeadline())){
            return CommonResult.fail("指定时间冻结方式下，截止时间不能为空");
        }else if(smartFreezeRequest.getType().intValue() ==  eFreezeRecord.PermanentFreeze.getValue()){
            smartFreezeRequest.setDeadline("9999-12-30 23:59:59");
        }

        SmartFreezeRecord sfr = new SmartFreezeRecord();
        sfr.setType(smartFreezeRequest.getType());
        sfr.setReason(smartFreezeRequest.getReason());
        sfr.setUserId(smartFreezeRequest.getUserId());
        sfr.setDeadline(TimeExchange.StringToDate(smartFreezeRequest.getDeadline(),"yyyy-MM-dd HH:mm:ss"));

        int result = smartFreezeRecordService.insertSmartFreezeRecord(sfr);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 更新冻结记录
     * @param sa 冻结记录数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartFreezeRecordById(SmartFreezeRecord sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = smartFreezeRecordService.updateSmartFreezeRecord(sa);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 冻结记录分页数据查询
     * @param currentPage 当前页数
     * @param pageCount 一页数据条数
     * @param name 查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartFreezeRecords(int currentPage, int pageCount, String name) {
        PageUtils<SmartFreezeRecord> result = smartFreezeRecordService.queryPageSmartFreezeRecords(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


    @Override
    public CommonResult deleteSmartFreezeRecordById(int id) {

        SmartFreezeRecord data = smartFreezeRecordService.getSmartById(id);

        if(data == null){
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = smartFreezeRecordService.deleteSmartFreezeRecordById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    @Override
    public CommonResult manualFreezing(manualFreezingRequest mfr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        //冻结记录不会有多条记录，解冻后只清理这一条
        //冻结的时候得看是否存在冻结期间的冻结数据
        int userCount = smartFreezeRecordService.getSmartByUserId(mfr.getUserId());
        if(userCount > 0){
            return CommonResult.fail("该用户已存在冻结记录，请勿重复操作");
        }

        if(mfr.getType().intValue() == 1 && mfr.getDeadline() == null){
            return CommonResult.fail("冻结截止时间不能为空");
        }

        SmartFreezeRecord sfr = new SmartFreezeRecord();
        sfr.setUserId(mfr.getUserId());
        sfr.setType(mfr.getType());
        sfr.setDeadline(TimeExchange.StringToDate(mfr.getDeadline() == null ? "9999-12-30 59:59:00" : mfr.getDeadline(),"yyyy-MM-dd HH:mm:ss"));
        sfr.setReason(mfr.getReason());

        int result = smartFreezeRecordService.insertSmartFreezeRecord(sfr);

        return result > 0 ? CommonResult.ok("冻结成功") : CommonResult.fail("冻结失败");
    }

    @Override
    public CommonResult queryPageFreezeRecord(int currentPage, int pageCount) {
        PageUtils<SmartFreezeRecordPageVo> result = smartFreezeRecordService.queryFreezeRecordPageList(currentPage, pageCount);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult thawFreezing(int id) {
        int deletedData = smartFreezeRecordService.deleteSmartFreezeRecordById(id);

        return deletedData > 0 ? CommonResult.ok("解冻成功") : CommonResult.fail("解冻失败");
    }
}

