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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.ggfw.rlzysc.blo.ZphglBLO;
import com.zjrs.ggfw.rlzysc.dto.JrzphdwListDTO;
import com.zjrs.ggfw.rlzysc.dto.QtzphQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.ZphglQueryDTO;
import com.zjrs.ggfw.rlzysc.entity.*;
import com.zjrs.ggfw.rlzysc.po.RsRlzyXczphPO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyZphfwLogPO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyZwxxPO;
import org.springframework.stereotype.Service;

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

@Service
public class ZphglBLOImpl implements ZphglBLO {
    @Resource
    private ZphglMapper zphglMapper;

    @Resource
    private ZwxxMapper zwxxMapper;
    @Resource
    private JrzphdwxxMapper jrzphdwxxMapper;

    @Resource
    RsRlzyZphfwLogMapper rsRlzyZphfwLogMapper;


    //获取招聘会列表
    @Override
    public PageResult getZphglList(ZphglQueryDTO dto, int page, int rows) {
        Page<RsRlzyXczphPO> page1 = new Page<>(page, rows);
        Page<RsRlzyXczphPO> pageResult = null;
        Calendar calendar2 = Calendar.getInstance();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isNotBlank(dto.getSj())) {
            if (dto.getSj().equals("3")) {
                String currentTime = sdf2.format(calendar2.getTime()); //当前日期
                calendar2.add(Calendar.DATE, 3);
                String three_days_after = sdf2.format(calendar2.getTime());//三天后的日期
                pageResult = (Page) this.zphglMapper.getZphglList(page1, currentTime, three_days_after, dto.getKeyWord(), dto.getDq(), dto.getSj(), dto.getZphlx(), dto.getZphjblx());
            }
        } else {
            pageResult = (Page) this.zphglMapper.getZphglList(page1, dto.getKeyWord(), dto.getDq(), dto.getSj(), dto.getZphlx(), dto.getZphjblx());
        }
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    //获取招聘会详细信息
    @Override
    public RsRlzyXczphPO getZphxxById(QtzphQueryDTO dto) {
        return this.zphglMapper.getZphDetail(dto.getRsrlzyzphgllsh());
    }


    @Override
    public List<RsRlzyXczphPO> getQtzphList(QtzphQueryDTO dto) {
        List<RsRlzyXczphPO> qtzphList = this.zphglMapper.getQtzphList(dto);
        return qtzphList;
    }

    //获取加入招聘会单位信息
    @Override
    public List getQyxxList(QtzphQueryDTO dto) {
        List<JrzphdwListDTO> qyxxList = this.jrzphdwxxMapper.getQyxxList(dto);
        List<RsRlzyZwxxPO> zwxxList = this.jrzphdwxxMapper.getZzzwxxList(dto.getRsrlzyzphgllsh());
        List<Map<String, Object>> zwxx = new ArrayList();
        for (int i = 0; i < qyxxList.size(); i++) {
            List<Map<String, Object>> list = new ArrayList();
            Map<String, Object> map = new HashMap<>();
            map.put("gsmc", qyxxList.get(i).getAab004());
            for (int j = 0; j < zwxxList.size(); j++) {
                if (qyxxList.get(i).getRsrlzyqyxxlsh().equals(zwxxList.get(j).getRsrlzyqyxxlsh())) {
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("zwmc", zwxxList.get(j).getZwmc());
                    list.add(map2);
                    map.put("zwxx", list);
                }
            }
            zwxx.add(map);
        }
        return zwxx;
    }

    @Override
    public List getZxjrdZphList() {
        List<JrzphdwListDTO> qyxxList = this.jrzphdwxxMapper.getJrzphdwxx();
        List<RsRlzyZwxxPO> zwxxList = this.jrzphdwxxMapper.getZwxxList();
        List<Map<String, Object>> dwxxlist = new ArrayList();
        for (int i = 0; i < qyxxList.size(); i++) {
            List<Map<String, Object>> list = new ArrayList();
            Map<String, Object> map = new HashMap<>();
            map.put("jrsj", qyxxList.get(i).getJrsj());
            map.put("gsmc", qyxxList.get(i).getAab004());
            for (int j = 0; j < zwxxList.size(); j++) {
                if (qyxxList.get(i).getRsrlzyqyxxlsh().equals(zwxxList.get(j).getRsrlzyqyxxlsh())) {
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("zwmc", zwxxList.get(j).getZwmc());
                    list.add(map2);
                    map.put("zwxx", list);
                }
            }
            if (dwxxlist.size() > 12) {
                return dwxxlist.subList(0, 12);
            } else {
                dwxxlist.add(map);
            }
        }
        return dwxxlist;
    }

    @Override
    public Map getQyzW(String rsrlzyzphgllsh) {
        return zphglMapper.getgetQyzW(rsrlzyzphgllsh);
    }

    @Override
    public int getLll(String rsrlzyzphgllsh) {
        return rsRlzyZphfwLogMapper.selectCount(new QueryWrapper<RsRlzyZphfwLogPO>()
                .eq("rs_rlzy_zphgl", rsrlzyzphgllsh));
    }

    @Override
    public List<Map<String, Object>> getQyList(String rsrlzyzphgllsh) {
        return zphglMapper.getQyList(rsrlzyzphgllsh);
    }

    @Override
    public List getChdwQcDetail(List<Map<String, Object>> qylist) {
        List list = new ArrayList();
        List qcqylshlist = new ArrayList();
        ArrayList zpfs = new ArrayList();
        zpfs.add("0");
        zpfs.add("2");
        for (int i = 0; i < qylist.size(); i++) {
            Map map = qylist.get(i);
            String rsrlzyqyxxlsh = (String) map.get("rsrlzyqyxxlsh");
            if (qcqylshlist.contains(rsrlzyqyxxlsh)){
                continue;
            } else {
                List<RsRlzyZwxxPO> po = zwxxMapper.selectList(new QueryWrapper<RsRlzyZwxxPO>()
                        .select("zwmc")
                        .eq("fbzt","1"));
                map.put("zwxx",po);
                map.put("zwcnt",po.size());
                list.add(map);
                qcqylshlist.add(rsrlzyqyxxlsh);
            }
        }
        return list;
    }

    @Override
    public int getKydxw(QtzphQueryDTO dto) {
        Map<String,Object> resMap =  zphglMapper.getKydxw(dto);
        if (null == resMap) {
            return 0;
        }
        return Integer.valueOf((String) resMap.get("kyd"));
    }

    @Override
    public List getChdwDetail(List<Map<String, Object>> qylist, String rsrlzyzphgllsh) {
        List list = new ArrayList();
        for (int i = 0; i < qylist.size(); i++) {
            Map<String, Object> map = qylist.get(i);
            map.put("rsrlzyzphgllsh", rsrlzyzphgllsh);
            List<RsRlzyZwxxPO> po = this.zphglMapper.getChdwDetail(map);
//            List<RsRlzyZwxxPO> po = zwxxMapper.selectList(new QueryWrapper<RsRlzyZwxxPO>()
//                    .select("zwmc")
//                    .eq("rs_rlzy_qyxx_lsh",rsrlzyqyxxlsh)
//                    .eq("fbzt","1").eq("ts_yxbz","1"));
            map.put("zwxx",po);
            map.put("zwcnt",po.size());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getZxQyList() {
        return zphglMapper.getZxQyList();
    }


}



