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

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

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

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

    @Override
    public QszjDTO getTjData(String year) {
        //查询是否存在全市专业技术人才记录，若无，则查询初始化数据
        QueryWrapper<QszjPO> queryWrapper = new QueryWrapper<>();
        QszjDTO qszjTjDTO=new QszjDTO();
        QueryWrapper<InitPO> initQueryWrapper = new QueryWrapper<>();
        InitPO initPO=initMapper.selectOne(initQueryWrapper);
        if(StringUtils.isNotBlank(year)) {
            queryWrapper.eq("nd",year);
            qszjTjDTO.setNd(year);
            if(year.equals((Long.parseLong(initPO.getNd())+1)+"")){
                //正高级职称
                Long zgjlj=Long.parseLong(initPO.getZgj());
                //高级职称
                Long gjlj=Long.parseLong(initPO.getGj());
                //中级职称
                Long zjlj=Long.parseLong(initPO.getZj());
                //初级职称
                Long cjlj=Long.parseLong(initPO.getCj());
                qszjTjDTO.setLjzl((gjlj+zgjlj+zjlj+cjlj)+"");
                qszjTjDTO.setGjlj(gjlj+"");
                qszjTjDTO.setZgjlj(zgjlj+"");
                qszjTjDTO.setZjlj(zjlj+"");
                qszjTjDTO.setCjlj(cjlj+"");
            }else{
                QsDTO qsDTO=new QsDTO();
                qsDTO.setNd(year);
                qszjTjDTO=qszjMapper.getTjData(qsDTO);
            }
        }else{
            int cnt=qszjMapper.selectCount(queryWrapper);
            if(cnt>0){
                String maxYear=qszjMapper.getMaxYear();
                int nextYear=Integer.parseInt(maxYear)+1;
                qszjTjDTO.setNd(nextYear+"");
                QsDTO qsDTO=new QsDTO();
                qsDTO.setNd(year);
                qszjTjDTO=qszjMapper.getTjData(qsDTO);
            }else{
                qszjTjDTO.setNd((Long.parseLong(initPO.getNd())+1)+"");
                //正高级职称
                Long zgjlj=Long.parseLong(initPO.getZgj());
                //高级职称
                Long gjlj=Long.parseLong(initPO.getGj());
                //中级职称
                Long zjlj=Long.parseLong(initPO.getZj());
                //初级职称
                Long cjlj=Long.parseLong(initPO.getCj());
                qszjTjDTO.setLjzl((gjlj+zgjlj+zjlj+cjlj)+"");
                qszjTjDTO.setGjlj(gjlj+"");
                qszjTjDTO.setZgjlj(zgjlj+"");
                qszjTjDTO.setZjlj(zjlj+"");
                qszjTjDTO.setCjlj(cjlj+"");
            }
        }
        return qszjTjDTO;
    }
    @Override
    public String getMaxYear(){
        return qszjMapper.getMaxYear();
    }
}
