package org.springblade.modules.app.manager;

import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.*;
import org.springblade.modules.app.common.query.StatBaseQuery;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.VerifyStationDTO;
import org.springblade.modules.app.service.IStatReportService;
import org.springblade.modules.app.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@RefreshScope
public class StatReportManager {

    @Autowired
    private IStatReportService statReportService;

    public StatHeatMapTopDTO statHeatMapTop(StatBaseQuery query){
        query = checkPram(query);
        return statReportService.statHeatMapTop(query);
    }

    public List<VerifyStationDTO> verify(StatBaseQuery query){
        query = checkPram(query);
        return statReportService.statVerifyStationOrderNum(query);
    }

    public List<StatHeatMapMidDTO> statHeatMapMid(StatBaseQuery query){
        query = checkPram(query);
        return statReportService.statHeatMapMid(query);
    }

    public StatHeatMapFootDTO statHeatMapFoot(StatBaseQuery query){
        query = checkPram(query);
        StatHeatMapFootDTO statHeatMapFootDTO = statReportService.statHeatMapFoot(query);
//        statHeatMapFootDTO.setPrepQuantity(11);
//        statHeatMapFootDTO.setWayQuantity(10);
        return statHeatMapFootDTO;
    }

    public List<StatWaybillDTO> prepList(StatBaseQuery query){
        query = checkPram(query);
        List<StatWaybillDTO> data = statReportService.prepList(query);
//        for (StatWaybillDTO statWaybillDTO : data){
//            if(statWaybillDTO.getPlanNum().equals(0)){
//                statWaybillDTO.setPlanNum(120);
//            }
//        }
        return data;
    }

    public List<StatWaybillDTO> wayList(StatBaseQuery query){
        query = checkPram(query);
        List<StatWaybillDTO> data = statReportService.wayList(query);
//        for (StatWaybillDTO statWaybillDTO : data){
//            if(statWaybillDTO.getPlanNum().equals(0)){
//                statWaybillDTO.setPlanNum(120);
//            }
//        }
        return data;
    }

    public List<StatWaybillDTO> finishList(StatBaseQuery query){
        query = checkPram(query);
        List<StatWaybillDTO> data = statReportService.finishList(query);
//        for (StatWaybillDTO statWaybillDTO : data){
//            if(statWaybillDTO.getPlanNum().equals(0)){
//                statWaybillDTO.setPlanNum(120);
//            }
//        }
        return data;
    }

    public List<StatBySalerDTO> statBySaler(StatBaseQuery query){
        query = checkPram(query);
        List<StatBySalerDTO> data = statReportService.statBySaler(query);
//        for(StatBySalerDTO statBySalerDTO : data){
//            if(StringUtils.isEmpty(statBySalerDTO.getSalerUserName())){
//                statBySalerDTO.setSalerUserName("伊通满族自治县营城子镇金扩家庭农场");
//                statBySalerDTO.setQuantity(240);
//            }
//            if(statBySalerDTO.getSalerUserName().equals("太谷县绿牧养殖专业合作社")){
//                statBySalerDTO.setQuantity(400);
//            }
//        }
        return data;
    }

    public List<StatByAgentUserDTO> statByAgent(StatBaseQuery query){
        query = checkPram(query);
        List<StatByAgentUserDTO> data = statReportService.statByAgent(query);

//        List<StatByAgentUserDTO> data = new ArrayList<StatByAgentUserDTO>();
//        StatByAgentUserDTO statByAgentUserDTO = new StatByAgentUserDTO();
//        statByAgentUserDTO.setAgentName("王涛");
//        statByAgentUserDTO.setAgentUserId(1465215623376683012L);
//        statByAgentUserDTO.setBuyerQuantity(1);
//        statByAgentUserDTO.setQuantity(150);
//        statByAgentUserDTO.setSalerQuantity(1);
//        data.add(statByAgentUserDTO);
//
//        statByAgentUserDTO = new StatByAgentUserDTO();
//        statByAgentUserDTO.setAgentName("张泽凯");
//        statByAgentUserDTO.setAgentUserId(1465105623376683012L);
//        statByAgentUserDTO.setBuyerQuantity(1);
//        statByAgentUserDTO.setQuantity(1105);
//        statByAgentUserDTO.setSalerQuantity(9);
//        data.add(statByAgentUserDTO);
//
//        statByAgentUserDTO = new StatByAgentUserDTO();
//        statByAgentUserDTO.setAgentName("谢建明");
//        statByAgentUserDTO.setAgentUserId(1465215623376683019L);
//        statByAgentUserDTO.setBuyerQuantity(1);
//        statByAgentUserDTO.setQuantity(1130);
//        statByAgentUserDTO.setSalerQuantity(6);
//        data.add(statByAgentUserDTO);
//
//        statByAgentUserDTO = new StatByAgentUserDTO();
//        statByAgentUserDTO.setAgentName("陈金富");
//        statByAgentUserDTO.setAgentUserId(1465105623376683015L);
//        statByAgentUserDTO.setBuyerQuantity(1);
//        statByAgentUserDTO.setQuantity(10);
//        statByAgentUserDTO.setSalerQuantity(11);
//        data.add(statByAgentUserDTO);
//
//        statByAgentUserDTO = new StatByAgentUserDTO();
//        statByAgentUserDTO.setAgentName("李成刚");
//        statByAgentUserDTO.setAgentUserId(1465105623376683018L);
//        statByAgentUserDTO.setBuyerQuantity(1);
//        statByAgentUserDTO.setQuantity(941);
//        statByAgentUserDTO.setSalerQuantity(4);
//        data.add(statByAgentUserDTO);


        return data;
    }

    public List<StatByBuyerDTO> statByBuyer(StatBaseQuery query){
        query = checkPram(query);
        //查询当前选择时间段的调运统计
        List<StatByBuyerDTO> list = statReportService.statByBuyer(query);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<Long> ids = new ArrayList<>();
        for(StatByBuyerDTO statByBuyerDTO : list){
            ids.add(statByBuyerDTO.getBuyerUserId());
        }
        //同比数据
        StatBaseQuery yoyQuery = new StatBaseQuery(DateUtils.getTbDate(query.getStartTime())+" 00:00:00",DateUtils.getTbDate(query.getEndTime())+ " 23:59:59");
        List<StatByBuyerDTO> yoyList = statReportService.statByBuyerHistory(yoyQuery,ids);
        //环比数据
        StatBaseQuery momQuery = new StatBaseQuery(DateUtils.getHbDate(query.getStartTime())+" 00:00:00",DateUtils.getHbDate(query.getEndTime())+ " 23:59:59");
        List<StatByBuyerDTO> momList = statReportService.statByBuyerHistory(momQuery,ids);
        //将同比，环比数量匹配到当前时间查询的数据中
        for(StatByBuyerDTO statByBuyerDTO : list){
            int yoys = 0;
            int moms = 0;
            for(StatByBuyerDTO yoy : yoyList){
                if(yoy != null
                        && yoy.getQuantity().intValue() > 0
                        && yoy.getBuyerUserId().longValue() == statByBuyerDTO.getBuyerUserId().longValue()){
                    //计算增长百分比
                    yoys = calculate(statByBuyerDTO.getQuantity(),yoy.getQuantity());
                }
            }
            for(StatByBuyerDTO mom : momList){
                if(mom != null
                        && mom.getQuantity().intValue() > 0
                        && mom.getBuyerUserId().longValue() == statByBuyerDTO.getBuyerUserId().longValue()){
                    //计算增长百分比
                    moms = calculate(statByBuyerDTO.getQuantity(),mom.getQuantity());
                }
            }
            statByBuyerDTO.setYoy(yoys);
            statByBuyerDTO.setMom(moms);
        }


//        List<StatByBuyerDTO> data = new ArrayList<StatByBuyerDTO>();
//
//
//
//        StatByBuyerDTO statByBuyerDTO = new StatByBuyerDTO();
//        statByBuyerDTO.setBuyerUserName("杭州径山畜禽屠宰有限公司");
//        statByBuyerDTO.setQuantity(3086);
//        statByBuyerDTO.setSalerQuantity(15);
//        statByBuyerDTO.setBuyerUserId(1465105623376683018L);
//        statByBuyerDTO.setMom(0);
//        statByBuyerDTO.setYoy(0);
//        data.add(statByBuyerDTO);
//
//        statByBuyerDTO = new StatByBuyerDTO();
//        statByBuyerDTO.setBuyerUserName("杭州联合西兴有限公司");
//        statByBuyerDTO.setQuantity(10);
//        statByBuyerDTO.setSalerQuantity(11);
//        statByBuyerDTO.setBuyerUserId(1465105623376683018L);
//        statByBuyerDTO.setMom(0);
//        statByBuyerDTO.setYoy(0);
//        data.add(statByBuyerDTO);



//        for (StatByBuyerDTO statWaybillDTO : list){
//            if(statByBuyerDTO.getBuyerUserName().equals("杭州联合西兴有限公司")){
//                statByBuyerDTO.setQuantity(10);
//                statByBuyerDTO.setSalerQuantity(11);
//            }
//
//
//        }

//        statByBuyerDTO = new StatByBuyerDTO();
//        statByBuyerDTO.setBuyerUserName("永嘉县食品有限公司");
//        statByBuyerDTO.setQuantity(150);
//        statByBuyerDTO.setSalerQuantity(1);
//        statByBuyerDTO.setBuyerUserId(1465105623376683018L);
//        statByBuyerDTO.setMom(0);
//        statByBuyerDTO.setYoy(0);
//        data.add(statByBuyerDTO);

        return list;
    }

    //计算增长百分比
    public int calculate(int currentQuantity,int historyQuantity){
        BigDecimal cur = new BigDecimal(String.valueOf(currentQuantity));
        BigDecimal his = new BigDecimal(String.valueOf(historyQuantity));
        return cur.subtract(his).divide(his,2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).intValue();
    }

    public List<StatExpOrderDTO> statExpOrder(StatBaseQuery query){
        query = checkPram(query);
//        List<StatExpOrderDTO> data = new ArrayList<StatExpOrderDTO>();
//        StatExpOrderDTO statExpOrderDTO = new StatExpOrderDTO();
//        statExpOrderDTO.setBuyerUserId(1465105623376683018L);
//        statExpOrderDTO.setBuyerUserName("杭州联合西兴有限公司");
//        statExpOrderDTO.setBillCode("BJ202206290767");
//        statExpOrderDTO.setExpType(2);
//        data.add(statExpOrderDTO);
//        return data;
        return statReportService.statExpOrder(query);
    }

    public List<StatViolationDTO> statViolation(StatBaseQuery query){
        query = checkPram(query);
        return statReportService.statViolation(query);
    }

    public StatBaseQuery checkPram(StatBaseQuery query){
        if(query!=null){
            if((StringUtils.isNotEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime()))
                    || (StringUtils.isEmpty(query.getStartTime()) && StringUtils.isNotEmpty(query.getEndTime()))){
                Validator.isTrue(false,ErrorCode.STAT_STATR_END_ISNULL);
            }
            query = new StatBaseQuery(query.getStartTime()+" 00:00:00",query.getEndTime()+" 23:59:59",query.getLimit());
        }
        return query;
    }
}
