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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.core.instrument.util.StringUtils;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import com.zjrs.zwnw.sxgl.blo.impl.JcsxqdkBLOimpl;
import com.zjrs.zwnw.zj.blo.QstcBLO;
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.entity.InitMapper;
import com.zjrs.zwnw.zj.entity.QstcMapper;
import com.zjrs.zwnw.zj.po.InitPO;
import com.zjrs.zwnw.zj.po.QstcPO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class QstcBLOImpl extends BLOImpl implements QstcBLO {
    @Resource
    QstcMapper qstcMapper;
    @Resource
    InitMapper initMapper;
    @Override
    public void export(QsDTO dto, HttpServletResponse res) {
        //列表数据
        List<QstcDTO> qstcDTOList = qstcMapper.getList(dto);
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "zj" + File.separator + "qstcExport.xlsx");

            String fileName = "全市突出贡献专家、享受国务院津贴数据导出结果.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void del(String nd) {
        QueryWrapper<QstcPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ND", nd);
        qstcMapper.delete(queryWrapper);
    }

    @Override
    public QstcPO getInfo(String nd) {
        QueryWrapper<QstcPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ND", nd);
        return qstcMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveOrUpd(QstcPO qstcPO) {
        QueryWrapper<QstcPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ND", qstcPO.getNd());
        int cnt=qstcMapper.selectCount(queryWrapper);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = dateFormat.format(date);
        if(cnt>0){
            UpdateWrapper<QstcPO> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("nd",qstcPO.getNd());
            qstcPO.setTsXgr(userid);
            qstcPO.setTsXgsj(nowDate);
            qstcMapper.update(qstcPO,updateWrapper);
        }else{
            qstcPO.setTsCjr(userid);
            qstcPO.setTsCjsj(nowDate);
            qstcPO.setTsXgr(userid);
            qstcPO.setTsXgsj(nowDate);
            qstcMapper.insert(qstcPO);
        }
    }

    @Override
    public PageResult getList(QsPageDTO dto, int page, int rows) {
        QsDTO qsDTO=new QsDTO();
        qsDTO.setNd(dto.getNd());
        Page<QstcDTO> page1 = new Page<>(page, rows);
        Page<QstcDTO> pageResult =qstcMapper.getListPage(page1,qsDTO);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public QstcDTO getTjData(String year) {
        //查询是否存在全市博士记录，若无，则查询初始化数据
        QueryWrapper<QstcPO> queryWrapper = new QueryWrapper<>();
        QstcDTO qstcTjDTO=new QstcDTO();
        QueryWrapper<InitPO> initQueryWrapper = new QueryWrapper<>();
        InitPO initPO=initMapper.selectOne(initQueryWrapper);
        if(StringUtils.isNotBlank(year)) {
            queryWrapper.eq("nd",year);
            qstcTjDTO.setNd(year);
            if(year.equals((Long.parseLong(initPO.getNd())+1)+"")){
                //国家级突出贡献专家
                Long gjzjlj=Long.parseLong(initPO.getGjzj());
                //省级突出贡献专家
                Long shengjzjlj=Long.parseLong(initPO.getShengjzj());
                //市级突出贡献专家
                Long shijzjlj=Long.parseLong(initPO.getShijzj());
                //享受国务院特殊津贴
                Long xsgwytsjtlj=Long.parseLong(initPO.getXsgwytsjt());
                qstcTjDTO.setLjzl((gjzjlj+shengjzjlj+shijzjlj+xsgwytsjtlj)+"");
                qstcTjDTO.setGjzjlj(gjzjlj+"");
                qstcTjDTO.setShengjzjlj(shengjzjlj+"");
                qstcTjDTO.setShijzjlj(shijzjlj+"");
                qstcTjDTO.setXsgwytsjtlj(xsgwytsjtlj+"");
            }else{
                QsDTO qsDTO=new QsDTO();
                qsDTO.setNd(year);
                qstcTjDTO=qstcMapper.getTjData(qsDTO);
            }
        }else{
            int cnt=qstcMapper.selectCount(queryWrapper);
            if(cnt>0){
                String maxYear=qstcMapper.getMaxYear();
                int nextYear=Integer.parseInt(maxYear)+1;
                qstcTjDTO.setNd(nextYear+"");
                QsDTO qsDTO=new QsDTO();
                qsDTO.setNd(year);
                qstcTjDTO=qstcMapper.getTjData(qsDTO);
            }else{
                qstcTjDTO.setNd((Long.parseLong(initPO.getNd())+1)+"");
                //国家级突出贡献专家
                Long gjzjlj=Long.parseLong(initPO.getGjzj());
                //省级突出贡献专家
                Long shengjzjlj=Long.parseLong(initPO.getShengjzj());
                //市级突出贡献专家
                Long shijzjlj=Long.parseLong(initPO.getShijzj());
                //享受国务院特殊津贴
                Long xsgwytsjtlj=Long.parseLong(initPO.getXsgwytsjt());
                qstcTjDTO.setLjzl((gjzjlj+shengjzjlj+shijzjlj+xsgwytsjtlj)+"");
                qstcTjDTO.setGjzjlj(gjzjlj+"");
                qstcTjDTO.setShengjzjlj(shengjzjlj+"");
                qstcTjDTO.setShijzjlj(shijzjlj+"");
                qstcTjDTO.setXsgwytsjtlj(xsgwytsjtlj+"");
            }
        }
        return qstcTjDTO;
    }
    @Override
    public String getMaxYear(){
        return qstcMapper.getMaxYear();
    }
}
