package com.chinaweal.aiccs.crgs.movein.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.crgs.movein.entity.TbZcEntityMovein;
import com.chinaweal.aiccs.crgs.movein.entity.dto.MoveInEntityDto;
import com.chinaweal.aiccs.crgs.movein.mapper.TbZcEntityMoveinMapper;
import com.chinaweal.aiccs.crgs.movein.service.TbZcEntityMoveinService;
import com.chinaweal.aiccs.crgs.moveinDetails.entity.*;
import com.chinaweal.aiccs.crgs.moveinDetails.service.*;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-10-19
 */
@Service
public class TbZcEntityMoveinServiceImpl extends BaseServiceImpl<TbZcEntityMoveinMapper, TbZcEntityMovein> implements TbZcEntityMoveinService {

    @Autowired
    private MoveinCasePubBaseinfoService moveinCasePubBaseinfoService;
    @Autowired
    private CasePubPartyinfoService casePubPartyinfoService;
    @Autowired
    private CasePubAlterService casePubAlterService;
    @Autowired
    private CasePubDocService casePubDocService;
    @Autowired
    private MoveInAnBaseinfoService qrAnBaseinfoService;
    @Autowired
    private AnSubcapitalService anSubcapitalService;
    @Autowired
    private AnForinvestmentService anForinvestmentService;
    @Autowired
    private AnWebsiteinfoService anWebsiteinfoService;
    @Autowired
    private AnAlterstockinfoService anAlterstockinfoService;
    @Autowired
    private AnForguaranteeinfoService anForguaranteeinfoService;
    @Autowired
    private AnUpdateinfoService anUpdateinfoService;
    @Autowired
    private AnSupBaseinfoService anSupBaseinfoService;
    @Autowired
    private AnSocsecinfoService anSocsecinfoService;
    @Autowired
    private AnOverduepayService anOverduepayService;
    @Autowired
    private EBaseinfoService eBaseinfoService;

    @Override
    public IPage<MoveInEntityDto> searchPageWithBaseInfo(AICUser loginUser, PageRequestDto pageRequestDto) {
        System.out.println(loginUser);
        Map<String, Object> paramMap = pageRequestDto.getParamMap();
        if (paramMap == null) paramMap = new HashMap<>();
        String uniscid = StringUtils.tranObject(paramMap.get("uniscid"));
        if(StringUtils.isNotBlank(uniscid) && !StringUtils.isUscc(uniscid)) {
            paramMap.put("uniscid","");
            paramMap.put("regno",uniscid);
        }

        if(StringUtils.isNotBlank(StringUtils.tranObject(paramMap.get("moveInDateRange")))){
            Object datas = paramMap.get("moveInDateRange");
            if (datas instanceof List) {
                List<String> list = (List<String>) datas;
                paramMap.put("moveInDateOn", list.get(0));
                paramMap.put("moveInDateEnd", list.get(1));
            }
        }

        // 区局只查本级
        String regOrgc = baseMapper.queryOrgNumber(loginUser.getPrimaryKey());
        paramMap.put("regOrg", regOrgc);
        IPage<MoveInEntityDto> rs = baseMapper.searchPageWithBaseInfo(pageRequestDto.getParamMap(), new Page<>(pageRequestDto.getCurPage(), pageRequestDto.getPageSize()));
        rs.getRecords().forEach(each -> {
            String xzqh = Constant.NM_REGION_CODE.get(each.getRegOrg());
            if(StringUtils.isNotBlank(xzqh)){
                xzqh =xzqh + "市场监督管理局";
                each.setRegOrgCn(xzqh);
            }
        });
        return rs;
    }

    @Override
    public Map<String, Object> queryDetails(String pripid,String miid) {
        Map<String,Object> rs = new HashMap<>();
        TbZcEntityMovein tbZcEntityMovein = getById(miid);
        String xzqh = Constant.NM_REGION_CODE.get(tbZcEntityMovein.getMoutareregorg());
        if(StringUtils.isNotBlank(xzqh)){
            xzqh =xzqh + "市场监督管理局";
        }
        LambdaQueryWrapper<EBaseinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EBaseinfo::getPripid, pripid);
        EBaseinfo eBaseinfo = eBaseinfoService.getOne(wrapper);

        // 年报和行政处罚数据
        LambdaQueryWrapper<MoveinCasePubBaseinfo> casewrapper = new LambdaQueryWrapper();
        casewrapper.eq(MoveinCasePubBaseinfo::getUniscid, eBaseinfo.getUniscid());
        List<MoveinCasePubBaseinfo> moveinCasePubBaseinfos = moveinCasePubBaseinfoService.list(casewrapper);

        LambdaQueryWrapper<MoveInAnBaseinfo> anbaseWrapper = new LambdaQueryWrapper<>();
        anbaseWrapper.eq(MoveInAnBaseinfo::getUniscid, eBaseinfo.getUniscid());
        List<MoveInAnBaseinfo> moveInAnBaseinfos = qrAnBaseinfoService.list(anbaseWrapper);

        rs.put("tbZcEntityMovein",tbZcEntityMovein);
        rs.put("companyinfo",eBaseinfo);
        rs.put("moveinCasePubBaseinfos",moveinCasePubBaseinfos);
        rs.put("anBaseinfos", moveInAnBaseinfos);
        return rs;
    }

    @Override
    public List<TbZcEntityMovein> queryAllInfo() {
        return baseMapper.queryAllInfo();
    }

    @Override
    public List<TbZcEntityMovein> queryAllByPage(String beginId, Integer pageSize) {
        return baseMapper.queryAllByPage(beginId, pageSize);
    }

    @Override
    public Map<String, Object> queryCaseInfo(String caseid) {
        Map<String, Object> result = new HashMap<>();
        LambdaQueryWrapper<MoveinCasePubBaseinfo> casewrapper = new LambdaQueryWrapper();
        casewrapper.eq(MoveinCasePubBaseinfo::getCaseid, caseid);
        MoveinCasePubBaseinfo moveinCasePubBaseinfo = moveinCasePubBaseinfoService.getOne(casewrapper);

        LambdaQueryWrapper<CasePubPartyinfo> casePubPwrapper = new LambdaQueryWrapper();
        casePubPwrapper.eq(CasePubPartyinfo::getCaseid, caseid);
        CasePubPartyinfo casePubPartyinfo = casePubPartyinfoService.getOne(casePubPwrapper);

        LambdaQueryWrapper<CasePubAlter> casePubAlterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        casePubAlterLambdaQueryWrapper.eq(CasePubAlter::getCaseid,caseid);
        CasePubAlter casePubAlter = casePubAlterService.getOne(casePubAlterLambdaQueryWrapper);

        LambdaQueryWrapper<CasePubDoc> casePubDocLambdaQueryWrapper = new LambdaQueryWrapper<>();
        casePubDocLambdaQueryWrapper.eq(CasePubDoc::getCaseid,caseid);
        CasePubDoc casePubDoc = casePubDocService.getOne(casePubDocLambdaQueryWrapper);

        result.put("moveinCasePubBaseinfo",moveinCasePubBaseinfo);
        result.put("casePubPartyinfo",casePubPartyinfo);
        result.put("casePubAlter",casePubAlter);
        result.put("casePubDoc",casePubDoc);
        return result;
    }

    @Override
    public Map<String, Object> queryAnInfo(String ancheid) {
        Map<String, Object> result = new HashMap<>();
        MoveInAnBaseinfo moveInAnBaseinfo = qrAnBaseinfoService.getById(ancheid);
        List<AnSubcapital> anSubcapital = anSubcapitalService.list(new LambdaQueryWrapper<AnSubcapital>().eq(AnSubcapital::getAncheid,ancheid));
        List<AnForinvestment> anForinvestment = anForinvestmentService.list(new LambdaQueryWrapper<AnForinvestment>().eq(AnForinvestment::getAncheid,ancheid));
        AnWebsiteinfo anWebsiteinfo = anWebsiteinfoService.getOne(new LambdaQueryWrapper<AnWebsiteinfo>().eq(AnWebsiteinfo::getAncheid,ancheid));
        List<AnAlterstockinfo> anAlterstockinfo = anAlterstockinfoService.list(new LambdaQueryWrapper<AnAlterstockinfo>().eq(AnAlterstockinfo::getAncheid,ancheid));
        AnForguaranteeinfo anForguaranteeinfo = anForguaranteeinfoService.getOne(new LambdaQueryWrapper<AnForguaranteeinfo>().eq(AnForguaranteeinfo::getAncheid,ancheid));
        List<AnUpdateinfo> anUpdateinfo = anUpdateinfoService.list(new LambdaQueryWrapper<AnUpdateinfo>().eq(AnUpdateinfo::getAncheid,ancheid));
        AnSupBaseinfo anSupBaseinfo = anSupBaseinfoService.getById(ancheid);
        AnSocsecinfo anSocsecinfo = anSocsecinfoService.getOne(new LambdaQueryWrapper<AnSocsecinfo>().eq(AnSocsecinfo::getAncheid,ancheid));
        AnOverduepay anOverduepay = anOverduepayService.getOne(new LambdaQueryWrapper<AnOverduepay>().eq(AnOverduepay::getAncheid,ancheid));
        result.put("anBaseinfo", moveInAnBaseinfo);
        result.put("anSubcapital",anSubcapital);
        result.put("anForinvestment",anForinvestment);
        result.put("anWebsiteinfo",anWebsiteinfo);
        result.put("anAlterstockinfo",anAlterstockinfo);
        result.put("anForguaranteeinfo",anForguaranteeinfo);
//        result.put("anForguaranteeinfo",anForguaranteeinfo);
        result.put("anUpdateinfo",anUpdateinfo);
        result.put("anSupBaseinfo",anSupBaseinfo);
        result.put("anSocsecinfo",anSocsecinfo);
        result.put("anOverduepay",anOverduepay);
        return result;
    }
}
