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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.portal.blo.CommonBLO;
import com.zjrs.zwnw.rlzysc.blo.YgbaBLO;
import com.zjrs.zwnw.rlzysc.dto.*;
import com.zjrs.zwnw.rlzysc.entity.YgbadwMapper;
import com.zjrs.zwnw.rlzysc.entity.YgbaygMapper;
import com.zjrs.zwnw.rlzysc.entity.YgbaygzsMapper;
import com.zjrs.zwnw.rlzysc.po.RsRlzyYgbaygYwPO;
import com.zjrs.zwnw.rlzysc.po.RsRlzyYgbaygZsPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.mohrss.leaf.core.framework.util.SecurityUtils.getCurrentUser;

@Service
public class YgbaBLOImpl implements YgbaBLO {

    private static final Logger LOG = LoggerFactory.getLogger(YgbaBLOImpl.class);

    @Resource
    private YgbadwMapper ygbadwMapper;

    @Resource
    private YgbaygMapper ygbaygMapper;
    @Resource
    private CommonBLO commonBLO;
    @Resource
    private YgbaygzsMapper ygbaygzsMapper;

    @Override
    public PageResult ygbaGetYgxxList(YgbaDwxxQueryDTO dto, Integer pagenum, Integer pagesize){
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) ygbadwMapper.ygbaGetYgxxList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public Map<String, Object> getDwxx(YgbaygCommDTO dto){
        return this.ygbadwMapper.getDwxx(dto.getIds());
    }

    @Override
    public PageResult ygbaGetZgmcShList(YgbaYgxxQueryDTO dto, Integer pagenum, Integer pagesize){
        Page<RsRlzyYgbaygYwPO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<RsRlzyYgbaygYwPO> pageResult = (Page) ygbadwMapper.ygbaGetZgmcShList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void ygbaYgxxSh(YgbaygShDTO dto){
        RsRlzyYgbaygYwPO ywupo = new RsRlzyYgbaygYwPO();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String currentTime = this.commonBLO.getCurrenttime();//获取当前时间
        ywupo.setBae004(userid);//最近修改人
        ywupo.setBae005(currentTime);//最近修改时间

        String ids = dto.getIds();
        String[] idArr = ids.split(",");
        List<String> list = Arrays.asList(idArr);

        for(String id : list){
            //更新业务表
            ywupo.setShzt(dto.getShzt());
            this.ygbaygMapper.updateById(ywupo);
            //审核通过，回写正式表
            if(dto.getShzt().equals("1")){
                RsRlzyYgbaygYwPO ywpo = this.ygbaygMapper.selectById(id);
                RsRlzyYgbaygZsPO zspo = new RsRlzyYgbaygZsPO();
                zspo.setBae004(userid);//最近修改人
                RsRlzyYgbaygZsPO checkzspo = new RsRlzyYgbaygZsPO();
                QueryWrapper<RsRlzyYgbaygZsPO> queryWrapper = new QueryWrapper<>();
                checkzspo = ygbaygzsMapper.selectOne(queryWrapper);

                if(checkzspo == null){//该职工不存在，新增
                    BeanUtils.copyProperties(ywpo, zspo);
                    this.ygbaygzsMapper.insert(zspo);
                }else{//该职工已存在，修改
                    checkzspo.setHjfxzqh(ywpo.getHjfxzqh());
                    checkzspo.setBae004(userid);//最近修改人
                    checkzspo.setBae005(currentTime);//最近修改时间
                    this.ygbaygzsMapper.updateById(checkzspo);

                }
            }

        }
    }

    @Override
    public PageResult ygbaGetZgmcCkList(YgbaYgxxQueryDTO dto, Integer pagenum, Integer pagesize){
        Page<RsRlzyYgbaygZsPO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<RsRlzyYgbaygZsPO> pageResult = (Page) ygbadwMapper.ygbaGetZgmcCkList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void ygbaZgmcExport(YgbaYgxxQueryDTO dto, HttpServletResponse res){
        List<YgbaZgmcExportDTO> result = ygbadwMapper.ygbaZgmcExport(dto);
        InputStream inputStream = null;
        //离散数据

        Map<String ,Object> qychtjRows =this.ygbadwMapper.getDwxx(dto.getRsrlzyygbadwlsh());
        qychtjRows.put("dwmc","企业名称："+qychtjRows.get("aab004"));
        try {
            inputStream = YgbaBLOImpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "ygba_zgmc_export.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 ygbaZgmcQuit(YgbaygQuitDTO dto){
        RsRlzyYgbaygYwPO ywpo = new RsRlzyYgbaygYwPO();
        RsRlzyYgbaygYwPO ywtemppo = new RsRlzyYgbaygYwPO();
        RsRlzyYgbaygZsPO zspo = new RsRlzyYgbaygZsPO();
        RsRlzyYgbaygZsPO zstemppo = new RsRlzyYgbaygZsPO();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String currentTime = this.commonBLO.getCurrenttime();//获取当前时间
        
        zspo.setLzsj(dto.getLzsj());

        String ids = dto.getIds();
        String[] idArr = ids.split(",");
        List<String> list = Arrays.asList(idArr);

        //校验入职时间是否大于离职时间
        for(String id : list){
            //更新正式表
            zstemppo = this.ygbaygzsMapper.selectById(id);
            if("01".equals(zstemppo.getRyzt())){
                zspo.setRsrlzyygbaygzslsh(id);
                this.ygbaygzsMapper.updateById(zspo);
            }
            //更新业务表
            QueryWrapper<RsRlzyYgbaygYwPO> queryWrapper = new QueryWrapper<>();
            ywtemppo = ygbaygMapper.selectOne(queryWrapper);
            if(ywtemppo != null){
                if("01".equals(zstemppo.getRyzt())){
                    ywpo.setRsrlzyygbaygywlsh(ywtemppo.getRsrlzyygbaygywlsh());
                    this.ygbaygMapper.updateById(ywpo);
                }
            }
        }

    }


    @Override
    public PageResult ygbaGetZgmccxList(YgbaZgmcQueryDTO dto){
        Page<YgbaZgmcListDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<YgbaZgmcListDTO> pageResult = (Page) ygbadwMapper.ygbaGetZgmccxList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void ygbaZgmccxExport(YgbaZgmcQueryDTO dto, HttpServletResponse res){
        List<YgbaZgmccxExportDTO> result = ygbadwMapper.ygbaZgmccxExport(dto);
        InputStream inputStream = null;

        try {
            inputStream = YgbaBLOImpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "ygba_zgmccx_export.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();
                }
            }
        }
    }

}
