package com.zjrs.ggfw.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.collections4.MapUtils;
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.ggfw.portal.blo.UserBLO;
import com.zjrs.ggfw.portal.po.VRsGgfwUser;
import com.zjrs.ggfw.rlzysc.blo.DwzpBLO;
import com.zjrs.ggfw.rlzysc.blo.InformationBLO;
import com.zjrs.ggfw.rlzysc.bpo.JbxxglFeignClientBPO;
import com.zjrs.ggfw.rlzysc.dto.*;
import com.zjrs.ggfw.rlzysc.dto.common.PageDTO;
import com.zjrs.ggfw.rlzysc.dto.dwzp.*;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.*;
import com.zjrs.ggfw.rlzysc.entity.*;
import com.zjrs.ggfw.rlzysc.po.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DwzpBLOImpl extends BLOImpl implements DwzpBLO {

    @Resource
    ZwxxMapper zwxxMapper;

    @Resource
    ZphglMapper zphglMapper;

    @Resource
    JlxxMapper jlxxMapper;

    @Resource
    JlglMapper jlglMapper;

    @Resource
    ScgzMapper scgzMapper;

    @Resource
    GzgsMapper gzgsMapper;

    @Resource
    MsyqrzMapper msyqrzMapper;

    @Resource
    private QyxxMapper qyxxMapper;

    @Resource
    private ZphYdMapper zphYdMapper;

    @Resource
    private RsRlzyZwwzyyzwxxMapper rsRlzyZwwzyyzwxxMapper;

    @Resource
    private RsRlzyRcssqMapper rsRlzyRcssqMapper;

    @Resource
    private VRsRlzyZwwzglMapper vRsRlzyZwwzglMapper;
    @Resource
    UserBLO userBLO;

    @Resource
    GwxxMapper gwxxMapper;

    @Resource
    InformationBLO informationBLO;

    @Resource
    RsRlzyZphfwLogMapper rsRlzyZphfwLogMapper;

    @Resource
    private JbxxglFeignClientBPO jbxxglFeignClientBPO;

    @Override
    public void updateGwxxTdjgbyId(String gwlsh, String tdjg) {
        GwxxPO po = new GwxxPO();
        po.setRsrlzygwxxlsh(gwlsh);
        po.setTdjg(tdjg);
        gwxxMapper.updateById(po);
    }


    @Override
    public PageResult getZwxxList(DwzpZwListQueryDTO dto) {
        QueryWrapper<RsRlzyZwxxPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_rlzy_qyxx_lsh", dto.getRsRlzyQyxxLsh());//企业流水号
        //职位
        if (StringUtils.isNotBlank(dto.getZwmc())) {
            queryWrapper.like("zwmc", dto.getZwmc());
        }
        //文化程度
        if (StringUtils.isNotBlank(dto.getWhcd())) {
            queryWrapper.eq("whcd", dto.getWhcd());
        }
        //工作经验
        if (StringUtils.isNotBlank(dto.getGzjy())) {
            queryWrapper.eq("gzjy", dto.getGzjy());
        }
        //招聘方式
        if (StringUtils.isNotBlank(dto.getZpfs())) {
            queryWrapper.eq("zpfs", dto.getZpfs());
        }
        //发布状态
        if (StringUtils.isNotBlank(dto.getFbzt())) {
            queryWrapper.eq("fbzt", dto.getFbzt());
        }
        //最新修改日期起
        if (StringUtils.isNotBlank(dto.getXgrqq())) {
            queryWrapper.ge("ts_xgsj", dto.getXgrqq() + " 00:00:00");
        }
        //最新修改日期止
        if (StringUtils.isNotBlank(dto.getXgrqz())) {
            queryWrapper.le("ts_xgsj", dto.getXgrqz() + " 23:59:59");
        }

        queryWrapper.eq("ts_yxbz", "1");//有效标志

        Page<RsRlzyZwxxPO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<RsRlzyZwxxPO> pageResult = (Page) this.zwxxMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public RsRlzyZwxxPO getRsRlzyZwxx(DwzpZwxxQueryDTO dto) {
        return zwxxMapper.selectById(dto.getRsRlzyZwxxLsh());
    }

    @Override
    public RsRlzyZwxxPO saveRsRlzyZwxx(RsRlzyZwxxPO rsRlzyZwxxPO) {
        if (StringUtils.isNotBlank(rsRlzyZwxxPO.getRsrlzyzwxxlsh())) {
            zwxxMapper.updateById(rsRlzyZwxxPO);
        } else {
            rsRlzyZwxxPO.setRsrlzyzwxxlsh(UUID.randomUUID().toString().replace("-", ""));
            zwxxMapper.insert(rsRlzyZwxxPO);
        }
        return rsRlzyZwxxPO;
    }

    @Override
    public void updateRsRlzyZwxxById(RsRlzyZwxxPO rsRlzyZwxxPO) {
        zwxxMapper.updateById(rsRlzyZwxxPO);
    }

    @Override
    public PageResult getJlxxList(DwzpJlListQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) jlglMapper.getDwzpJlxxList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void updateRsRlzyJlglById(RsRlzyJlglPO rsRlzyJlglPO) {
        jlglMapper.updateById(rsRlzyJlglPO);
    }


    public void saveRsRlzyMsyqrz(RsRlzyMsyqrzPO rsRlzyMsyqrzPO) {
        msyqrzMapper.insert(rsRlzyMsyqrzPO);
    }

    @Override
    public void updateRsRlzyScgzById(RsRlzyScgzPO rsRlzyScgzPO) {
        scgzMapper.updateById(rsRlzyScgzPO);
    }

    @Override
    public void addRsRlzyScgz(RsRlzyScgzPO rsRlzyScgzPO) {
        //校验是否为取消收藏的记录，若是直接更新收藏状态sczt为1即可
        QueryWrapper<RsRlzyScgzPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RsRlzyQyxxPO> qyxxWrapper = new QueryWrapper<>();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        qyxxWrapper.eq("rs_yhlsh", userid);
        RsRlzyQyxxPO qyxx = qyxxMapper.selectOne(qyxxWrapper);
        queryWrapper.eq("ts_yxbz", "1");
        RsRlzyScgzPO rsRlzyScgzTempPO = scgzMapper.selectOne(queryWrapper);
        if (rsRlzyScgzTempPO != null) {
            rsRlzyScgzTempPO.setSczt("1");
            scgzMapper.updateById(rsRlzyScgzTempPO);
        } else {
            rsRlzyScgzPO.setRsRlzyQyxxLsh(qyxx.getRsrlzyqyxxlsh());
            scgzMapper.insert(rsRlzyScgzPO);
        }
    }

    @Override
    public PageResult getScJlxxList(DwzpJlListQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) scgzMapper.getDwzpScJlxxList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getGzJlxxList(DwzpJlListQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) scgzMapper.getDwzpGzJlxxList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void updateRsRlzyGzgsById(GzgsPO rsRlzyGzgsPO) {
        gzgsMapper.updateById(rsRlzyGzgsPO);
    }

    @Override
    public RsRlzyQyxxPO getRsRlzyQyxxByUserid(String userid) {
        QueryWrapper<RsRlzyQyxxPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_yhlsh", userid);
        return qyxxMapper.selectOne(queryWrapper);
    }

    @Override
    public JlxxPO getRsRlzyJlxxById(String rsRlzyJlxxLsh) {
        return jlxxMapper.selectById(rsRlzyJlxxLsh);
    }

    @Override
    public PageResult getZxzphList(ZphglQueryDTO dto) {
        Page<RsRlzyXczphPO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<RsRlzyXczphPO> pageResult = null;
        pageResult = (Page) this.zphglMapper.getZxscZphxx(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getTjjlList(PageDTO dto) {
        Page<TjjlListDTO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<TjjlListDTO> pageResult = null;
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            QueryWrapper<RsRlzyQyxxPO> queryWrapper = new QueryWrapper<>();
            RsRlzyQyxxPO qyxxPO = this.qyxxMapper.selectOne(queryWrapper);
            pageResult = this.jlxxMapper.getTjjlList(page1, qyxxPO.getRsrlzyqyxxlsh());
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        } else {
            throw new AppException("您当前处于未登录状态，请先登录");
        }

    }

    @Override
    public PageResult getDwTjjlList(PageDTO dto) {
        Page<TjjlListDTO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<TjjlListDTO> pageResult = null;
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            QueryWrapper<RsRlzyQyxxPO> queryWrapper = new QueryWrapper<>();
            RsRlzyQyxxPO qyxxPO = this.qyxxMapper.selectOne(queryWrapper);
            pageResult = this.jlxxMapper.getDwTjjlList(page1, qyxxPO.getRsrlzyqyxxlsh());
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        } else {
            throw new AppException("您当前处于未登录状态，请先登录");
        }

    }

    @Override
    public List<RsRlzyJlglPO> selectGwlsh(String rsRlzyJlglLsh) {
        return jlglMapper.selectList(new QueryWrapper<RsRlzyJlglPO>().eq("RS_RLZY_JLGL_LSH", rsRlzyJlglLsh));
    }

    @Override
    public Map getQyxx(QyxxDTO dto) {
        Map<String, Object> res = new HashMap<>();
        Map<String, Object> resmap = new HashMap<>();
        if(StringUtils.isNotBlank(dto.getAab004()) && StringUtils.isNotBlank(dto.getAab998()) && dto.getFlag().equals("validate")){
            if(StringUtils.isNotBlank(dto.getZjlsh())){ //企业录入修改
                RsRlzyQyxxPO rsRlzyQyxxPO = this.qyxxMapper.selectById(dto.getZjlsh());
                if (!rsRlzyQyxxPO.getAab004().equals(dto.getAab004()) || !rsRlzyQyxxPO.getAab998().equals(dto.getAab998())){//假如相同，则不校验
                    resmap = qyxxMapper.validateQyxx(dto); //企业信息录入时检验该企业是否已存在
                }
            }else {
                resmap = qyxxMapper.validateQyxx(dto); //企业信息录入时检验该企业是否已存在
                if (resmap == null) {
                    resmap = new HashMap<>();
                    resmap.put("validate","0");
                }
            }

            res.putAll(resmap);
        }else{
            String qyssr = dto.getQyssr();
            if (StringUtils.isBlank(qyssr)){
                CurrentUser currentUser = getCurrentUser();
                qyssr = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
            }
            if (StringUtils.isNotBlank(qyssr)) {
                //本地没有该企业信息，仅需要显示最新的基本信息
                //获取基本信息管理中的单位基本信息
                Map<String, Object> data = qyxxMapper.getJbxxglDwjbxx(qyssr);
                if (data != null) {
                    res.putAll(data);
                }
            } else {
                throw new AppException("获取不到该用户信息");
            }
        }

        return res;
    }

    @Override
    public Map getSyQyxx(ZjQueryDTO dto) {
        Map<String, Object> result = new HashMap<String, Object>();
        CurrentUser currentUser = getCurrentUser();
        //统一认证登录的用户id
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            HashMap<String, Object> qmap = new HashMap<String, Object>();
            qmap.put("userid", userid);
            if (StringUtils.isNotBlank(dto.getZjlsh())) {
                qmap.put("rsrlzyqyxxlsh", dto.getZjlsh());
            }           
        } else {
            throw new AppException("获取不到该用户信息");
        }

        return result;
    }

    @Override
    public RsRlzyQyxxPO saveQyxx(QyxxUDTO dto) {
        RsRlzyQyxxPO po = new RsRlzyQyxxPO();
        BeanUtils.copyProperties(dto, po);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        //公共服务统一登录，个人账户以副组织机构登录，则userid为单位id，operid为操作人个人用户id
        //公共服务统一登录，单位账户直接登录，则userid和operid均为单位id
        //政务内网登录调用，则userid为操作人id
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        String operid = currentUser.getOrgname() == null ? "" : currentUser.getOrgname().trim();

        String qyssr = dto.getQyssr();
        String sftjsh = "0";

        String rsrlzyqyxxlsh = dto.getRsrlzyqyxxlsh();

        if (StringUtils.isNotBlank(qyssr)) {
            po.setTsxgr(operid);//修改人
            po.setTsxgsj(dataString);//修改时间
            //从登录信息里面取出公司名称
            HashMap<String, Object> qmap = new HashMap<String, Object>();
            qmap.put("userid", qyssr);
            Map<String, Object> rmap = qyxxMapper.getSyQyMc(qmap);
            String gsmc = rmap.get("gsmc") == null ? "" : (String) rmap.get("gsmc");
            po.setGsmc(gsmc);
            po.setQyxxshzt("0");//状态置为0，0为未审核，1为审核通过，2为审核不通过
            qyxxMapper.insert(po);
            rsrlzyqyxxlsh =  po.getRsrlzyqyxxlsh();
        } else {
            throw new AppException("获取不到该用户信息");
        }
        return po;
    }

    @Override
    public void uploadQyTx(DwTxSpQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);

        String sftjnwsh = "0";
        String logoshr = "";
        String logoshsj = "";
        String logoshyj = "";
        String logoshzt = "";

        String qyssr = dto.getQyssr();
        if (StringUtils.isBlank(qyssr)){
            qyssr = userid;
        } else {
            sftjnwsh = "1";
            logoshr = userid;
            logoshsj = dataString;
            logoshyj = "审核通过";
            logoshzt = "1";
        }

        if (StringUtils.isNotBlank(qyssr)) {
            RsRlzyQyxxPO po = new RsRlzyQyxxPO();
            po.setLogodz(dto.getTyccid());
            po.setSftjtxsh(sftjnwsh);//头像提交审核置为未提交审核
            po.setLogoshzt(logoshzt);//将头像审核状态置为空，0为审核不通过，1为审核通过
            qyxxMapper.update(po, new UpdateWrapper<RsRlzyQyxxPO>()
                            .eq("rs_yhlsh", qyssr)
//					.eq("rs_rlzy_qyxx_lsh",dto.getRsrlzyqyxxlsh())
            );
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public String queryQyTx(QyTxDTO dto) {
        RsRlzyQyxxPO po = new RsRlzyQyxxPO();
        String qylsh = dto.getZjlsh();
        if (StringUtils.isNotBlank(qylsh)) {
            //小程序调用
            po = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().select("logo_dz")
                    .eq("rs_rlzy_qyxx_lsh", qylsh));
        } 
        return po == null ? "" : (po.getLogodz() == null ? "" : po.getLogodz());
    }

    @Override
    public void uploadQySp(DwTxSpQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            RsRlzyQyxxPO po = new RsRlzyQyxxPO();
            po.setSpdz(dto.getTyccid());
            po.setSftjspsh("0");//视频提交审核置为未提交审核
            po.setSpshzt("");//将视频审核状态置为空，0为审核不通过，1为审核通过
            qyxxMapper.update(po, new UpdateWrapper<RsRlzyQyxxPO>())
//					.eq("rs_rlzy_qyxx_lsh",dto.getRsrlzyqyxxlsh())
            ;
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public String queryQySp() {
        RsRlzyQyxxPO po = new RsRlzyQyxxPO();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            po = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().select("sp_dz"));
        } else {
            throw new AppException("获取不到该用户信息");
        }
        return po == null ? "" : (po.getSpdz() == null ? "" : po.getSpdz());
    }

    @Override
    public RsRlzyQyxxPO toQyxxTjsh(QyxxUDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        String operid = currentUser.getOrgname() == null ? "" : currentUser.getOrgname().trim();
        if (StringUtils.isNotBlank(userid)) {
            //提交审核之前再保存一次
            RsRlzyQyxxPO po = new RsRlzyQyxxPO();
            BeanUtils.copyProperties(dto, po);
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dataString = dateFormat.format(date);
            po.setTsxgr(operid);//修改人
            po.setTsxgsj(dataString);//修改时间

            if (StringUtils.isNotBlank(dto.getRsrlzyqyxxlsh())) {
                //清空审核状态
                po.setQyxxshr("");
                po.setQyxxshsj("");
                //更新提交审核状态
                po.setSftjsh("1");//提交审核置为未提交审核
                qyxxMapper.update(po, new UpdateWrapper<RsRlzyQyxxPO>()
                        .eq("rs_yhlsh", userid));
                return po;
            } else {
                //从基本信息里面取出公司名称
                HashMap<String, Object> qmap = new HashMap<String, Object>();
                qmap.put("userid", userid);
                Map<String, Object> rmap = qyxxMapper.getSyQyMc(qmap);
                String gsmc = rmap.get("gsmc") == null ? "" : (String) rmap.get("gsmc");

                po.setGsmc(gsmc);
                qyxxMapper.insert(po);
                return po;
            }
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public void toQytxTjsh(DwTxSpQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            //保存信息并更新状态
            RsRlzyQyxxPO po = new RsRlzyQyxxPO();
            po.setLogodz(dto.getTyccid());//保存统一存储id
            po.setLogoshzt("0");//将头像审核状态置为0，0为未审核，1为审核通过，2为审核不通过
            qyxxMapper.update(po, new UpdateWrapper<RsRlzyQyxxPO>());
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public void toQyspTjsh(DwTxSpQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            //保存信息并更新状态
            RsRlzyQyxxPO po = new RsRlzyQyxxPO();
            po.setSpdz(dto.getTyccid());
            po.setSftjspsh("1");//视频提交审核置为提交审核
            qyxxMapper.update(po, new UpdateWrapper<RsRlzyQyxxPO>()
                            .eq("rs_yhlsh", userid)
            );
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public PageResult getZphYdList(ZphYdQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            //获取企业流水号
            RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
            String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());
            if (StringUtils.isNotBlank(qyxxlsh)) {
                dto.setRsrlzyqyxxlsh(qyxxlsh);
            } else {
                throw new AppException("请完善您的企业信息！");
            }
        } else {
            throw new AppException("获取不到该用户信息");
        }
        Page<Map<String, Object>> pageResult = null;
        if ("01".equals(dto.getZphjblx())){
            pageResult = (Page) zphYdMapper.getZphYdList(page, dto);
        } else if("02".equals(dto.getZphjblx())){
            pageResult = (Page) zphYdMapper.getZphBmList(page, dto);
        }

        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void ydZph(ZwYdDTO dto) {
        CurrentUser currentUser = getCurrentUser();

        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        String operid = currentUser.getOrgname() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            //获取企业流水号
            RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
            String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());
            if (StringUtils.isNotBlank(qyxxlsh)) {
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dataString = dateFormat.format(date);
                //展位已结束，则不允许预定
                //校验招聘会是否过期
                String zphzt = zphYdMapper.getZphzt(dto.getRsrlzyzphgllsh());
                if ("3".equals(zphzt)) {
                    throw new AppException("该招聘会已过期，不能操作");
                }

                String[] rszwwzlshs = dto.getRszwwzlshs().split(",");

                int cnt = zphYdMapper.selectCount(new QueryWrapper<RsRlzyZwwzyyshPO>().eq("sfyy", "1").eq("sftjnwsh", "1")
                        .and(wrapper -> wrapper.eq("shjg", "0")));
                if (cnt > 0) {
                    throw new AppException("您选中的展位已被其他公司预定，请刷新后重试！");
                } else {
                    //保存之前先逻辑删除之前的数据
                    RsRlzyZwwzyyshPO oldshpo = new RsRlzyZwwzyyshPO();
                    oldshpo.setSfyy("0");
                    RsRlzyZwwzyyzwxxPO oldzwxxpo = new RsRlzyZwwzyyzwxxPO();
                    oldzwxxpo.setRsrlzyzwgllsh(dto.getRsrlzyzwgllsh());
                    rsRlzyZwwzyyzwxxMapper.delete(new QueryWrapper<RsRlzyZwwzyyzwxxPO>()
                            .eq("rs_rlzy_zwgl_lsh", dto.getRsrlzyzwgllsh())
                    );

                    //保存多个展位审核信息
                    List<RsRlzyZwwzyyshPO> shpolist = new ArrayList<RsRlzyZwwzyyshPO>();
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    for (int i = 0; i < rszwwzlshs.length; i++) {
                        String rszwwzlsh = rszwwzlshs[i];
                        RsRlzyZwwzyyshPO po = new RsRlzyZwwzyyshPO();
                        po.setLxfs(dto.getLxfs());
                        po.setSftjnwsh("1");
                        po.setShjg("0");
                        po.setSfyy("1");
                        po.setRsrlzyzwgllsh(dto.getRsrlzyzwgllsh());
                        po.setTscjr(operid);
                        po.setTscjsj(dataString);
                        shpolist.add(po);
                    }
                    //批量保存展位信息
                    zphYdMapper.batchinsertzwsh(shpolist);


                    //保存招聘会的职业
                    String rsrlzyzwxxlshs = dto.getRsrlzyzwxxlshs() == null ? "" : dto.getRsrlzyzwxxlshs();
                    if (StringUtils.isNotBlank(rsrlzyzwxxlshs)) {
                        String[] rsrlzyzwxxlsh = rsrlzyzwxxlshs.split(",");
                        for (int i = 0; i < rsrlzyzwxxlsh.length; i++) {
                            RsRlzyZwwzyyzwxxPO zwpo = new RsRlzyZwwzyyzwxxPO();
                            zwpo.setRsrlzyhcgllsh(dto.getRsrlzyhcgllsh());
                            zwpo.setRsrlzyzwgllsh(dto.getRsrlzyzwgllsh());
                            rsRlzyZwwzyyzwxxMapper.insert(zwpo);
                        }
                    }
                }
            } else {
                throw new AppException("请完善您的企业信息！");
            }
        } else {
            throw new AppException("获取不到该用户信息");
        }

    }

    @Override
    public void bmZph(ZphBmDTO dto) {
        //校验招聘会是否过期
        String zphzt = zphYdMapper.getZphzt(dto.getRsrlzyzphgllsh());
        if ("3".equals(zphzt)) {
            throw new AppException("该招聘会已过期，不能操作");
        }

        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();

        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (StringUtils.isNotBlank(userid)) {
            //获取企业流水号
            RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
            String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());

            //报名操作标志，0为取消报名，1为报名
            String bmbz = dto.getBmbz();

            //查询该招聘会是否已报名，已报名，则该接口为报名，否则为取消报名
            int cnt = rsRlzyZwwzyyzwxxMapper.selectCount(new QueryWrapper<RsRlzyZwwzyyzwxxPO>()
                    .eq("RS_RLZY_ZPHGL_LSH",dto.getRsrlzyzphgllsh()).eq("RS_RLZY_QYXX_LSH",qyxxlsh));

            if ("0".equals(bmbz)){
                if (cnt<=0){
                    throw new AppException("您尚未报名该招聘会！");
                }
                RsRlzyZwwzyyzwxxPO zwpo = new RsRlzyZwwzyyzwxxPO();
                zwpo.setRsrlzyqyxxlsh(qyxxlsh);
                //取消报名
                rsRlzyZwwzyyzwxxMapper.delete(new QueryWrapper<RsRlzyZwwzyyzwxxPO>()
                        .eq("RS_RLZY_ZPHGL_LSH",dto.getRsrlzyzphgllsh()));
            }else if ("1".equals(bmbz)){
                if(cnt>0){
                    throw new AppException("您已报名该招聘会，如需重新报名，请先取消！");
                }
                //报名
                //保存招聘会的职业
                String rsrlzyzwxxlshs = dto.getRsrlzyzwxxlshs() == null ? "" : dto.getRsrlzyzwxxlshs();
                if (StringUtils.isNotBlank(rsrlzyzwxxlshs)) {
                    String[] rsrlzyzwxxlsh = rsrlzyzwxxlshs.split(",");
                    for (int i = 0; i < rsrlzyzwxxlsh.length; i++) {
                        RsRlzyZwwzyyzwxxPO zwpo = new RsRlzyZwwzyyzwxxPO();
                        zwpo.setRsrlzyzwxxlsh(rsrlzyzwxxlsh[i]);
                        rsRlzyZwwzyyzwxxMapper.insert(zwpo);
                    }
                }
            }

        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public void setZphLog(SetZphLogDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        String tsrylx = "03";
        if (StringUtils.isNotBlank(userid)){
            VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(userid);
            String fwdx = vRsGgfwUser.getFwdx();//获取服务对象
            if ("1".equals(fwdx)){
                //个人用户
                tsrylx = "01";
            } else if ("2".equals(fwdx)){
                //单位用户
                tsrylx = "02";
            }
        }
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        RsRlzyZphfwLogPO po = new RsRlzyZphfwLogPO();
        po.setRsrlzyzphgllsh(dto.getRsrlzyzphgllsh());
        po.setTscjsj(df.format(t));
        po.setTsrylx(tsrylx);
        rsRlzyZphfwLogMapper.insert(po);
    }

    @Override
    public void copyZw(CopyZwDTO dto) {
        //复制V_RS_RLZY_ZWXX的数据
        //更新shsj,shjg,shyj,TS_CJR,TS_CJSJ,TS_XGR,TS_XGSJ,SFTJNWSH,FBZT
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        String qyssr = dto.getQyssr();
        String zwlsh = dto.getZjlsh();
        if (StringUtils.isBlank(qyssr)){
            qyssr = userid;
        }
        RsRlzyZwxxPO po = zwxxMapper.selectOne(new QueryWrapper<RsRlzyZwxxPO>().eq("RS_RLZY_ZWXX_LSH",zwlsh));
        if (po==null){
            throw new AppException("请检查要复制的职位是否存在");
        }

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);

        //清空主键
        po.setRsrlzyzwxxlsh("");
        po.setTscjsj(dataString);
        //更改复制职位名称
        String zwmc = "";
        //复制的简历名始终是招聘岗位（数字递增）
        po.setZwmc(zwmc);
        zwxxMapper.insert(po);
    }

    @Override
    public PageResult getWlzphZw(ZwzpfsDTO dto) {
        Page<ZwxxListDTO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<ZwxxListDTO> pageResult = null;
        pageResult = (Page) this.zwxxMapper.getWlzphZw(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void qxZphYd(ZjDTO dto) {
        try {
            CurrentUser currentUser = getCurrentUser();
            String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
            //获取企业流水号
            RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
            String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());
            if (StringUtils.isNotBlank(qyxxlsh)) {
                // 判断是否允许取消
                Map<String, Object> zphxx = zphYdMapper.getZphksrq(dto.getZjlsh());
                String time = MapUtils.getString(zphxx, "jbrqc");
                SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

                //取消该招聘会展位下全部预约的展位,取消预约就无效，下次重新预约，要重新插入数据
                RsRlzyZwwzyyshPO po = new RsRlzyZwwzyyshPO();
                po.setSfyy("0");
                zphYdMapper.update(po, new UpdateWrapper<RsRlzyZwwzyyshPO>()
                        .eq("rs_rlzy_zwgl_lsh", dto.getZjlsh()));
                //查询要更新展位状态（企业下该展位的所有展位状态）
                List<RsRlzyZwwzyyshPO> shlist = zphYdMapper.selectList(new QueryWrapper<RsRlzyZwwzyyshPO>()
                        .eq("rs_rlzy_qyxx_lsh", qyxxlsh));
                if (shlist != null) {
                    List<String> zwwzlshlist = new ArrayList<>();
                    for (int i = 0; i < shlist.size(); i++) {
                        RsRlzyZwwzyyshPO shpo = shlist.get(i);
                        if (StringUtils.isNotBlank(shpo.getRszwwzlsh())) {
                            zwwzlshlist.add(shpo.getRszwwzlsh());
                        }
                    }
                }
                //删除展位下的职业
                RsRlzyZwwzyyzwxxPO oldzwxxpo = new RsRlzyZwwzyyzwxxPO();
                oldzwxxpo.setRsrlzyzwgllsh(dto.getZjlsh());
                rsRlzyZwwzyyzwxxMapper.delete(new QueryWrapper<RsRlzyZwwzyyzwxxPO>()
                        .eq("rs_rlzy_zwgl_lsh", dto.getZjlsh())
                );

                // 发送机构端信息
                String bae002 = MapUtils.getString(zphxx, "bae002");
                String zphmc = MapUtils.getString(zphxx, "zphmc");
                List<String> sjrids = new ArrayList<>();
                sjrids.add(bae002);

                SendInformationDTO infoDto = new SendInformationDTO();
                infoDto.setFjrid(userid);
                informationBLO.sendInfo(infoDto);
            } else {
                throw new AppException("请完善您的企业信息！");
            }
        } catch (Exception e) {
            throw new AppException(e.getMessage());
        }
    }

    public static Date DateBefAft(Date date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 给定时间2天前
        calendar.add(Calendar.DAY_OF_YEAR, -2);
        Date c = calendar.getTime();
        return c;
    }

    @Override
    public PageResult getRcssqList(PageDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            QueryWrapper<RsRlzyRcssqPO> queryWrapper = new QueryWrapper<RsRlzyRcssqPO>();
            //获取企业流水号
            RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
            String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());
            if (StringUtils.isNotBlank(qyxxlsh)) {
                Page<RsRlzyRcssqPO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
                Page<RsRlzyRcssqPO> pageResult = (Page) this.rsRlzyRcssqMapper.selectPage(page, queryWrapper);
                PageResult result = PageUtil.PageResult(pageResult);
                return result;
            } else {
                throw new AppException("请完善您的企业信息！");
            }
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public RsRlzyRcssqPO saveRcssq(RcssqUDTO dto) {
        RsRlzyRcssqPO po = new RsRlzyRcssqPO();
        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.getUsername() == null ? "" : currentUser.getUsername().trim();
        String operid = currentUser.getOrgname() == null ? "" : currentUser.getOrgname().trim();
        //获取企业流水号
        RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
        String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());
        if (StringUtils.isNotBlank(userid)) {
            
            if (StringUtils.isNotBlank(dto.getRsrlzyrcssqlsh())) {
                rsRlzyRcssqMapper.updateById(po);
            } else {
                //新增
                //7、最多可创建10条人才搜索器。
                int cnt = rsRlzyRcssqMapper.selectCount(new QueryWrapper<RsRlzyRcssqPO>()
                        .eq("ts_yxbz", "1"));
                if (cnt > 10) {
                    throw new AppException("最多可创建10条人才搜索器！");
                } else {
                    po.setTscjr(operid);//创建人
                    po.setTscjsj(dataString);//创建时间
                    rsRlzyRcssqMapper.insert(po);
                }
            }
        } else {
            throw new AppException("获取不到该用户信息");
        }
        return po;
    }

    @Override
    public void deleteRcssq(String id) {
        RsRlzyRcssqPO po = new RsRlzyRcssqPO();
        po.setRsrlzyrcssqlsh(id);
        po.setTsyxbz("0");
        rsRlzyRcssqMapper.updateById(po);
    }

    @Override
    public void updateZnts(ZjUDTO dto) {
        RsRlzyRcssqPO po = new RsRlzyRcssqPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        po.setDsqzxsj(dataString);
        po.setRsrlzyrcssqlsh(dto.getZjlsh());
        rsRlzyRcssqMapper.updateById(po);
    }

    @Override
    public PageResult getRcxxList(RcxxcxQueryDTO dto) {
        //需求暂未确定
        return null;
    }

    @Override
    public Map getZwXx(ZjDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getOrgname() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            //获取企业流水号
            RsRlzyQyxxPO qyxxpo = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().eq("rs_yhlsh", userid));
            String qyxxlsh = qyxxpo == null ? "" : (qyxxpo.getRsrlzyqyxxlsh() == null ? "" : qyxxpo.getRsrlzyqyxxlsh());
            if (StringUtils.isNotBlank(qyxxlsh)) {
                Map<String, Object> res = new HashMap<String, Object>();
                res.put("dwmc", qyxxpo.getGsmc());

                List<List<Map<String, Object>>> zwlist = new ArrayList<List<Map<String, Object>>>();
                //再查询展位位置管理数据
                List<String> hshlist = zphYdMapper.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 = zphYdMapper.getZwList(qmap);
                    if (zwhshlist != null) {
                        zwlist.add(zwhshlist);
                    }
                }
                if (zwlist != null) {
                    res.put("zwlist", zwlist);
                }
                return res;
            } else {
                throw new AppException("获取不到该企业信息");
            }
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public PageResult getzyxxList(PageDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
            Page<Map<String, Object>> pageResult = (Page) zphYdMapper.getzyxxList(page, userid);
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public PageResult getWlzphzyxxList(PageDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
            Page<Map<String, Object>> pageResult = (Page) zphYdMapper.getWlzphzyxxList(page, userid);
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        } else {
            throw new AppException("您尚未登录，请登录后进行操作！");
        }
    }

    @Override
    public void toYdSh(ZjDTO dto) {
        //先保存数据

        RsRlzyZwwzyyshPO po = new RsRlzyZwwzyyshPO();
        po.setSftjnwsh("1");
        po.setShjg("0");
        po.setRsrlzyzwwzyyshlsh(dto.getZjlsh());
        zphYdMapper.updateById(po);
    }

    @Override
    public Map getQytjxx() {
        HashMap res = new HashMap();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        HashMap qmap = new HashMap();
        qmap.put("userid", userid);
        Map ydzphsmap = qyxxMapper.getYdzphs(qmap);
        if (ydzphsmap != null) {
            res.put("ydzphs", ydzphsmap.get("ydzphs") == null ? "" : ydzphsmap.get("ydzphs"));
        }
        return res;
    }

    @Override
    public RsRlzyRcssqPO getRcssqXx(ZjDTO dto) {
        RsRlzyRcssqPO po = rsRlzyRcssqMapper.selectOne(new QueryWrapper<RsRlzyRcssqPO>().eq("rs_rlzy_rcssq_lsh", dto.getZjlsh()));
        return po;
    }

    @Override
    public PageResult getJlzntjList(PageDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
            Page<Map<String, Object>> pageResult = (Page) jlxxMapper.getJlzntjList(page, userid);
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public PageResult getJlrgtjList(PageDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {
            Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
            Page<Map<String, Object>> pageResult = (Page) jlxxMapper.getJlrgtjList(page, userid);
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        } else {
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public RsRlzyQyxxPO getQyxxDetail(String id) {
        if (StringUtils.isNotBlank(id)) {
            QueryWrapper<RsRlzyQyxxPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("V_RS_RLZY_QYXX.*", "f_jbxxgl_getAllAreaName(V_RS_RLZY_QYXX.aab078)  as \"aab078name\"").eq("rs_rlzy_qyxx_lsh", id);
            RsRlzyQyxxPO po =  this.qyxxMapper.selectOne(queryWrapper);

            // 20211008 安全问题处理, 清空个别信息
            po.setAae004("");
            po.setAab014("");
            return po;
        } else {
            throw new AppException("企业信息流水号不能为空");
        }

    }

    @Override
    public PageResult getZwzpfs(ZwzpfsDTO dto) {
        Page<ZwxxListDTO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<ZwxxListDTO> pageResult = null;
        pageResult = (Page) this.zwxxMapper.getZwzpfs(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
}



