package com.zjrs.zwnw.zj.bpo.impl;

import io.micrometer.core.instrument.util.StringUtils;
import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.zj.blo.InitBLO;
import com.zjrs.zwnw.zj.blo.QstcBLO;
import com.zjrs.zwnw.zj.bpo.QstcBPO;
import com.zjrs.zwnw.zj.dto.QsDTO;
import com.zjrs.zwnw.zj.dto.QsPageDTO;
import com.zjrs.zwnw.zj.dto.QstcDTO;
import com.zjrs.zwnw.zj.po.InitPO;
import com.zjrs.zwnw.zj.po.QstcPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;

@Service
public class QstcBPOImpl extends BPOImpl implements QstcBPO {
    @Resource
    private QstcBLO qstcBLO;
    @Resource
    private InitBLO initBLO;
    @Override
    public void export(QsDTO dto, HttpServletResponse res) {
        qstcBLO.export(dto,res);
    }

    @Override
    public AjaxResponse del(QsDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //判断是否从最大年度开始删除
        String maxYear=qstcBLO.getMaxYear();
        if(!dto.getNd().equals(maxYear)){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("只能删除填报年度为最新年度的填报记录");
            return ajaxResponse;
        }
        qstcBLO.del(dto.getNd());
        return ajaxResponse;
    }

    @Override
    public AjaxResponse getInfo(QsDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        QstcPO qstcPO = qstcBLO.getInfo(dto.getNd());
        QstcDTO qstcDTO=new QstcDTO();
        BeanUtils.copyProperties(qstcPO, qstcDTO);
        QstcDTO tjDTO=qstcBLO.getTjData(dto.getNd());
        Long gjzjlj=Long.parseLong(tjDTO.getGjzjlj());
        qstcDTO.setGjzjlj(gjzjlj+"");

        Long shengjzjlj=Long.parseLong(tjDTO.getShengjzjlj());
        qstcDTO.setShengjzjlj(shengjzjlj+"");

        Long shijzjlj=Long.parseLong(tjDTO.getShijzjlj());
        qstcDTO.setShijzjlj(shijzjlj+"");

        Long xsgwytsjtlj=Long.parseLong(tjDTO.getXsgwytsjtlj());
        qstcDTO.setXsgwytsjtlj(xsgwytsjtlj+"");

        Long ljzl=gjzjlj+shengjzjlj+shijzjlj+xsgwytsjtlj;
        qstcDTO.setLjzl(ljzl+"");
        qstcDTO.setNdxz((Long.parseLong(qstcDTO.getGjzjxz())+Long.parseLong(qstcDTO.getShengjzjxz())
                +Long.parseLong(qstcDTO.getShijzjxz())+Long.parseLong(qstcDTO.getXsgwytsjtxz()))+"");
        qstcDTO.setNdlc((Long.parseLong(qstcDTO.getGjzjlc())+Long.parseLong(qstcDTO.getShengjzjlc())
                +Long.parseLong(qstcDTO.getShijzjlc())+Long.parseLong(qstcDTO.getXsgwytsjtlc()))+"");

        ajaxResponse.setData("data",qstcDTO);
        return ajaxResponse;
    }

    @Override
    public AjaxResponse saveOrUpd(QstcDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        QstcPO qstcPO = new QstcPO();
        BeanUtils.copyProperties(dto, qstcPO);
        //判断年度是否比初始年度小
        InitPO initPO=initBLO.getInfo();
        if(Long.parseLong(dto.getNd())<=Long.parseLong(initPO.getNd())){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("年度有误，请刷新页面");
            return ajaxResponse;
        }
        String maxNd=qstcBLO.getMaxYear();
        if(StringUtils.isNotBlank(maxNd)) {
            if(Long.parseLong(dto.getNd())>Long.parseLong(maxNd)+1){
                ajaxResponse.setAppcode("1");
                ajaxResponse.setErrorMsg("年度有误，请刷新页面");
                return ajaxResponse;
            }
        }else if(Long.parseLong(dto.getNd())!=Long.parseLong(initPO.getNd())+1){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("年度有误，请刷新页面");
            return ajaxResponse;
        }
        QstcDTO qstcDTO=qstcBLO.getTjData(dto.getNd());
        //1.国家级突出贡献专家流出/减少：必须小于或等于“国家级突出贡献专家累计总量”。
        Long num1=Long.parseLong(dto.getGjzjlc());
        Long num2=Long.parseLong(qstcDTO.getGjzjlj());
        if(num1>num2){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("“国家级突出贡献专家流出/减少” 必须小于或等于 “上一年度的国家级突出贡献专家累计总量”");
            return ajaxResponse;
        }
        //2.省级突出贡献专家流出/减少：必须小于或等于“省级突出贡献专家累计总量”。
        num1=Long.parseLong(dto.getShengjzjlc());
        num2=Long.parseLong(qstcDTO.getShengjzjlj());
        if(num1>num2){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("“省级突出贡献专家流出/减少” 必须小于或等于 “上一年度的省级突出贡献专家累计总量”");
            return ajaxResponse;
        }
        //3.市级突出贡献专家流出/减少：必须小于或等于“市级突出贡献专家累计总量”。
        num1=Long.parseLong(dto.getShijzjlc());
        num2=Long.parseLong(qstcDTO.getShijzjlj());
        if(num1>num2){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("“市级突出贡献专家流出/减少” 必须小于或等于 “上一年度的市级突出贡献专家累计总量”");
            return ajaxResponse;
        }
        //4.享受国务院特殊津贴流出/减少：必须小于或等于“享受国务院特殊津贴累计总量”。
        num1=Long.parseLong(dto.getXsgwytsjtlc());
        num2=Long.parseLong(qstcDTO.getXsgwytsjtlj());
        if(num1>num2){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("“享受国务院特殊津贴流出/减少” 必须小于或等于 “上一年度的受国务院特殊津贴累计总量”");
            return ajaxResponse;
        }
        qstcBLO.saveOrUpd(qstcPO);
        return ajaxResponse;
    }

    @Override
    public PageResult getList(QsPageDTO dto, int page, int rows) {
        return qstcBLO.getList(dto,page,rows);
    }

    @Override
    public AjaxResponse getTjData() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //判断是否有初始化数据
        int cnt=initBLO.getCnt();
        if(cnt==0){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("初始数据未设置，请先设置！");
            return ajaxResponse;
        }
        QstcDTO tjData = qstcBLO.getTjData(null);
        ajaxResponse.setData("data",tjData);
        return ajaxResponse;
    }
}
