package com.sckw.report.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.NumberUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.fleet.api.RemoteFleetService;
import com.sckw.report.model.vo.KwfCapacityVo;
import com.sckw.system.api.RemoteUserService;
import com.sckw.system.api.model.dto.req.QueryEntInfoByCityCodeAndEntTypesWithPageReqDto;
import com.sckw.system.api.model.dto.res.KwsEnterpriseResDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zk
 * @desc 车队服务
 * @date 2023/7/10 0010
 */
@Service
public class KwFleetService {

    @DubboReference(version = "1.0.0", group = "design", check = false)
    RemoteUserService userService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    RemoteFleetService fleetService;

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询
     * @author zk
     * @date 2023/9/1
     **/
    public PageResult capacity(HashMap<String, Object> params) {
        // 参数组装
        List<Long> entIds = new ArrayList<>();
        Integer capacityAmountMin = null;
        Integer capacityAmountMax = null;
        String totalLoadStr = StringUtils.objectStr(params.get("totalLoad"));
        String [] array = StringUtils.isNotBlank(totalLoadStr) ? totalLoadStr.split(Global.MINUS_SIGN) : null;
        capacityAmountMin = NumberUtils.parseEmptyInteger(array != null && array.length > 0 ? array[0] : null);
        capacityAmountMax = NumberUtils.parseEmptyInteger(array != null && array.length > 1 ? array[1] : null);
        if (capacityAmountMin != null || capacityAmountMax != null) {
            List<Map<String, Object>> statistics = fleetService.capacityStatistics(entIds, capacityAmountMin, capacityAmountMax);
            statistics.forEach(e -> entIds.add(NumberUtils.parseEmptyLong(e.get("entId"))));
            if (CollectionUtils.isEmpty(entIds)) {
                return new PageResult();
            }
        }

        // 企业数据查询
        List<KwfCapacityVo> list = new ArrayList<>();
        int page = PageResult.getPage(params);
        int pageSize = PageResult.getPageSize(params);
        QueryEntInfoByCityCodeAndEntTypesWithPageReqDto queryPage = new QueryEntInfoByCityCodeAndEntTypesWithPageReqDto();
        queryPage.setPage(page);
        queryPage.setPageSize(pageSize);
        queryPage.setEntTypeList(new ArrayList<>(Arrays.asList(3, 4)));
        queryPage.setCityCode(NumberUtils.parseIntV1(params.get("cityCode")));
        queryPage.setEntName(StringUtils.objectStr(params.get("firmName")));
        queryPage.setEntIdList(entIds);
        PageResult pageResult = userService.queryEntInfoByCityCodeAndEntTypesWithPage(queryPage);
        List<KwsEnterpriseResDto> ents = JSON.parseArray(JSON.toJSONString(pageResult.getList()), KwsEnterpriseResDto.class);
        for (KwsEnterpriseResDto ent:ents) {
            KwfCapacityVo capacity = new KwfCapacityVo();
            capacity.setEntId(ent.getId());
            capacity.setFirmName(ent.getFirmName());
            capacity.setCityCode(ent.getCityCode());
            capacity.setCityName(ent.getCityName());
            capacity.setDetailAddress(ent.getDetailAddress());
            capacity.setApproval(ent.getApproval());
            capacity.setHead(ent.getHead());
            list.add(capacity);
            entIds.add(ent.getId());
        }

        //企业运力统计
        List<Map<String, Object>> statistics = fleetService.capacityStatistics(entIds, capacityAmountMin, capacityAmountMax);
        Map<Long, Map<String, Object>> entMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        statistics.forEach(e -> entMap.put(NumberUtils.parseEmptyLong(e.get("entId")), e));

        //企业运力统计查询
        for (KwfCapacityVo capacity:list) {
            Map<String, Object> statistic = entMap.get(capacity.getEntId());
            capacity.setCapacityTotal(statistic != null ? NumberUtils.parseInt(statistic.get("capacityTotal")) : Global.NUMERICAL_ZERO);
            capacity.setCapacityAmount(statistic != null ? NumberUtils.parseDouble(statistic.get("capacityAmount")) : Global.NUMERICAL_ZERO);
        }
        pageResult.setList(list);
        /*PageHelper.startPage(PageResult.getPage(params), PageResult.getPageSize(params));
        pageResult = PageHelperUtil.getPageResult(new PageInfo<>(list));*/
        return pageResult;
    }
}
