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

import jakarta.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.portal.blo.CommBLO;
import com.zjrs.ggfw.portal.bpo.CommBPO;
import com.zjrs.ggfw.portal.dto.*;
import com.zjrs.ggfw.portal.po.*;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommBPOImpl extends BPOImpl implements CommBPO {
	
	@Resource
    private CommBLO commBLO;
	
	@Override
	public Map<String, Object> getDict(DictQueryDTO dto){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for(int i=0;i<dto.getDictArr().length;i++) {
			List<DictDTO> dicts = new ArrayList<DictDTO>();
			List<VGgfwDict> dictList = commBLO.getGgfwDict((String) dto.getDictArr()[i]);
			for(VGgfwDict vGgfwDict : dictList) {
				DictDTO dictDTO = new DictDTO();
				dictDTO.setCode(vGgfwDict.getAaa102());
				dictDTO.setDetail(vGgfwDict.getAaa103());
				dicts.add(dictDTO);
			}
			//写入字典
			resultMap.put((String) dto.getDictArr()[i], dicts);
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> getZjXzqhDict(){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<DictDTO> dicts = new ArrayList<DictDTO>();
		//查询湛江行政区划
		List<VSgArea> xzqhList = commBLO.getXzqh("440800");
		for(VSgArea vSgArea : xzqhList) {
			DictDTO dictDTO = new DictDTO();
			dictDTO.setCode(vSgArea.getAreaCode());
			dictDTO.setDetail(vSgArea.getAreaName());
			dicts.add(dictDTO);
		}
		resultMap.put("XZQH", dicts);
		return resultMap;
	}
	
	@Override
	public AjaxResponse getXzqhDict(XzqhQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		List<XzqhDTO> dicts = new ArrayList<XzqhDTO>();
		//查询湛江行政区划
		List<VSgArea> xzqhList = commBLO.getXzqh(dto.getParentId());
		for(VSgArea vSgArea : xzqhList) {
			XzqhDTO xzqhDTO = new XzqhDTO();
			xzqhDTO.setValue(vSgArea.getAreaCode());
			xzqhDTO.setLabel(vSgArea.getAreaName());
			if("0".equals(vSgArea.getIsleaf())) {
				xzqhDTO.setIsLeaf(true);
			}else {
				xzqhDTO.setIsLeaf(false);
			}
			//广东省放前面
			if("86".equals(dto.getParentId()) && vSgArea.getAreaCode().toString().indexOf("44")>=0) {
				dicts.add(0, xzqhDTO);
			}else {
				dicts.add(xzqhDTO);
			}
		}
		ajaxResponse.setData("data", dicts);
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getSshy(SshyQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		List<SshyDTO> sshyList = new ArrayList<SshyDTO>();
		
		List<VGgfwDict> dictList = commBLO.getSshy(dto.getParentId());
		for(VGgfwDict vGgfwDict : dictList) {
			SshyDTO sshyDTO = new SshyDTO();
			sshyDTO.setId(vGgfwDict.getAaa102());
			sshyDTO.setpId(dto.getParentId());
			sshyDTO.setValue(vGgfwDict.getAaa102());
			sshyDTO.setTitle(vGgfwDict.getAaa103());
			if("0".equals(dto.getParentId())) {
				sshyDTO.setIsLeaf(false);
			}else {
				sshyDTO.setIsLeaf(true);
			}
			sshyList.add(sshyDTO);
		}
		
		ajaxResponse.setData("data", sshyList);
		return ajaxResponse;
	}
	
	@Override
	public PageResult getSxList(SxQueryDTO dto) {
		return commBLO.getSxList(dto);
	}
	
	@Override
	public AjaxResponse getZcfgYwztCntList() {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		List<Map<String, Object>> zcfgYwztCntList = commBLO.getZcfgYwztCntList();
		ajaxResponse.setData("data", zcfgYwztCntList);
		
		return ajaxResponse;
	}
	
	@Override
	public PageResult getZcfgList(ZcfgQueryDTO dto) {
		return commBLO.getZcfgList(dto);
	}
	
	@Override
	public PageResult getZcfgFjList(ZcfgfjQueryDTO dto) {
		return commBLO.getZcfgFjList(dto);
	}
	
	@Override
	public List<VRsWbRmsx> getRmSxList(RmsxQueryDTO dto){
		return commBLO.getRmSxList(dto.getSsbm());
	}
	
	@Override
	public List<VRsWbRmfw> getRmFwList(){
		return commBLO.getRmFwList();
	}
	
	@Override
	public AjaxResponse getGccrck(GccrcQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsZjGccrcQf vRsZjGccrcQf = commBLO.getGccrck(dto);
		if(vRsZjGccrcQf == null) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("未查询到有效的人才卡信息！");
		}else {
			ajaxResponse.setData("data", vRsZjGccrcQf);
		}
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getGxbysXsda(GxbysdaQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//若不使用身份证号查询，则姓名与毕业院校必录
		if(!"1".equals(dto.getIsUseSfzh())) {
			if(StringUtils.isBlank(dto.getAac003()) || StringUtils.isBlank(dto.getBccp43())) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setMsg("姓名、毕业院校不能为空！");
				return ajaxResponse;
			}
		}
		PageResult pageResult = commBLO.getGxbysXsda(dto);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", dto.getPagesize());
        ajaxResponse.setData("pagenum", dto.getPagenum());
        return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getGxbysRsdlda(GxbysdaQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//若不使用身份证号查询，则姓名与毕业院校必录
		if(!"1".equals(dto.getIsUseSfzh())) {
			if(StringUtils.isBlank(dto.getAac003()) || StringUtils.isBlank(dto.getBccp43())) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setMsg("姓名、毕业院校不能为空！");
				return ajaxResponse;
			}
		}
		PageResult pageResult = commBLO.getGxbysRsdlda(dto);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", dto.getPagesize());
        ajaxResponse.setData("pagenum", dto.getPagenum());
        return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getLatestZcfg(ZcfgQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		List<VRsSyXxlx> zcfgList = commBLO.getLatestZcfg(dto);
		ajaxResponse.setData("data", zcfgList);
		
		return ajaxResponse;
	}

	@Override
	public AjaxResponse checkRequestParameter(BindingResult bindingResult){
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setAppcode("1");
		List<FieldError> fieldErrors = bindingResult.getFieldErrors();
		String errmsg = "";
		for (int i=0;i<fieldErrors.size();i++){
			if (i == 0){
				errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
			} else {
				errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
			}
		}
		ajaxResponse.setErrorMsg(errmsg);
		return ajaxResponse;
	}
}