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.QsbshBLO;
import com.zjrs.zwnw.zj.dto.QsDTO;
import com.zjrs.zwnw.zj.dto.QsPageDTO;
import com.zjrs.zwnw.zj.dto.QsbshDTO;
import com.zjrs.zwnw.zj.entity.InitMapper;
import com.zjrs.zwnw.zj.entity.QsbshMapper;
import com.zjrs.zwnw.zj.po.InitPO;
import com.zjrs.zwnw.zj.po.QsbshPO;
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 QsbshBLOImpl extends BLOImpl implements QsbshBLO {
    @Resource
    QsbshMapper qsbshMapper;
    @Resource
    InitMapper initMapper;
    @Override
    public void export(QsDTO dto, HttpServletResponse res) {
        //列表数据
        List<QsbshDTO> qsbsDTOList = qsbshMapper.getList(dto);
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "zj" + File.separator + "qsbshExport.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<QsbshPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ND", nd);
        qsbshMapper.delete(queryWrapper);
    }

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

    @Override
    public void saveOrUpd(QsbshPO qsbshPO) {
        QueryWrapper<QsbshPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ND", qsbshPO.getNd());
        int cnt=qsbshMapper.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<QsbshPO> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("nd",qsbshPO.getNd());
            qsbshPO.setTsXgr(userid);
            qsbshPO.setTsXgsj(nowDate);
            qsbshMapper.update(qsbshPO,updateWrapper);
        }else{
            qsbshPO.setTsCjr(userid);
            qsbshPO.setTsCjsj(nowDate);
            qsbshPO.setTsXgr(userid);
            qsbshPO.setTsXgsj(nowDate);
            qsbshMapper.insert(qsbshPO);
        }
    }

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

    @Override
    public QsbshDTO getTjData(String year) {
        //查询是否存在全市博士记录，若无，则查询初始化数据
        QueryWrapper<QsbshPO> queryWrapper = new QueryWrapper<>();
        QsbshDTO qsbsTjDTO=new QsbshDTO();
        QueryWrapper<InitPO> initQueryWrapper = new QueryWrapper<>();
        InitPO initPO=initMapper.selectOne(initQueryWrapper);
        if(StringUtils.isNotBlank(year)) {
            queryWrapper.eq("nd",year);
            qsbsTjDTO.setNd(year);
            if(year.equals((Long.parseLong(initPO.getNd())+1)+"")){
                //40周岁（含）以上博士
                Long nl40ulj=Long.parseLong(initPO.getNl40ubsh());
                //40周岁以下博士
                Long nl40dlj=Long.parseLong(initPO.getNl40dbsh());
                //中央、省驻湛单位博士
                Long zyszzdwlj=Long.parseLong(initPO.getZybsh());
                //市属单位博士
                Long ssdwlj=Long.parseLong(initPO.getSsbsh());
                qsbsTjDTO.setLjzl((nl40ulj+nl40dlj)+"");
                qsbsTjDTO.setNl40ulj(nl40ulj+"");
                qsbsTjDTO.setNl40dlj(nl40dlj+"");
                qsbsTjDTO.setZyszzdwlj(zyszzdwlj+"");
                qsbsTjDTO.setSsdwlj(ssdwlj+"");
            }else{
                QsDTO qsDTO=new QsDTO();
                qsDTO.setNd(year);
                qsbsTjDTO=qsbshMapper.getTjData(qsDTO);
            }
        }else{
            int cnt=qsbshMapper.selectCount(queryWrapper);
            if(cnt>0){
                String maxYear=qsbshMapper.getMaxYear();
                int nextYear=Integer.parseInt(maxYear)+1;
                qsbsTjDTO.setNd(nextYear+"");
                QsDTO qsDTO=new QsDTO();
                qsDTO.setNd(year);
                qsbsTjDTO=qsbshMapper.getTjData(qsDTO);
            }else{
                qsbsTjDTO.setNd((Long.parseLong(initPO.getNd())+1)+"");
                //40周岁（含）以上博士
                Long nl40ulj=Long.parseLong(initPO.getNl40ubsh());
                //40周岁以下博士
                Long nl40dlj=Long.parseLong(initPO.getNl40dbsh());
                //中央、省驻湛单位博士
                Long zyszzdwlj=Long.parseLong(initPO.getZybsh());
                //市属单位博士
                Long ssdwlj=Long.parseLong(initPO.getSsbsh());
                qsbsTjDTO.setLjzl((nl40ulj+nl40dlj)+"");
                qsbsTjDTO.setNl40ulj(nl40ulj+"");
                qsbsTjDTO.setNl40dlj(nl40dlj+"");
                qsbsTjDTO.setZyszzdwlj(zyszzdwlj+"");
                qsbsTjDTO.setSsdwlj(ssdwlj+"");
            }
        }
        return qsbsTjDTO;
    }
    @Override
    public String getMaxYear(){
        return qsbshMapper.getMaxYear();
    }
}
