package com.dsk.system.dskService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.domain.model.*;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.utils.DskOpenApiUtil;
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.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnterpriseBussinessService {

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

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;


    public TableDataInfo clientPage(EnterpriseBussinessClientPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/clientPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public Map<String, Object> clientProjectPage(EnterpriseBussinessClientProjectPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/clientProjectPage", BeanUtil.beanToMap(body, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        Map data = MapUtils.getMap(map, "data", null);
        if (200 != code) throw new RuntimeException();
        Integer count = MapUtils.getInteger(data, "totalCount", 0);
        Object list = MapUtils.getObject(data, "list", "");
        TableDataInfo tableDataInfo = new TableDataInfo((List<?>) list, count);
        Map<String, Object> resMap = BeanUtil.beanToMap(tableDataInfo);
        resMap.put("totalAmount", MapUtils.getDouble(data, "totalAmount", 0.0));
        return resMap;
    }


    public TableDataInfo tenderPage(EnterpriseBussinessTenderPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/tenderPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R tenderDetail(EnterpriseBussinessTenderDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/tenderDetail", BeanUtil.beanToMap(body, false, false));
        Map data = MapUtils.getMap(map, "data", null);
        String mongoContentId = MapUtils.getString(data, "mongoContentId");
        if (ObjectUtil.isEmpty(mongoContentId)) {
            return BeanUtil.toBean(map, R.class);
        }

        HashMap<String, Object> contentParam = new HashMap<>();
        contentParam.put("data_type", "kaibiao");
        contentParam.put("filter_type", 2);
        contentParam.put("strategy_id", mongoContentId);
        Map<String, Object> contentMap = dskOpenApiUtil.requestBody("/mongocontent/v1/cjb/mongo_content", contentParam);

        log.info("contentData:{}", JSONUtil.toJsonStr(contentMap));
        Map contentData = MapUtils.getMap(contentMap, "data", null);

        if (200 != MapUtils.getInteger(contentMap, "code", 300)) {
            return BeanUtil.toBean(map, R.class);
        }

        String htmlContent = MapUtils.getString(contentData, "htmlContent");

        data.put("content", htmlContent);
        map.put("data", data);

        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo supplierPage(EnterpriseBussinessSupplierPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/supplierPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public Map<String, Object> supplierProjectPage(EnterpriseBussinessSupplierProjectPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/supplierProjectPage", BeanUtil.beanToMap(body, false, false));
        log.info(JSONUtil.toJsonStr(map));
        Integer code = MapUtils.getInteger(map, "code", 300);
        Map data = MapUtils.getMap(map, "data", null);
        if (200 != code) throw new RuntimeException();
        Integer count = MapUtils.getInteger(data, "totalCount", 0);
        Object list = MapUtils.getObject(data, "list", "");
        TableDataInfo tableDataInfo = new TableDataInfo((List<?>) list, count);
        Map<String, Object> resMap = BeanUtil.beanToMap(tableDataInfo);
        resMap.put("totalAmount", MapUtils.getDouble(data, "totalAmount", 0.0));
        return resMap;
    }


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


    public TableDataInfo oftenAgencyPage(EnterpriseBussinessOftenAgencyPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/oftenAgencyPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo bidCooperatePage(EnterpriseBussinessBidCooperatePageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/bidCooperatePage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo historySendPage(EnterpriseBussinessHistorySendPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/historySendPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


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