package com.sky.ioc.service.supermarket.impl;

import com.alibaba.fastjson.JSONObject;
import com.sky.ioc.entity.Indeicator;
import com.sky.ioc.entity.Label;
import com.sky.ioc.entity.domain.supermarket.SupermarketOrder;
import com.sky.ioc.entity.params.IocParam;
import com.sky.ioc.entity.params.IocTimeRange;
import com.sky.ioc.mapper.SupermarketOrderMapper;
import com.sky.ioc.service.supermarket.SupermarketService;
import com.sky.ioc.tool.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * TODO
 *
 * @Description
 * @Author LunCe
 * @Date 2023/2/10 14:19
 **/
@Service
public class SupermarketServiceImpl implements SupermarketService {

    @Autowired
    private SupermarketOrderMapper supermarketOrderMapper;

    @Override
    public ReturnMsg getCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        Double totalPrice = supermarketOrderMapper.getTotalPriceByCompanyIdAndDeptId(iocParam);
        totalPrice = totalPrice == null ? 0 : totalPrice;
        Integer totalOrder = supermarketOrderMapper.getTotalOrderByCompanyIdAndDeptId(iocParam);
        Integer totalPerson = supermarketOrderMapper.getTotalPersonByCompanyIdAndDeptId(iocParam);
        double avgPrice = MathUtils.division(totalPrice,totalOrder,2);
        double avgPersonPrice = MathUtils.division(totalPrice,totalPerson,2);
        ThreadLocalRandom random = ThreadLocalRandom.current();
        double compare = random.nextDouble(-1, 1);
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        totalPrice = MathUtils.division(totalPrice,10000,2);
        compare = Double.parseDouble(decimalFormat.format(compare));
        objects.get(0).put("value", totalPrice);
        objects.get(0).put("compare", compare);

        objects.get(1).put("value", totalOrder);
        objects.get(1).put("compare", compare);

        objects.get(2).put("value", avgPersonPrice);
        objects.get(2).put("compare", compare);

        objects.get(3).put("value", avgPrice);
        objects.get(3).put("compare", compare);
        indeicator.setWorthAttention("商品的价格和种类可能需要有针对的提升喽");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getQuotientAnalysiss(IocParam iocParam) {
        String[] param = {"morningTotal", "afternoonTotal", "morningOrder", "afternoonOrder", "averageOrder"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> list = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = list.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            IocParam iocParam1 = new IocParam();
            iocParam1.setCompanyId(iocParam.getCompanyId());
            iocParam1.setDeptId(iocParam.getDeptId());
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 12:00:00");
            iocParam1.setTimeRange(iocTimeRange);
            IocParam iocParam2 = new IocParam();
            IocTimeRange iocTimeRange2 = new IocTimeRange();
            iocTimeRange2.setStartDate(labKey[i]+" 12:00:01");
            iocTimeRange2.setEndDate(labKey[i]+" 23:59:59");
            iocParam2.setTimeRange(iocTimeRange2);
            //上午
            Double totalPrice = supermarketOrderMapper.getTotalPriceByCompanyIdAndDeptId(iocParam1);
            totalPrice = totalPrice == null ? 0 : Double.valueOf(decimalFormat.format(totalPrice));
            Integer totalOrder = supermarketOrderMapper.getTotalOrderByCompanyIdAndDeptId(iocParam1);
            //下午
            Double afternoonTotalPrice = supermarketOrderMapper.getTotalPriceByCompanyIdAndDeptId(iocParam2);
            afternoonTotalPrice = afternoonTotalPrice == null ? 0 : Double.valueOf(decimalFormat.format(afternoonTotalPrice));
            Integer afternoonTotalOrder = supermarketOrderMapper.getTotalOrderByCompanyIdAndDeptId(iocParam2);
           // BigDecimal bigDecimal = new BigDecimal(totalPrice+afternoonTotalPrice);
            double avgPrice = 0d;
            if (totalPrice+afternoonTotalPrice > 0) {
              //  BigDecimal avgPriceDec = bigDecimal.divide(new BigDecimal(totalOrder+afternoonTotalOrder), 2, RoundingMode.HALF_UP);
                avgPrice = MathUtils.division(totalPrice+afternoonTotalPrice,totalOrder+afternoonTotalOrder,2);
            }
            jsonObject.put(param[0],totalPrice);
            jsonObject.put(param[1],afternoonTotalPrice);
            jsonObject.put(param[2],totalOrder);
            jsonObject.put(param[3],afternoonTotalOrder);
            jsonObject.put(param[4],avgPrice);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getProductRanking(IocParam iocParam) {
        List<Map<String, String>> list = supermarketOrderMapper.getListByCompanyIdAndDeptId(iocParam);
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getSupermarketInfoList(IocParam iocParam) {
        String[] param = {"consumptionAmount", "consumptionOrder"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> list = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = list.toArray(new String[0]);
        //List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        List<Label> labels1 = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            IocParam iocParam1 = new IocParam();
            iocParam1.setCompanyId(iocParam.getCompanyId());
            iocParam1.setDeptId(iocParam.getDeptId());
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:59");
            iocParam1.setTimeRange(iocTimeRange);
            Double totalPrice = supermarketOrderMapper.getTotalPriceByCompanyIdAndDeptId(iocParam1);
            totalPrice = totalPrice == null ? 0 : Double.valueOf(decimalFormat.format(totalPrice));
            Integer totalOrder = supermarketOrderMapper.getTotalOrderByCompanyIdAndDeptId(iocParam1);
            jsonObject.put(param[0],totalPrice);
            jsonObject.put(param[1],totalOrder);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getSupermarketCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        Double totalPrice = supermarketOrderMapper.getTotalPriceByCompanyIdAndDeptId(iocParam);
        totalPrice = totalPrice == null ? 0 : Double.valueOf(decimalFormat.format(totalPrice));
        BigDecimal bigDecimal = new BigDecimal(totalPrice);
        Integer totalOrder = supermarketOrderMapper.getTotalOrderByCompanyIdAndDeptId(iocParam);
        double avgPrice = 0d;
        if (totalPrice > 0) {
            BigDecimal avgPriceDec = bigDecimal.divide(new BigDecimal(totalOrder), 2, RoundingMode.HALF_UP);
            avgPrice = avgPriceDec.doubleValue();
        }
        ThreadLocalRandom random = ThreadLocalRandom.current();
        Double thirdPay = supermarketOrderMapper.getThirdTotalPriceByCompanyIdAndDeptId(iocParam);
        thirdPay = thirdPay == null ? 0 : Double.valueOf(decimalFormat.format(thirdPay));
        if(thirdPay>0){
            BigDecimal bigDecimal2 = new BigDecimal(thirdPay);
            thirdPay =  bigDecimal2.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP).doubleValue()*6;
        }
        thirdPay = Double.valueOf(decimalFormat.format(thirdPay));
        double compare = random.nextDouble(-1, 1);
        compare = Double.parseDouble(decimalFormat.format(compare));

        objects.get(0).put("value", totalPrice);
        objects.get(0).put("compare", compare);

        objects.get(1).put("value", totalOrder);
        objects.get(1).put("compare", compare);

        objects.get(2).put("value", avgPrice);
        objects.get(2).put("compare", compare);

        objects.get(3).put("value", thirdPay);
        objects.get(3).put("compare",compare);


        indeicator.setWorthAttention("员⼯消费意愿较低");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }
}
