package com.chinaweal.aiccs.aiccs.nonIocalCirculation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSWrit;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.EOtCase;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.EOtPermit;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.mapper.EOtCaseMapper;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.mapper.EOtPermitMapper;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.service.EOtCaseService;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.TSSerIllegalList;
import com.chinaweal.aiccs.aiccs.seriousillegal.mapper.TSSerIllegalListMapper;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.util.ConstantsUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行政处罚信息(临时) 服务实现类
 * </p>
 *
 * @author Yweirdo
 * @since 2021-08-18
 */
@Service
public class EOtCaseServiceImpl extends BaseServiceImpl<EOtCaseMapper, EOtCase> implements EOtCaseService {

    @Autowired
    EBaseinfoService eBaseinfoService;
    @Resource
    EOtPermitMapper eOtPermitMapper;
    @Resource
    TSSerIllegalListMapper tsSerIllegalListMapper;
    @Autowired
    private TRBaseCodeService baseCodeService;
    @Autowired
    private TSWritService tsWritService;

    @Override
    public Map<String,Object> queryByUNISCID(String pripid) {
        EBaseinfo eBaseinfo = eBaseinfoService.getOne(new LambdaQueryWrapper<EBaseinfo>().eq(EBaseinfo::getPripid,pripid));
        String UNISCID = "";
        if(StringUtils.isNotBlank(eBaseinfo.getUniscid())){
            UNISCID = eBaseinfo.getUniscid();
        } else {
            UNISCID = eBaseinfo.getRegno();
        }
        List<EOtCase> eOtCases = baseMapper.queryByUNISCID(UNISCID);
        List<EOtPermit> eOtPermits = eOtPermitMapper.queryByUNISCID(UNISCID);
        List<TSSerIllegalList> tsSerIllegalLists = tsSerIllegalListMapper.queryByPripid(pripid);
        // 查询文书
        for (TSSerIllegalList tsSerIllegalList : tsSerIllegalLists) {
            if (StringUtils.isNotBlank(tsSerIllegalList.getIntowritid())) {
                TSWrit tsWritLR = tsWritService.getById(tsSerIllegalList.getIntowritid());
                if (tsWritLR != null) tsSerIllegalList.setIntowritno(tsWritLR.getWritsno());
            }
            if (StringUtils.isNotBlank(tsSerIllegalList.getRemwritid())) {
                TSWrit tsWritYC = tsWritService.getById(tsSerIllegalList.getRemwritid());
                if (tsWritYC != null) tsSerIllegalList.setRemwritidno(tsWritYC.getWritsno());
            }
        }
        Map<String, TRBaseCode> codeMap = baseCodeService.getBaseCodeMapFormCache("CD73");
        Map<String, TRBaseCode> moveOutRea = baseCodeService.getBaseCodeMapFormCache("CD74");
        if(CollectionUtils.isNotEmpty(tsSerIllegalLists)){
            for (TSSerIllegalList tsSerIllegalList : tsSerIllegalLists) {
                if(StringUtils.isNotBlank(tsSerIllegalList.getRemexcpres())) {
                    TRBaseCode trBaseCode = codeMap.get(tsSerIllegalList.getSerillrea());
                    if (trBaseCode != null) {
                        tsSerIllegalList.setSerillreaString(trBaseCode.getName());
                    }
                }
                if(StringUtils.isNotBlank(tsSerIllegalList.getRemexcpres())){
                    TRBaseCode code = moveOutRea.get(tsSerIllegalList.getRemexcpres());
                    if(code != null){
                        tsSerIllegalList.setRemexcpresString(code.getName());
                    }
                }
                if(StringUtils.isBlank(tsSerIllegalList.getDecorgCn()) && StringUtils.isNotBlank(tsSerIllegalList.getDecorg())){
                    String areaName = ConstantsUtil.getAreaName(tsSerIllegalList.getDecorg().substring(0, 6));
                    tsSerIllegalList.setDecorgCn(areaName == null? StringUtils.EMPTY: (areaName + "市场监督管理局"));
                }
                if(StringUtils.isBlank(tsSerIllegalList.getRemdecorgCn()) && StringUtils.isNotBlank(tsSerIllegalList.getRemdecorg())){
                    String areaName = ConstantsUtil.getAreaName(tsSerIllegalList.getRemdecorg().substring(0, 6));
                    tsSerIllegalList.setRemdecorgCn(areaName == null? StringUtils.EMPTY: (areaName + "市场监督管理局"));
                }
            }
        }

        Map<String,Object> map = new HashMap();
        map.put("eOtCase",eOtCases);
        map.put("eOtPermit",eOtPermits);
        map.put("tsSerIllegalList",tsSerIllegalLists);
        return map;
    }
}
