package com.dsk.jsk.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.jsk.domain.vo.EnterpriseCreditChinaVo;
import com.dsk.jsk.domain.vo.EnterpriseCreditChinaYzsxztmdVo;
import com.dsk.system.utils.DskOpenApiUtil;
import com.dsk.jsk.domain.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class EnterpriseCreditService {

    private static final Logger log = LoggerFactory.getLogger(EnterpriseService.class);

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;


    public TableDataInfo penalizePage(EnterpriseCreditPenalizePageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/penalizePage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R penalizeReasonType(EnterpriseCreditPenalizeReasonTypeBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/penalizeReasonType", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo abnormalPage(EnterpriseCreditAbnormalPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/abnormalPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R abnormalYears(EnterpriseCreditAbnormalYearsBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/abnormalYears", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo executedPersonsPage(EnterpriseCreditExecutedPersonsPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/executedPersonsPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo executedPage(EnterpriseCreditExecutedPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/executedPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo lawsuitsPage(EnterpriseCreditLawsuitsPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/lawsuitsPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R lawsuitsCauseAction(EnterpriseCreditLawsuitsCauseActionBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/lawsuitsCauseAction", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public R lawsuitsRole(EnterpriseCreditLawsuitsRoleBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/lawsuitsRole", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo noticesPage(EnterpriseCreditNoticePageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/noticesPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R noticesType(EnterpriseCreditNoticeTypeBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/noticesType", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public R noticesRole(EnterpriseCreditNoticeRoleBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/noticesRole", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public R noticesCaseReason(EnterpriseCreditNoticeCaseReasonBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/noticesCaseReason", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo kaitingPage(EnterpriseCreditKaitingPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/kaitingPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R kaitingCauseAction(EnterpriseCreditKaitingCauseActionBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/kaitingCauseAction", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public R kaitingPureRole(EnterpriseCreditKaitingPureRoleBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/kaitingPureRole", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo restrictedConsumerPage(EnterpriseCreditPageBody body) throws Exception{
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/restrictedConsumerPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo judicialFreezesPage(EnterpriseCreditPageBody body) throws Exception{
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/judicialFreezesPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo selectChinaXzxkPage(EnterpriseCreditChinaPageBody body) throws Exception{
        body.setPageSize(1000);
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaXzxkPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public R selectChinaXzxkDetail(EnterpriseCreditChinaDetailBody body) throws Exception{
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaXzxkDetail", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo selectChinaXzcfPage(EnterpriseCreditChinaPageBody body) throws Exception{
        body.setPageSize(1000);
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaXzcfPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public R selectChinaXzcfDetail(EnterpriseCreditChinaDetailBody body) throws Exception{
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaXzcfDetail", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo selectChinaNsxydjPage(EnterpriseCreditChinaPageBody body) throws Exception{
        body.setPageSize(1000);
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaNsxydjPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo selectChinaHggjczqyPage(EnterpriseCreditChinaPageBody body) throws Exception{
        body.setPageSize(1000);
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaHggjczqyPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo selectChinaYzsxztmdPage(EnterpriseCreditChinaPageBody body) throws Exception{
        body.setPageSize(1000);
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaYzsxztmdPage", BeanUtil.beanToMap(body, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (200 != code) {
            throw new RuntimeException();
        }
        Map data = MapUtils.getMap(map, "data", null);
        List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));
        if (CollectionUtils.isEmpty(list)) {
            return new TableDataInfo(list, 0);
        }

        for (Object supplierProject : list) {
            Map<String, Object> dataMap = CommonUtils.assertAsMap(supplierProject);
            String json = MapUtils.getString(dataMap, "json");
            if (ObjectUtil.isNotEmpty(json)) {
                EnterpriseCreditChinaVo creditChinaVo = JSONUtil.toBean(json, EnterpriseCreditChinaVo.class);
                EnterpriseCreditChinaYzsxztmdVo yzsxztmdVo = creditChinaVo.getEntity();
                dataMap.put("area_name", yzsxztmdVo.getArea_name());
                dataMap.put("disreput_type_name", yzsxztmdVo.getDisreput_type_name());
                dataMap.put("case_code", yzsxztmdVo.getCase_code());
                dataMap.put("unperform_part", yzsxztmdVo.getUnperform_part());
                dataMap.put("iname", yzsxztmdVo.getIname());
                dataMap.put("reg_date", yzsxztmdVo.getReg_date());
                dataMap.put("performance", yzsxztmdVo.getPerformance());
                dataMap.put("gist_unit", yzsxztmdVo.getGist_unit());
                dataMap.put("gist_cid", yzsxztmdVo.getGist_cid());
                dataMap.put("duty", yzsxztmdVo.getDuty());
                dataMap.put("cardnumber", yzsxztmdVo.getCardnumber());
                dataMap.put("court_name", yzsxztmdVo.getCourt_name());
                dataMap.put("publish_date", yzsxztmdVo.getPublish_date());
                dataMap.put("performed_part", yzsxztmdVo.getPerformed_part());
            } else {
                dataMap.put("area_name", null);
                dataMap.put("disreput_type_name", null);
                dataMap.put("case_code", null);
                dataMap.put("unperform_part", null);
                dataMap.put("iname", null);
                dataMap.put("reg_date", null);
                dataMap.put("performance", null);
                dataMap.put("gist_unit", null);
                dataMap.put("gist_cid", null);
                dataMap.put("duty", null);
                dataMap.put("cardnumber", null);
                dataMap.put("court_name", null);
                dataMap.put("publish_date", null);
                dataMap.put("performed_part", null);
            }
        }
        return new TableDataInfo(list, MapUtils.getInteger(data, "totalCount", 0));
    }

    public TableDataInfo selectChinaJyycPage(EnterpriseCreditChinaPageBody body) throws Exception{
        body.setPageSize(1000);
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCredit/selectChinaJyycPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }
}
