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 org.mohrss.leaf.core.framework.util.CurrentUser;
import com.zjrs.ggfw.portal.blo.UserBLO;
import com.zjrs.ggfw.portal.po.VRsGgfwUser;
import com.zjrs.ggfw.rlzysc.blo.SyBLO;
import com.zjrs.ggfw.rlzysc.dto.JyzxQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.SyZwxxQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.ZphglQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.ZwxxListDTO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.SyMapQyQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.ZjDTO;
import com.zjrs.ggfw.rlzysc.entity.*;
import com.zjrs.ggfw.rlzysc.po.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

@Service
public class SyBLOImpl implements SyBLO {
    @Resource
    private LbggMapper lbggMapper;
    @Resource
    private ZyxxMapper zyxxMapper;
    @Resource
    private ZwxxMapper zwxxMapper;
    @Resource
    private QytjMapper qytjMapper;
    @Resource
    private FwggMapper fwggMapper;
    @Resource
    private ZphglMapper zphglMapper;
    @Resource
    UserBLO userBLO;
    @Resource
    JlxxMapper jlxxMapper;
    @Resource
    private QyxxMapper qyxxMapper;
    @Override
    public  List<RsRlzyLbggPO> getLbggList() {
        QueryWrapper<RsRlzyLbggPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("RS_RLZY_LBGG_LSH","RS_RLZY_LBGG_MC","RS_RLZY_LBGG_BZ", "RS_RLZY_LBGG_FWDZ", "RS_TYCC_LSH","BAE003","BAE002","BAE004","BAE005");
        queryWrapper.eq("TS_YXBZ","1");
        queryWrapper.eq("ZDZT","1");
        queryWrapper.orderByDesc("ZDSJ");
        return this.lbggMapper.selectList(queryWrapper);
    }

    @Override
    @Cacheable(value = "rlzy-zyxxlist")
    public List getZyxxList() {
        List<RsRlzyZyxxPO> zyxxList1 = this.zyxxMapper.getZyxxList(); //一级分类
        List<RsRlzyZyxxPO> zyxxList2 = this.zyxxMapper.getZyxxList2(); //二级分类
        List<Map<String, Object>> zwxx = new ArrayList();
        for (int i = 0;i<zyxxList1.size();i++) {
            List<RsRlzyZyxxPO> list = new ArrayList();
            Map<String,Object> map = new HashMap<>();
            map.put("rsrlzyzyxxbm",zyxxList1.get(i).getRsrlzyzyxxbm());
            for (int j = 0;j<zyxxList2.size();j++){
                if (zyxxList2.get(j).getParentrsrlzyzyxxlsh().equals(zyxxList1.get(i).getRsrlzyzyxxbm())){
                    list.add(zyxxList2.get(j));
                    map.put("children",list); 
                }
            }
            zwxx.add(map);
        }
        return zwxx;
    }

    @Override
    public List getZyxxList(String id) {
        QueryWrapper<RsRlzyZyxxPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("RS_RLZY_ZYXX_DJ", "3");
        List<RsRlzyZyxxPO> list = zyxxMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<ZwxxListDTO> getZwtjList(SyZwxxQueryDTO dto) {
        List<ZwxxListDTO> list = null;
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)) {//用户登录情况下
            VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(userid);
            String fwdx = vRsGgfwUser.getFwdx();//获取服务对象
            if ("1".equals(fwdx)) {//个人用户
                JlxxPO mrjl = jlxxMapper.getMrjllsh(userid); //查看当前用户上是否设置了默认简历
                if (null == mrjl){//如果没设置责按未登录情况下推荐职位
                    list = this.zwxxMapper.getNoLoginZwtj(dto);
                }
            }
            if ("2".equals(fwdx)) {//单位用户登录和未登录的情况下推荐的规则是一样的
                if (dto.getPxfs().equals("1")) { //1为最新职位
                    list = this.zwxxMapper.getNoLoginZwtj(dto);
                }
            }
        } else {//未登录情况下
            if (dto.getPxfs().equals("1")) { //1为最新职位
                list = this.zwxxMapper.getNoLoginZwtj(dto);
            }
        }
        return list;
    }

    @Override
    public List getQytjList() {
        List<RsRlzyQyxxPO> qyxxlist = this.qytjMapper.getQytjList(); //企业信息
        List<RsRlzyZwxxPO> zwxxList = this.qytjMapper.getZwxxList(); //职位信息
        List<Map<String, Object>> zwxx = new ArrayList();
        for (int i = 0;i<qyxxlist.size();i++) {
            List<RsRlzyZwxxPO> list = new ArrayList();
            Map<String,Object> map = new HashMap<>();
            map.put("zczb",qyxxlist.get(i).getZczb());
            for (int j = 0;j<zwxxList.size();j++){
                if (qyxxlist.get(i).getRsrlzyqyxxlsh().equals(zwxxList.get(j).getRsrlzyqyxxlsh())){
                    list.add(zwxxList.get(j));
                    map.put("zwxx",list);
                }
            }
            zwxx.add(map);
        }
        return zwxx;
    }

    @Override
    public PageResult getFwggList(JyzxQueryDTO dto,int page,int rows) {
        QueryWrapper<RsRlzyFwggPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ts_yxbz","1");//1为有效
        queryWrapper.orderByDesc("bae005");
        Page<RsRlzyFwggPO> page1 = new Page<>(page, rows);
        Page<RsRlzyFwggPO> pageResult = (Page) this.fwggMapper.selectPage(page1,queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getXyzpxxList(int page,int rows) {
        QueryWrapper<RsRlzyXczphPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("BAE003");//按时间降序
        Page<RsRlzyXczphPO> page1 = new Page<>(page, rows);
        Page<RsRlzyXczphPO> pageResult = (Page) this.zphglMapper.selectPage(page1,queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getZxscZphxx(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 List<Map<String, Object>> getZphCalendarList() {
        return this.zphglMapper.getZphCalendarList();
    }

    @Override
    public List<Map<String,Object>> getMapQy(ZjDTO dto) {
        return qyxxMapper.getMapQy(dto);
    }

    @Override
    public List<Map<String, Object>> getMapZw(SyMapQyQueryDTO dto) {

        // 湛江市默认查询所有
        if(StringUtils.equals("01", dto.getZpdq()))
            dto.setZpdq("");

        return qyxxMapper.getMapZw(dto);
    }
}



