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 com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
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 org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.rlzysc.blo.ZphglBLO;
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 com.zjrs.zwnw.sxgl.blo.impl.JcsxqdkBLOimpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ZphglBLOimpl extends BLOImpl implements ZphglBLO {
    private static final Logger LOG = LoggerFactory.getLogger(ZphglBLOimpl.class);
    @Resource
    private VRsRlzyZphglMapper vRsRlzyZphglMapper;

    @Resource
    private VRsRlzyHcglMapper vRsRlzyHcglMapper;

    @Resource
    private VRsRlzyZwglMapper vRsRlzyZwglMapper;

    @Resource
    private RcqdMapper rcqdMapper;

    @Resource
    private VRsRlzyZwwzglMapper vRsRlzyZwwzglMapper;

    @Autowired
    private RsRlzyZphfwLogMapper rsRlzyZphfwLogMapper;

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

    @Override
    public Map getZphXx(ZjQueryDTO dto) {
        Map<String, Object> res = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(dto.getFlag())) {
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dataString = dateFormat.format(date);
            CurrentUser currentUser = getCurrentUser();
            String username = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
            String orgname = currentUser.getOrgname() == null ? "" : currentUser.getOrgname().trim();
            String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
            String flag = dto.getFlag().trim();
            if ("add".equals(flag)) {
                res.put("bae002", username);//创建人
                res.put("zphjblx", dto.getZphjblx());//招聘会举办类型
            } else if ("edit".equals(flag) || "view".equals(flag)) {
                res = vRsRlzyZphglMapper.getZphXx(dto,userid);
                if ("02".equals(dto.getZphjblx())) {
                    String rsrlzyzphgllsh = (String) res.get("rsrlzyzphgllsh");
                    res.putAll(vRsRlzyZphglMapper.getZphWscs(rsrlzyzphgllsh));
                    int lll = rsRlzyZphfwLogMapper.selectCount(new QueryWrapper<RsRlzyZphfwLogPO>()
                            .eq("rs_rlzy_zphgl", rsrlzyzphgllsh));
                    res.put("lll",String.valueOf(lll)+"次");
                }
            }
        } else {
            throw new AppException("flag参数不能为空");
        }
        return res;
    }

    public String getZphbh() {
        String zphbh = "";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dataString = dateFormat.format(date);
        Map<String, Object> res = vRsRlzyZphglMapper.getZphbh(dataString);
        if (res != null && res.get("fw") != null) {
            zphbh = dataString + String.format("%03d", (Integer.parseInt((String) res.get("fw")) + 1));
        } else {
            zphbh = dataString + String.format("%03d", 1);
        }
        return zphbh;
    }

    @Override
    public String saveZphXx(ZphXxUDTO dto) throws ParseException {
        VRsRlzyZphglPO po = new VRsRlzyZphglPO();
        BeanUtils.copyProperties(dto, po);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "1" : currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid() == null ? "2" : currentUser.getOrgid().trim();
        po.setBae004(userid);//最近修改人
        if (StringUtils.isNotBlank(po.getRsrlzyzphgllsh())) {
            //修改
            //先查询该招聘会是否有效且未发布,才能进行修改
            int cnt = vRsRlzyZphglMapper.selectCount(new QueryWrapper<VRsRlzyZphglPO>()
                    .eq("rs_rlzy_zphgl", dto.getRsrlzyzphgllsh().trim()));//未发布
            if (cnt > 0) {
                vRsRlzyZphglMapper.updateById(po);
            } else {
                throw new AppException("该招聘会已删除或已发布，不能进行此操作");
            }
        } 
        return po.getRsrlzyzphgllsh();
    }


    @Override
    public void delZphXx(ZjDTO dto) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid() == null ? "" : currentUser.getOrgid().trim();
        VRsRlzyZphglPO po = new VRsRlzyZphglPO();
        po.setRsrlzyzphgllsh(dto.getZjlsh().trim());
        //先查询该招聘会是否有效且未发布,才能删除
        int cnt = vRsRlzyZphglMapper.selectCount(new QueryWrapper<VRsRlzyZphglPO>()
               .eq("rs_rlzy_zphgl", dto.getZjlsh().trim()));//未发布
        if (cnt > 0) {
            vRsRlzyZphglMapper.updateById(po);
        } else {
            throw new AppException("该招聘会不存在或已删除或已发布，不能进行此操作");
        }
    }

    @Override
    public void endZphXx(ZjDTO dto) {
        VRsRlzyZphglPO po = new VRsRlzyZphglPO();        
        po.setRsrlzyzphgllsh(dto.getZjlsh().trim());
        //先查询该招聘会是否有效且已发布，才能停止
        int cnt = vRsRlzyZphglMapper.selectCount(new QueryWrapper<VRsRlzyZphglPO>()
                .eq("rs_rlzy_zphgl", dto.getZjlsh().trim()));
        if (cnt > 0) {
            vRsRlzyZphglMapper.updateById(po);
        } else {
            throw new AppException("该招聘会不存在或已删除或未发布，不能进行此操作");
        }
    }

    @Override
    public void UpdateZphFbzt(ZjUDTO dto) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid() == null ? "" : currentUser.getOrgid().trim();
        VRsRlzyZphglPO po = new VRsRlzyZphglPO();
        po.setRsrlzyzphgllsh(dto.getZjlsh().trim());

        //查询该招聘会zphjblx	招聘会举办类型
        VRsRlzyZphglPO zphglpo = vRsRlzyZphglMapper.selectOne(new QueryWrapper<VRsRlzyZphglPO>()
//                .select("zphjblx, jbrqc")
                .eq("rs_rlzy_zphgl", dto.getZjlsh().trim()));
        String zphjblx = zphglpo.getZphjblx() == null ? "" : zphglpo.getZphjblx();


        if ("02".equals(zphjblx)) {//网络招聘会
            if ("1".equals(dto.getFlag().trim())) {
                if(!isLatesWeeks(zphglpo.getJbrqc(), date, -7)) {
                    throw new AppException("招聘会离开始时间7天内才可以发布，请核查！");
                }

                vRsRlzyZphglMapper.updateById(po);
            } else if ("0".equals(dto.getFlag().trim())) {
                //招聘会取消发布
                vRsRlzyZphglMapper.updateById(po);
            }
        } 
    }

    public boolean isLatesWeeks(String target, Date date, int dayOfMonth) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date targetDate = simpleDateFormat.parse(target);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(targetDate);
            calendar.add(Calendar.DAY_OF_MONTH, dayOfMonth);
            Date before7days = calendar.getTime();

            return before7days.getTime() <= date.getTime() ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Map<String, Object> getZphEwm(ZjDTO dto) {
        Map<String, Object> res = new HashMap<String, Object>();
        VRsRlzyZphglPO po = vRsRlzyZphglMapper.selectOne(new QueryWrapper<VRsRlzyZphglPO>()
                .select("rs_rlzy_zphgl", "zphbh", "zphmc")
                .eq("rs_rlzy_zphgl", dto.getZjlsh().trim())
                .eq("fbzt", "1").eq("ts_yxbz", "1"));
        if (po != null) {
            String zjlsh = po.getRsrlzyzphgllsh() == null ? "" : po.getRsrlzyzphgllsh().trim();
            String zphbh = po.getZphbh() == null ? "" : po.getZphbh().trim();
            String zphmc = po.getZphmc() == null ? "" : po.getZphmc().trim();
            if (StringUtils.isNotBlank(zjlsh)) {
                res.put("filename", zphmc + zphbh);
            } else {
                throw new AppException("该招聘会不存在或未发布，不能进行此操作");
            }
        }
        return res;
    }

    //生成二维码图片字节流
    public byte[] makeQRcode(String chah, int picWidth, int picHeight, String picType) {
        Hashtable<EncodeHintType, Object> hints = null;
        ByteArrayOutputStream ret = null;
        BitMatrix byteMatrix;
        byte[] b = null;
        try {
            hints = new Hashtable<EncodeHintType, Object>();
            ret = new ByteArrayOutputStream();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //参数顺序分别为：编码内容，编码类型，生成图片宽度，生成图片高度，设置参数
            byteMatrix = new MultiFormatWriter().encode(new String(chah.getBytes("gbk"), "iso-8859-1"),
                    BarcodeFormat.QR_CODE, picWidth, picHeight, hints);
            MatrixToImageWriter.writeToStream(byteMatrix, picType, ret);
            b = ret.toByteArray();
            ret.close();
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

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

    @Override
    public Map getHcXx(ZjQueryDTO dto) {
        Map<String, Object> res = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(dto.getFlag())) {
            CurrentUser currentUser = getCurrentUser();
            String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
            String orgname = currentUser.getOrgname() == null ? "" : currentUser.getOrgname().trim();
            String flag = dto.getFlag().trim();
            if ("add".equals(flag)) {
                res.put("bae001", orgname);//创建人
            } else if ("edit".equals(flag) || "view".equals(flag)) {
                res = vRsRlzyHcglMapper.getHcXx(dto,userid);
            }
        } else {
            throw new AppException("flag参数不能为空");
        }
        return res;
    }

    public String getHcbh() {
        String hcbh = "";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dataString = dateFormat.format(date);
        Map<String, Object> res = vRsRlzyHcglMapper.getHcbh(dataString);
        if (res != null && res.get("fw") != null) {
            hcbh = dataString + String.format("%03d", (Integer.parseInt((String) res.get("fw")) + 1));
        } else {
            hcbh = dataString + String.format("%03d", 1);
        }
        return hcbh;
    }

    @Override
    public String saveHcXx(HcXxUDTO dto) {
        VRsRlzyHcglPO po = new VRsRlzyHcglPO();
        BeanUtils.copyProperties(dto, po);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setBae004(userid);//最近修改人
        if (StringUtils.isNotBlank(po.getRsrlzyhcgllsh())) {
            //修改
            //先查询该会场是否有效且未发布,才能进行修改
            int cnt = vRsRlzyHcglMapper.selectCount(new QueryWrapper<VRsRlzyHcglPO>()
                    .eq("rs_rlzy_hcgl", dto.getRsrlzyhcgllsh().trim()));//未发布
            if (cnt > 0) {
                vRsRlzyHcglMapper.updateById(po);
            } else {
                throw new AppException("该会场不存在或已删除或已发布，不能进行此操作");
            }
        } else {
            //新增
            po.setFbzt("0");//发布状态
            vRsRlzyHcglMapper.insert(po);
        }
        return po.getRsrlzyhcgllsh();
    }

    @Override
    public void delHcXx(ZjDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        VRsRlzyHcglPO po = new VRsRlzyHcglPO();
        po.setBae004(userid);//最近修改人
        po.setTsyxbz("0");//有效标志
        po.setRsrlzyhcgllsh(dto.getZjlsh().trim());
        //先查询该会场是否有效且未发布,才能进行删除
        int cnt = vRsRlzyHcglMapper.selectCount(new QueryWrapper<VRsRlzyHcglPO>()
                .eq("rs_rlzy_hcgl", dto.getZjlsh().trim()));//未发布
        if (cnt > 0) {
            vRsRlzyHcglMapper.updateById(po);
        } else {
            throw new AppException("该会场不存在或已删除或已发布，不能进行此操作");
        }
    }

    @Override
    public void updateHcFbzt(ZjUDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "1" : currentUser.getUserid().trim();
        VRsRlzyHcglPO po = new VRsRlzyHcglPO();
        po.setBae004(userid);//最近修改人
        po.setRsrlzyhcgllsh(dto.getZjlsh().trim());

        //查询该会场关联的招聘会和展位
        Map<String, String> map = new HashMap<String, String>();
        map.put("zwfbzt", "1");
        List<Map<String, Object>> cnt = vRsRlzyZphglMapper.getZphHcZwCnt(map);
        if (cnt != null) {
            for (int i = 0; i < cnt.size(); i++) {
                Map<String, Object> xxmap = cnt.get(i);
                if (xxmap != null) {
                    xxmap.put("bae004", userid);
                }
            }
        }

        if ("1".equals(dto.getFlag().trim())) {
            //会场下有展位后，才能发布该会场
            if (cnt != null && cnt.size() > 0) {
                vRsRlzyHcglMapper.updateById(po);
                //会场发布，其上招聘会会自动发布
                vRsRlzyZphglMapper.updateZphFb(cnt);
            } else {
                throw new AppException("该会场下没有已发布的展位，不能发布");
            }
        } 
    }

    /**
     * 查询未发布的展位
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, Object> getZwglList(ZwQueryDTO dto) {
        Map<String, Object> map = new HashMap<>();
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyZwglMapper.getZwglList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        map.put("result", result);
        return map;
    }

    @Override
    public Map getZwXx(ZjQueryDTO dto) {
        Map<String, Object> res = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(dto.getFlag())) {
            CurrentUser currentUser = getCurrentUser();
            String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
            String username = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
            String flag = dto.getFlag().trim();
            if ("add".equals(flag)) {
                //新增根据选择的会场管理流水号带出招聘会信息
                Map<String, Object> zwxx = vRsRlzyZwglMapper.getHcToZphXx(dto.getZjlsh().trim());
                if (zwxx != null) {
                    res.putAll(zwxx);
                }
                res.put("bae002", username);//创建人
            } else if ("edit".equals(flag) || "view".equals(flag)) {
                //先查询展位管理数据
                Map<String, Object> zwglxx = vRsRlzyZwglMapper.getZwglXx(dto.getZjlsh().trim(),userid);
                if (zwglxx != null) {
                    res.putAll(zwglxx);
                }
                String rsrlzyzwgllsh = res.get("rsrlzyzwgllsh") == null ? "" : (String) res.get("rsrlzyzwgllsh");
                if (StringUtils.isNotBlank(rsrlzyzwgllsh)) {
                    List<List<Map<String, Object>>> zwlist = new ArrayList<List<Map<String, Object>>>();
                    if (zwlist != null) {
                        res.put("zwlist", zwlist);
                    }
                }
            }
        } else {
            throw new AppException("flag参数不能为空");
        }
        return res;
    }

    @Override
    public Map<String, Object> saveZwXx(ZwXxUDTO dto) {
        HashMap<String, Object> res = new HashMap<String, Object>();
        //保存展位管理信息
        VRsRlzyZwglPO zwglpo = new VRsRlzyZwglPO();
        BeanUtils.copyProperties(dto, zwglpo);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid() == null ? "" : currentUser.getOrgid().trim();
        zwglpo.setBae004(userid);//最近修改人
        zwglpo.setBae005(dataString);//最近修改时间
        zwglpo.setBae006(orgid);//修改系统机构代码
        if (StringUtils.isNotBlank(dto.getRsrlzyzwgllsh())) {
            //修改
            vRsRlzyZwglMapper.updateById(zwglpo);
        } 
        List<VRsRlzyZwwzglPO> insertlist = new ArrayList<VRsRlzyZwwzglPO>();
        List<VRsRlzyZwwzglPO> updatelist = new ArrayList<VRsRlzyZwwzglPO>();
        List<List<Map<String, Object>>> zwlist = dto.getZwlist();
        if (zwlist != null) {
            for (int i = 0; i < zwlist.size(); i++) {
                List<Map<String, Object>> list = zwlist.get(i);
                for (int j = 0; j < list.size(); j++) {
                    Map<String, Object> map = list.get(j);
                    VRsRlzyZwwzglPO po = new VRsRlzyZwwzglPO();
                    String rsrlzyzwwzgllsh = map.get("rsrlzyzwwzgllsh") == null ? "" : (String) map.get("rsrlzyzwwzgllsh");
                    String rsrlzyzwgllsh = dto.getRsrlzyzwgllsh() == null ? "" : (String) dto.getRsrlzyzwgllsh();
                    po.setSsqy(map.get("ssqy") == null ? "" : (String) map.get("ssqy"));
                    if (StringUtils.isNotBlank(po.getHsh()) && StringUtils.isNotBlank(po.getLsh()) && StringUtils.isNotBlank(po.getZwzt()) && (StringUtils.isNotBlank(po.getZwwzmc()) || (!StringUtils.isNotBlank(po.getZwwzmc()) && "0".equals(po.getZwzt())))) {
                        if (StringUtils.isNotBlank(rsrlzyzwwzgllsh) && StringUtils.isNotBlank(rsrlzyzwgllsh)) {
                            po.setRsrlzyzwgllsh(rsrlzyzwgllsh);
                            updatelist.add(po);
                        } 
                    } else {
                        throw new AppException("请检查展位是否正确生成");
                    }
                }
            }
        }
        if (insertlist.size() > 0) {
            vRsRlzyZwwzglMapper.insertZwXx(insertlist);
        }
        if (updatelist.size() > 0) {
            vRsRlzyZwwzglMapper.updateZwXx(updatelist);
        }
        List<VRsRlzyZwwzglPO> alllist = new ArrayList<VRsRlzyZwwzglPO>();
        alllist.addAll(insertlist);
        res.put("zwlist", alllist);
        return res;
    }

    @Override
    public void delZwXx(ZjDTO dto) {
        VRsRlzyZwglPO po = new VRsRlzyZwglPO();
        po.setRsrlzyzwgllsh(dto.getZjlsh().trim());
        //先查询该展位是否有效且未发布,才能进行删除

        int cnt = vRsRlzyZwglMapper.selectCount(new QueryWrapper<VRsRlzyZwglPO>()
                .eq("rs_rlzy_zwgl", dto.getZjlsh().trim()));//未发布
        if (cnt > 0) {
            vRsRlzyZwglMapper.updateById(po);
        } else {
            throw new AppException("该展位不存在或已删除或已发布，不能进行此操作");
        }
    }

    @Override
    public void updateZwFbzt(ZjUDTO dto) {
        VRsRlzyZwglPO po = new VRsRlzyZwglPO();
        po.setRsrlzyzwgllsh(dto.getZjlsh().trim());

        //查询该会场关联的招聘会和展位
        Map<String, String> map = new HashMap<String, String>();
        map.put("zwlsh", dto.getZjlsh().trim());
        List<Map<String, Object>> cnt = vRsRlzyZphglMapper.getZphHcZwCnt(map);

        if ("1".equals(dto.getFlag().trim())) {
            //已有该展位信息，才能发布该展位
            if (cnt != null && cnt.size() > 0) {
                vRsRlzyZwglMapper.updateById(po);
                //展位发布，其上招聘会和会场会自动发布
                //vRsRlzyZphglMapper.updateZphFb(cnt);//限制20天内才能发布，去掉自动发布
                vRsRlzyHcglMapper.updateHcFb(cnt);
            } else {
                throw new AppException("该展位不存在，不能发布");
            }
        } else if ("0".equals(dto.getFlag().trim())) {
            //展位取消发布
            vRsRlzyZwglMapper.updateById(po);
        }
    }

    /**
     * 查询已经发布的展位
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, Object> getZwsyqkckList(ZwQueryDTO dto) {
        Map<String, Object> map = new HashMap<>();
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyZwglMapper.getZwsyqkckList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        map.put("result", result);
        return map;
    }

    @Override
    public Map getZwsyqkckXx(ZjDTO dto) {
        Map<String, Object> res = new HashMap<String, Object>();
        //根据展位管理流水号获取招聘会信息
        Map<String, Object> zphxx = vRsRlzyZwglMapper.getZwToZphHcXx(dto.getZjlsh().trim());
        if (zphxx != null) {
            res.putAll(zphxx);
        }

        List<List<Map<String, Object>>> zwlist = new ArrayList<List<Map<String, Object>>>();
        //再查询展位位置管理数据
        List<String> hshlist = vRsRlzyZwwzglMapper.getDixtinctHshList(dto.getZjlsh().trim());

        for (int i = 0; i < hshlist.size(); i++) {
            String hsh = hshlist.get(i);
            HashMap<String, Object> qmap = new HashMap<String, Object>();
            qmap.put("hsh", hsh);
            //再查询展位位置管理数据
            List<Map<String, Object>> zwhshlist = vRsRlzyZwwzglMapper.getZwList(qmap);
            if (zwhshlist != null) {
                zwlist.add(zwhshlist);
            }
        }
        if (zwlist != null) {
            res.put("zwlist", zwlist);
        }
        return res;
    }

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

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

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

    @Override
    public void exportYdzphgl(ZphglExportDTO dto, HttpServletResponse res) {
        QueryWrapper<VRsRlzyZphglPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_rlzy_zphgl", dto.getRsrlzyzphgllsh());
        VRsRlzyZphglPO zphglPo = vRsRlzyZphglMapper.selectOne(queryWrapper);
        zphglPo.setLxzj("联系座机:" + zphglPo.getLxzj());
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_ydxx.xlsx");

            String fileName = zphglPo.getZphmc() + ".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 PageResult queryQychtjList(QychtjDTO 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) vRsRlzyZphglMapper.queryQychtjList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportQychtj(QychtjDTO dto, HttpServletResponse res) {
        CurrentUser currentUser = getCurrentUser();
        String userregioncode = UserUtil.getBae001(currentUser);//行政区划代码
        dto.setUserregioncode(userregioncode);
        List<QychtjExportResultDTO> result = vRsRlzyZphglMapper.queryExportQychtj(dto);
        for (int i = 0; i < result.size(); i++) {
            result.get(i).setRownum(String.valueOf(i + 1));
        }
        Map<String, String> qychtjRows = new HashMap<>();

        StringBuilder titleBuilder = new StringBuilder("企业参会统计导出(统计年月: ")
                .append(dto.getTjnyc() == null ? "" : dto.getTjnyc()).append(" 至 ")
                .append(dto.getTjnyz() == null ? "" : dto.getTjnyz()).append(" )");

        qychtjRows.put("title", titleBuilder.toString());
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_qychtj.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 PageResult queryQychtjDetailsList(QychtjDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyZphglMapper.queryQychtjDetailsList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void UpdateZphBmzt(ZjUDTO dto) {
        UpdateWrapper<VRsRlzyZphglPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("rs_rlzy_zphgl", dto.getZjlsh());
        VRsRlzyZphglPO po = new VRsRlzyZphglPO();
        this.vRsRlzyZphglMapper.update(po, updateWrapper);
    }

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

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

    @Override
    public void exportWlzphZwXx(ZjDTO dto, HttpServletResponse res) {
        //列表数据
        QueryWrapper<VRsRlzyZphglPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_rlzy_zphgl",dto.getZjlsh());
        List<ExportZphZwXxResDTO> wlzphZwXxList = vRsRlzyZphglMapper.exportWlzphZwXx(dto);
        for (int i = 0;i < wlzphZwXxList.size();i++){
            wlzphZwXxList.get(i).setRsrlzyzwxxlsh(String.valueOf(i+1));
        }
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "wlzph_zwxx.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 exportZwtj(QychtjDTO dto, HttpServletResponse res) {
        List<ZwtjExportResultDTO> result = vRsRlzyZphglMapper.queryExportZwtj(dto);
   
        QueryWrapper<VRsRlzyZphglPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_rlzy_zphgl", dto.getZphlsh());

        InputStream inputStream = null;

        // 处理excel内容
        List<ZwtjExportResultDTO> _result = new ArrayList<>();
        List<String> parentZwmcs = new ArrayList<>();

        result.forEach(item -> {
            if(parentZwmcs.indexOf(item.getParentzwmc()) == -1) {
                parentZwmcs.add(item.getParentzwmc());
            }
        });

        parentZwmcs.forEach(parent ->{
            ZwtjExportResultDTO parentDto = new ZwtjExportResultDTO();
            parentDto.setParentzwmc(parent);
            parentDto.setZwmc("");
            parentDto.setNum(0);
            _result.add(parentDto);

            result.forEach(item -> {
                if(parent.equals(item.getParentzwmc())) {
                    ZwtjExportResultDTO childrenDto = new ZwtjExportResultDTO();
                    childrenDto.setParentzwmc("");
                    _result.add(childrenDto);
                }
            });
        });

        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_zwtj.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 exportZwtjRylb(QychtjDTO dto, HttpServletResponse res) {
        Map<String, String> rows = new HashMap<>();

        QueryWrapper<VRsRlzyZphglPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_rlzy_zphgl", dto.getZphlsh());
        VRsRlzyZphglPO zphgl = this.vRsRlzyZphglMapper.selectOne(queryWrapper);

        StringBuilder ssdqBuilder  = new StringBuilder("所属地区：")
                .append(zphgl.getBae001() == null ? "" : zphgl.getBae001());

        rows.put("ssdq", ssdqBuilder.toString());

        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_zwtj_rylb.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 UpdateZphZd(ZjUDTO dto){
        VRsRlzyZphglPO po = new VRsRlzyZphglPO();
        
        po.setRsrlzyzphgllsh(dto.getZjlsh().trim());
        vRsRlzyZphglMapper.updateById(po);
    }

    @Override
    public PageResult getZphWscsList(ZphQueryDTO dto) {
        Page<WscsmxListDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<WscsmxListDTO> pageResult = (Page) vRsRlzyZphglMapper.getZphWscsList(page,dto.getZphlsh());
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportZphfbWscsmx(ZphQueryDTO dto, HttpServletResponse res){
        Page<WscsmxListDTO> page = new Page<>(1, 10000);
        Page<WscsmxListDTO> pageResult = (Page) vRsRlzyZphglMapper.getZphWscsList(page,dto.getZphlsh());

        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_zphfb_wscsmx.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 exportQychtjRylbtj(ZphQueryDTO dto, HttpServletResponse res){
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_qychtj_rylbtj.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 exportQychtjZwtj(ZphQueryDTO dto, HttpServletResponse res){
        CurrentUser currentUser = getCurrentUser();
        String bae001 = UserUtil.getBae001(currentUser);
        List<ZwtjExportResultDTO> result = vRsRlzyZphglMapper.exportQychtjZwtj(dto,bae001);

        InputStream inputStream = null;

        // 处理excel内容
        List<ZwtjExportResultDTO> _result = new ArrayList<>();
        List<String> parentZwmcs = new ArrayList<>();

        result.forEach(item -> {
            if(parentZwmcs.indexOf(item.getParentzwmc()) == -1) {
                parentZwmcs.add(item.getParentzwmc());
            }
        });

        parentZwmcs.forEach(parent ->{
            ZwtjExportResultDTO parentDto = new ZwtjExportResultDTO();
            parentDto.setParentzwmc(parent);
            parentDto.setZwmc("");
            parentDto.setNum(0);
            _result.add(parentDto);

        });

        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_qychtj_zwtj.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 exportAllWscsmx(ZphQueryDTO dto, HttpServletResponse res) {
        List<WscsmxListDTO> result = vRsRlzyZphglMapper.exportAllWscsmx(dto);
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_zphfb_wscsmx.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 exportBmcjqysDc(ZphQueryDTO dto, HttpServletResponse res) {
        List<ExportBmcjQysDTO> result = vRsRlzyZphglMapper.exportBmcjqysDc(dto);
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_zphfb_bmcjqysdc.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 exportHzdc(ZphQueryDTO dto, HttpServletResponse res){
        //-----------------招聘岗位数、招聘人数及网申次数统计------------
        CurrentUser currentUser = getCurrentUser();
        String bae001 = UserUtil.getBae001(currentUser);
        List<ZwtjExportResultDTO> result = vRsRlzyZphglMapper.exportQychtjZwtj(dto,bae001);

        // 处理excel内容
        List<ZwtjExportResultDTO> _result = new ArrayList<>();
        List<String> parentZwmcs = new ArrayList<>();

        result.forEach(item -> {
            if(parentZwmcs.indexOf(item.getParentzwmc()) == -1) {
                parentZwmcs.add(item.getParentzwmc());
            }
        });
       //-----------------报名参加企业数及浏览量统计------------
        List<QychtjExportHzBmqyDTO> result2 = vRsRlzyZphglMapper.exportQychtjHzBmqy(dto,bae001);
        InputStream inputStream = null;
        InputStream inputStream2 = null;
        //离散数据
        Map<String ,Object> qychtjRows =new HashMap<>();
        String jzrqc = "";
        String jzrqz = "";
        if(StringUtils.isNotBlank(dto.getJzrqc())){
            jzrqc=dto.getJzrqc();
        }
        if(StringUtils.isNotBlank(dto.getJzrqz())){
            jzrqz = dto.getJzrqz();
        }
        qychtjRows.put("title","举办截止日期从："+jzrqc+" 至 "+jzrqz);

        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "zphgl_qychtj_hztj.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();
                }
            }
            if (inputStream2 != null) {
                try {
                    inputStream2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
