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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
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 com.zjrs.ggfw.portal.blo.CommBLO;
import com.zjrs.ggfw.portal.dto.*;
import com.zjrs.ggfw.portal.entity.*;
import com.zjrs.ggfw.portal.po.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CommBLOImpl extends BLOImpl implements CommBLO {
	
	@Resource
	VGgfwDictMapper vGgfwDictMapper;
	
	@Resource
	VRsSyXxlxMapper vRsSyXxlxMapper;
	
	@Resource
	VRsSyXxlxFjMapper vRsSyXxlxFjMapper;
	
	@Resource
	VRsSxConfigMapper vRsSxConfigMapper;
	
	@Resource
	VRsWbRmsxMapper vRsWbRmsxMapper;
	
	@Resource
	VSgAreaMapper vSgAreaMapper;
	
	@Resource
	VRsWbRmfwMapper vRsWbRmfwMapper;
	
	@Resource
	VRsZjGccrcQfMapper vRsZjGccrcQfMapper;
	
	@Resource
	VRsZjGccrcMapper vRsZjGccrcMapper;
	
	@Resource
	VZjldXsdaMapper vZjldXsdaMapper;
	
	@Resource
	VZjldRsdldaMapper vZjldRsdldaMapper;

	@Resource
	RsCommMapper rsCommMapper;

	@Resource
	RsWbBjSubMapper rsWbBjSubMapper;

	@Override
	@Cacheable(value="ggfw-dict", key="#kind")
	public List<VGgfwDict> getGgfwDict(String kind){
		QueryWrapper<VGgfwDict> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("aaa100", kind);
		return vGgfwDictMapper.selectList(queryWrapper);
	}
	
	@Override
	@Cacheable(value="ggfw-xzqh", key="#parentId")
	public List<VSgArea> getXzqh(String parentId){
		QueryWrapper<VSgArea> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_valid", "1");//是否有效
		return vSgAreaMapper.selectList(queryWrapper);
	}
	
	@Override
	@Cacheable(value="ggfw-sshy", key="#parentId")
	public List<VGgfwDict> getSshy(String parentId){
		QueryWrapper<VGgfwDict> queryWrapper = new QueryWrapper<>();
    	queryWrapper.orderByAsc("aaa102");
		return vGgfwDictMapper.selectList(queryWrapper);
	}
	
	@Override
	public PageResult getSxList(SxQueryDTO dto) {
        QueryWrapper<VRsSxConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("yxbz", "1");
        queryWrapper.eq("fbzt", "1");//发布状态
        //事项名称
        if(StringUtils.isNotBlank(dto.getSxmc())) {
    		queryWrapper.like("sxmc", dto.getSxmc());
    	}
        //服务对象
        if(StringUtils.isNotBlank(dto.getFwdx())) {
    		queryWrapper.like("fwdx", dto.getFwdx());
    	}
        //到现场次数
        if(StringUtils.isNotBlank(dto.getDxccs())) {
        	Object[] dxccs = dto.getDxccs().split(",");
    		queryWrapper.in("dxccs", dxccs);
    	}
        //办理类型
        if(StringUtils.isNotBlank(dto.getBllx())) {
        	Object[] bllx = dto.getBllx().split(",");
    		queryWrapper.in("bllx", bllx);
    	}
        //一件事
		if(StringUtils.isNotBlank((dto.getIsyjs()))) {
			queryWrapper.isNotNull("GLSX");
		}
        Page<VRsSxConfig> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<VRsSxConfig> pageResult = (Page) this.vRsSxConfigMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
	}
	
	@Override
	public List<Map<String, Object>> getZcfgYwztCntList(){
		return vRsSyXxlxMapper.getZcfgYwztCntList();
	}
	
	@Override
	public PageResult getZcfgList(ZcfgQueryDTO dto){
		QueryWrapper<VRsSyXxlx> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("xxlx", dto.getXxlx());
        
    	//标题
    	if(StringUtils.isNotBlank(dto.getBt())) {
    		queryWrapper.like("bt", dto.getBt());
    	}
    	queryWrapper.orderByDesc("zdbz","fbsj");
    	
    	Page<VRsSyXxlx> page = new Page<>(dto.getPagenum(), dto.getPagesize());
    	Page<VRsSyXxlx> pageResult = (Page) this.vRsSyXxlxMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
		return result;
	}
	
	@Override
	public PageResult getZcfgFjList(ZcfgfjQueryDTO dto){
		QueryWrapper<VRsSyXxlxFj> queryWrapper = new QueryWrapper<>();
    	queryWrapper.orderByDesc("bae003");
    	
    	Page<VRsSyXxlxFj> page = new Page<>(dto.getPagenum(), dto.getPagesize());
    	Page<VRsSyXxlxFj> pageResult = (Page) this.vRsSyXxlxFjMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
		return result;
	}
	
	@Override
	public List<VRsSyXxlx> getLatestZcfg(ZcfgQueryDTO dto){
		QueryWrapper<VRsSyXxlx> queryWrapper = new QueryWrapper<>();
    	queryWrapper.in("xxlx", "1", "3");//政策法规、通知公告
    	queryWrapper.orderByDesc("zdbz","fbsj");
    	List<VRsSyXxlx> zcfgList = vRsSyXxlxMapper.selectList(queryWrapper);
    	if(zcfgList != null && zcfgList.size() > 6) {
    		return zcfgList.subList(0, 6);
    	}else {
    		return zcfgList;
    	}
	}
	
	@Override
	@Cacheable(value="ggfw-rmsx", key="#ssbm")
	public List<VRsWbRmsx> getRmSxList(String ssbm){
		QueryWrapper<VRsWbRmsx> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ssbm", ssbm);//实施部门
		return vRsWbRmsxMapper.selectList(queryWrapper);
	}
	
	@Override
	@Cacheable(value="ggfw-rmfw")
	public List<VRsWbRmfw> getRmFwList(){
		QueryWrapper<VRsWbRmfw> queryWrapper = new QueryWrapper<>();
		return vRsWbRmfwMapper.selectList(queryWrapper);
	}
	
	@Override
	public VRsZjGccrcQf getGccrck(GccrcQueryDTO dto) {
		QueryWrapper<VRsZjGccrcQf> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("aac058", dto.getZjlx());
    	queryWrapper.eq("aac147", dto.getZjhm());
    	List<VRsZjGccrcQf> zjGccrcQfList = vRsZjGccrcQfMapper.selectList(queryWrapper);
    	if(zjGccrcQfList != null && zjGccrcQfList.size() > 0) {
    		VRsZjGccrcQf vRsZjGccrcQf = zjGccrcQfList.get(0);
			//校验人才卡状态是否正常
			VRsZjGccrc vRsZjGccrc = vRsZjGccrcMapper.selectById(vRsZjGccrcQf.getRsZjGccrcLsh());
    		if("1".equals(vRsZjGccrc.getRckzt())) {//非正常状态无法查询到人才卡
    			return vRsZjGccrcQf;
    		}
    	}
    	return null;
	}
	
	@Override
	public PageResult getGxbysXsda(GxbysdaQueryDTO dto) {
		QueryWrapper<VZjldXsda> queryWrapper = new QueryWrapper<>();
		Page<VZjldXsda> page = new Page<>(dto.getPagenum(), dto.getPagesize());
		Page<VZjldXsda> pageResult = null;
		if("1".equals(dto.getIsUseSfzh())) {
			//仅用身份证号查询
			queryWrapper.eq("aac147",dto.getAac147()).eq("bac015",dto.getDatype());
			queryWrapper.orderByDesc("bae003");
		}else {
			queryWrapper.orderByDesc("bae003");
			pageResult = (Page) this.vZjldXsdaMapper.selectPage(page, queryWrapper);
			PageResult result = PageUtil.PageResult(pageResult);
				return result;
		}
		pageResult = (Page) this.vZjldXsdaMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
	}
	
	@Override
	public PageResult getGxbysRsdlda(GxbysdaQueryDTO dto) {
		QueryWrapper<VZjldRsdlda> queryWrapper = new QueryWrapper<>();
		Page<VZjldRsdlda> page = new Page<>(dto.getPagenum(), dto.getPagesize());
		Page<VZjldRsdlda> pageResult = null;
		if("1".equals(dto.getIsUseSfzh())) {
			//仅用身份证号查询
			queryWrapper.eq("aac147", dto.getAac147());
			queryWrapper.orderByDesc("bae003");
		}else {
			queryWrapper.orderByDesc("bae003");
			pageResult = (Page) this.vZjldRsdldaMapper.selectPage(page,queryWrapper);
			if (pageResult.getRecords().size() == 0){ //无记录，再按姓名+毕业院校前4个字去模糊查询
				queryWrapper = new QueryWrapper<>();
			}else{
				PageResult result = PageUtil.PageResult(pageResult);
				return result;
			}
		}
        pageResult = (Page) this.vZjldRsdldaMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
	}

	//获取当前时间
	@Override
	public String getCurrenttime(){
		return this.rsCommMapper.getCurrenttime();
	}

	@Override
	public Map<String, String> getSubBjXx(HcpDTO dto) {
		return rsWbBjSubMapper.getSubBjXx(dto);
	}
}