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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import com.zjrs.zwnw.jbxxgl.dto.AreaDTO;
import com.zjrs.zwnw.jbxxgl.entity.VAb01Mapper;
import com.zjrs.zwnw.jbxxgl.entity.VAc01Mapper;
import com.zjrs.zwnw.jbxxgl.po.VSAb01PO;
import com.zjrs.zwnw.jbxxgl.po.VSAc01PO;
import com.zjrs.zwnw.portal.dto.common.CommonDictDTO;
import com.zjrs.zwnw.rlzysc.blo.RlzyScCommonBLO;
import com.zjrs.zwnw.rlzysc.entity.RsGgfwUserMapper;
import com.zjrs.zwnw.rlzysc.entity.VGgfwDictMapper;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyZyxxMapper;
import com.zjrs.zwnw.rlzysc.po.RsGgfwUserPO;
import com.zjrs.zwnw.rlzysc.po.VGgfwDict;
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;

@Service
public class RlzyScCommonBLOimpl extends BLOImpl implements RlzyScCommonBLO {

    @Resource
    private VRsRlzyZyxxMapper vRsRlzyZyxxMapper;

    @Resource
    private VGgfwDictMapper vGgfwDictMapper;
    
    @Resource
    private VAc01Mapper vAc01Mapper;
    
    @Resource
    private VAb01Mapper vAb01Mapper;
    
    @Resource
    private RsGgfwUserMapper rsGgfwUserMapper;

    @Override
    public List<Object> getZyTree(AreaDTO dto) {
        List<Object> orgList = new ArrayList<Object>();
        if (StringUtils.isNotBlank(dto.getParentid())){
            List<Map<String,Object>> itemList = vRsRlzyZyxxMapper.getZyxxCList(dto.getParentid().trim());
            for (int i = 0; i < itemList.size(); i++) {
                Map<String, Object> orgmap = new HashMap<String, Object>();
                Map Cmap = itemList.get(i);
                orgmap.put("id", Cmap.get("id"));
                orgmap.put("isLeaf", true);
                orgList.add(orgmap);
            }
        } else {
            List<Map<String,Object>> itemList = vRsRlzyZyxxMapper.getZyxxPList();
            for (int i = 0; i < itemList.size(); i++) {
                Map<String, Object> orgmap = new HashMap<String, Object>();
                Map Pmap = itemList.get(i);
                orgmap.put("id", Pmap.get("id"));
                orgmap.put("pId", "0");
                orgList.add(orgmap);
            }
        }
        return orgList;
    }

    @Override
    public Map<String, Object> getDicts(CommonDictDTO dto) {
        String[] arr = dto.getDictArr();
        Map<String, Object> dictMap = new HashMap<>();
        Map<String, List> codedetailDictMap = new HashMap<>();
        Map<String, List> valuelabelDictMap = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            List codedetailList = new ArrayList();
            List valuelabelList = new ArrayList();
            List<VGgfwDict> itemList = vGgfwDictMapper.selectList(new QueryWrapper<VGgfwDict>()
                    .select("aaa100", "aaa102", "aaa103")
                    .eq("aaa100", arr[i]).orderByAsc("aaa100","aaa102"));
            if (itemList!=null){
                for (int j = 0; j < itemList.size(); j++) {
                    Map<String, Object> keyVlueMap = new HashMap<>();
                    Map<String, Object> valuelabelMap = new HashMap<>();
                    VGgfwDict po = itemList.get(j);
                    keyVlueMap.put("code", po.getAaa102());
                    valuelabelList.add(valuelabelMap);
                }
            }
            valuelabelDictMap.put(arr[i], valuelabelList);
        }
        dictMap.put("value_label", valuelabelDictMap);
        return dictMap;
    }
    
    @Override
    public VSAc01PO getAc01(String zjlx, String zjhm) {
    	QueryWrapper<VSAc01PO> queryWrapper = new QueryWrapper<VSAc01PO>();
    	queryWrapper.eq("aac058", zjlx);
    	return vAc01Mapper.selectOne(queryWrapper);
    }
    
    @Override
    public RsGgfwUserPO getGgfwUser(String ztid, String fwdx, String zjlx, String zjhm) {
    	QueryWrapper<RsGgfwUserPO> queryWrapper = new QueryWrapper<RsGgfwUserPO>();
    	queryWrapper = new QueryWrapper<RsGgfwUserPO>();
		queryWrapper.eq("fwdx", fwdx);
    	if("2".equals(fwdx)) {
    		queryWrapper.eq("zjhm", zjhm);
    	}
    	queryWrapper.eq("rownum", 1);
    	return rsGgfwUserMapper.selectOne(queryWrapper);
    }
    
    @Override
    public void saveAc01(VSAc01PO vSAc01PO) {
    	vAc01Mapper.insert(vSAc01PO);
    }
    
    @Override
    public void saveGgfwUser(RsGgfwUserPO rsGgfwUserPO) {
    	if(StringUtils.isNotBlank(rsGgfwUserPO.getUserid())) {
    		rsGgfwUserMapper.updateById(rsGgfwUserPO);
    	}else {
    		rsGgfwUserMapper.insert(rsGgfwUserPO);
    	}
    }
    
    @Override
    public VSAb01PO getAb01(String dwmc, String tyshxydm) {
    	QueryWrapper<VSAb01PO> queryWrapper = new QueryWrapper<VSAb01PO>();
    	//优先用统一社会信用代码查询
		queryWrapper.eq("aab998", tyshxydm);
		//用单位名称查询
		queryWrapper = new QueryWrapper<VSAb01PO>();
		queryWrapper.eq("aab004", dwmc);
		return vAb01Mapper.selectOne(queryWrapper);
    }
    
    @Override
    public void saveAb01(VSAb01PO vSAb01PO) {
    	vAb01Mapper.insert(vSAb01PO);
    }
}
