package com.zjrs.zwnw.rlzysc.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 org.apache.commons.lang.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.jbxxgl.blo.DwxxglBLO;
import com.zjrs.zwnw.jbxxgl.blo.GrxxglBLO;
import com.zjrs.zwnw.rlzysc.blo.QzzpglBLO;
import com.zjrs.zwnw.rlzysc.dto.*;
import com.zjrs.zwnw.rlzysc.entity.*;
import com.zjrs.zwnw.rlzysc.po.*;
import com.zjrs.zwnw.rlzysc.util.UserUtil;
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.*;

@Service
public class QzzpglBLOimpl extends BLOImpl implements QzzpglBLO {

    @Resource
    private JlxxMapper jlxxMapper;

    @Resource
    private VRsRlzyQztjMapper vRsRlzyQztjMapper;

    @Resource
    private VRsRlzyQyxxMapper vRsRlzyQyxxMapper;

    @Resource
    private VRsRlzyZwxxMapper vRsRlzyZwxxMapper;

    @Resource
    private VRsRlzyGwtjMapper vRsRlzyGwtjMapper;

    @Resource
    private YdzwshMapper ydzwshMapper;

    @Resource
    private HmdglMapper hmdglMapper;

    @Resource
    private VRsRlzyZwwzglMapper vRsRlzyZwwzglMapper;

    @Resource
    private DwxxglBLO dwxxglBLO;

    @Resource
    private GrxxglBLO grxxglBLO;

    @Resource
    private RsGgfwUserMapper rsGgfwUserMapper;

    @Override
    public Map<String, Object> getGrqzxxshList(GrqzQueryDTO dto) {
        Map<String, Object> map = new HashMap();
        CurrentUser currentUser = getCurrentUser();
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) jlxxMapper.getGrqzxxshList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        map.put("result",result);
        map.put("bae001",UserUtil.getBae001(currentUser));
        return map;
    }

    @Override
    public Map getGrqzxxshXx(ZjQueryDTO dto) {
        Map<String, Object> result = jlxxMapper.getGrqzxxshXxAndJbxx(dto);
        return result;
    }

    @Override
    public void doGrqzxxsh(ShUDTO dto) {
        VRsRlzyJlxxPO po = new VRsRlzyJlxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setShsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setJlshr(userid);
        jlxxMapper.updateById(po);

        //从备份表取出数据回写进基本信息管理
        HashMap<String, Object> qmap = new HashMap<>();
        qmap.put("rsrlzyjlxxlsh", dto.getZjlsh());
    }

    @Override
    public void doGrqzxxshTx(ShUDTO dto) {
        VRsRlzyJlxxPO po = new VRsRlzyJlxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setTxshsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setTxshr(userid);
        jlxxMapper.updateById(po);
    }

    @Override
    public void doGrqzxxshJssp(ShUDTO dto) {
        VRsRlzyJlxxPO po = new VRsRlzyJlxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setSpshsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setSpshr(userid);
        jlxxMapper.updateById(po);
    }

    @Override
    public PageResult getGrqzxxglList(GrqzQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) jlxxMapper.getGrqzxxglList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void updateGrqzxxshZt(GrOrDwZtUDTO dto) {
        VRsRlzyJlxxPO po = new VRsRlzyJlxxPO();
        jlxxMapper.updateById(po);
    }

    @Override
    public PageResult getQzxxQzppList(GrqzQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) jlxxMapper.getQzxxQzppList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getTjdw(TjdwQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyQztjMapper.getTjdw(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void saveQzxxTjxx(TjUDTO dto) {
        List<VRsRlzyQztjPO> list = new ArrayList<VRsRlzyQztjPO>();
        vRsRlzyQztjMapper.saveQzxxTjxx(list);
    }

    @Override
    public Map<String, Object> getDwzpxxshList(ZpxxQueryDTO dto) {
        Map<String, Object> map = new HashMap<>();
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyZwxxMapper.getDwzpxxshList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        map.put("result",result);
        return map;
    }

    @Override
    public Map getDwzpxxshXx(ZjQueryDTO dto) {
        Map<String, Object> result = vRsRlzyZwxxMapper.getDwzpxxshXx(dto);
        return result;
    }

    @Override
    public void doDwzpxxsh(ShUDTO dto) {
        VRsRlzyZwxxPO po = new VRsRlzyZwxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date).trim();
        po.setShsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setZwxxshr(userid);
        vRsRlzyZwxxMapper.updateById(po);
    }

    @Override
    public Map getQyRsyhlsh(String zjlsh) {
        Map res = new HashMap();
        VRsRlzyZwxxPO zwxxpo = vRsRlzyZwxxMapper.selectOne(new QueryWrapper<VRsRlzyZwxxPO>().eq("rs_rlzy_zwxx_lsh",zjlsh));
        String zwmc = zwxxpo==null?"":zwxxpo.getZwmc();
        res.put("zwmc",zwmc);
        return res;
    }

    @Override
    public void doDwzpxxshXc(ShUDTO dto) {
        VRsRlzyQyxxPO po = new VRsRlzyQyxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setLogoshsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setLogoshr(userid);
        vRsRlzyQyxxMapper.updateById(po);
    }

    @Override
    public void doDwzpxxshJssp(ShUDTO dto) {
        VRsRlzyQyxxPO po = new VRsRlzyQyxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setSpshsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setSpshr(userid);
        vRsRlzyQyxxMapper.updateById(po);
    }

    @Override
    public PageResult getDwzpxxglList(ZpxxQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyZwxxMapper.getDwzpxxglList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportDwzpxx(ZpxxQueryDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = QzzpglBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "qyzpzwxxExport.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 updateDwzpxxglZt(GrOrDwZtUDTO dto) {
        VRsRlzyZwxxPO po = new VRsRlzyZwxxPO();
        if (StringUtils.isNotBlank(dto.getZt())) {
            po.setZwzt(dto.getZt().trim());
        }
        if (StringUtils.isNotBlank(dto.getZjlsh())) {
            po.setRsrlzyzwxxlsh(dto.getZjlsh().trim());
        }
        vRsRlzyZwxxMapper.updateById(po);
    }

    @Override
    public PageResult getZpxxGwppList(ZpxxQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyZwxxMapper.getZpxxGwppList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getTjr(TjrQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyGwtjMapper.getTjr(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void saveZpxxTjxx(TjUDTO dto) {
        List<VRsRlzyGwtjPO> list = new ArrayList<VRsRlzyGwtjPO>();
        vRsRlzyGwtjMapper.saveZpxxTjxx(list);
    }

    @Override
    public PageResult getZphList(YdZphQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userregioncode = UserUtil.getBae001(currentUser);//行政区划代码
        dto.setUserregioncode(userregioncode);
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) ydzwshMapper.getZphList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getYdzwshList(YdQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) ydzwshMapper.getYdzwshList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public Map getYdxx(ZjQueryDTO dto) {
        String yypc = dto.getZjlsh().trim();
        //基本预定信息
        Map<String, Object> jbxx = ydzwshMapper.getYdxx(yypc);
        if (jbxx != null) {
            //预定展位
            String ydzw = ydzwshMapper.getzwdzconcat(yypc) == null ? "" : ydzwshMapper.getzwdzconcat(yypc);
            if (ydzw != null) {
                jbxx.put("ydzw", ydzw);
            }
        }
        return jbxx;
    }

    @Override
    public PageResult getzyxxList(ZjQueryListDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) ydzwshMapper.getzyxxList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void doYdzwsh(ShUDTO dto) {
        VRsRlzyZwwzyyshPO po = new VRsRlzyZwwzyyshPO();
        if (StringUtils.isNotBlank(dto.getShyj())) {
            po.setShyj(dto.getShyj().trim());
        }
        if (StringUtils.isNotBlank(dto.getShjg())) {
            po.setShjg(dto.getShjg().trim());
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setShsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setZwyyshr(userid);
        String orgid = currentUser.getOrgid() == null ? "" : currentUser.getOrgid().trim();
        po.setBae006(orgid);//修改系统机构代码
        ydzwshMapper.update(po, new UpdateWrapper<VRsRlzyZwwzyyshPO>().eq("yypc", dto.getZjlsh().trim()));
    }

    @Override
    public PageResult getHmdglGrList(HmdGrQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) hmdglMapper.getHmdglGrList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getHmdglDwList(HmdDwQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) hmdglMapper.getHmdglDwList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void yrHmdglZt(HmdZtUDTO dto) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        //先查询要移入黑名单的是否已存在
        int cnt = hmdglMapper.selectCount(new QueryWrapper<VRsRlzyHmdglPO>()
                .eq("hmdlx", dto.getHmdlx().trim()));
        if (cnt > 0) {} else if (cnt == 0) {
            //不存在则插入新数据
            VRsRlzyHmdglPO po = new VRsRlzyHmdglPO();
            po.setHmdlx(dto.getHmdlx().trim());
            po.setRsrlzytslsh(dto.getZjlsh().trim());
            po.setTsyxbz("1");
            po.setLhyy(dto.getLhyy());
            po.setBae003(dataString);//创建时间
            hmdglMapper.insert(po);
        }
    }

    @Override
    public void ycHmdglZt(HmdZtUDTO dto) {
        //修改有效标志为0
        VRsRlzyHmdglPO po = new VRsRlzyHmdglPO();
        po.setTsyxbz("0");
        po.setLhyy("");
        hmdglMapper.update(po, new UpdateWrapper<VRsRlzyHmdglPO>()
                .eq("hmdlx", dto.getHmdlx().trim()));
    }

    @Override
    public PageResult getDwjbxxList(DwxxQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userregioncode = UserUtil.getBae001(currentUser);//行政区划代码
        dto.setUserregioncode(userregioncode);
        if (!userregioncode.equals("440800")) //假如用户所属机构不为市本级，则清空行政区划查询
            dto.setXjzdxzqh(null);
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyQyxxMapper.getDwjbxxList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public Map getDwjbxx(ZjQueryDTO dto) {
        Map<String, Object> res = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getZjlsh())) {
            HashMap<String, Object> qmap = new HashMap<>();
            qmap.put("userid", dto.getZjlsh());
        }

        return res;
    }

    @Override
    public String doDwjbxxsh(ShUDTO dto) {
        String res = "";
        VRsRlzyQyxxPO po = new VRsRlzyQyxxPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setQyxxshsj(dataString);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setQyxxshr(userid);
        vRsRlzyQyxxMapper.updateById(po);

        //查询该企业是否已经拥有基本信息
        HashMap<String, Object> qmap = new HashMap<>();
        Map<String, Object> data = vRsRlzyQyxxMapper.getQyxxByQyid(qmap);
        if ("1".equals(po.getQyxxshzt())) {
            //如果该企业没有基本信息，则在审核通过后将基本信息回写进基本信息管理
            if (data != null){
                String rsyhlsh = (String) data.get("rsyhlsh");
                res = rsyhlsh;
            }
        } else {
            if (data != null) {
                String rsyhlsh = (String) data.get("rsyhlsh");
                res = rsyhlsh;
            }
        }
        return res;
    }

    @Override
    public void exportGrqzxxglList(GrqzQueryDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = QzzpglBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "grqzxxgl.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();
                }
            }
        }
    }
}
