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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.ioc.entity.Indeicator;
import com.sky.ioc.entity.Label;
import com.sky.ioc.entity.domain.space.Company;
import com.sky.ioc.entity.domain.space.Department;
import com.sky.ioc.entity.params.IocParam;
import com.sky.ioc.entity.params.IocTimeRange;
import com.sky.ioc.mapper.carbon.PrintCarbonMapper;
import com.sky.ioc.mapper.space.CompanyMapper;
import com.sky.ioc.mapper.space.DepartmentMapper;
import com.sky.ioc.service.carbon.PrintCarbonService;
import com.sky.ioc.tool.DateUtil;
import com.sky.ioc.tool.GeneralMethod;
import com.sky.ioc.tool.MathUtils;
import com.sky.ioc.tool.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

@Service
public class PrintCarbonServiceImpl implements PrintCarbonService {

    @Autowired
    PrintCarbonMapper printCarbonMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public ReturnMsg getPrintCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        Double totalPrint = printCarbonMapper.getDischargeByTime(iocParam);
        Long totalPaper = printCarbonMapper.getPaperByTime(iocParam);
        Double totalInkbox = printCarbonMapper.getInkboxByTime(iocParam);
        Integer totalPerson = printCarbonMapper.getUserByTime(iocParam);
        float compare = (float) Math.random();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        compare = Float.parseFloat(decimalFormat.format(compare));
        objects.get(0).put("value",MathUtils.division(totalPrint,1,2));
        objects.get(0).put("compare",compare);

        objects.get(1).put("value", MathUtils.division(totalPrint,totalPerson,2));
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",MathUtils.division(totalPaper,totalPerson,2));
        objects.get(2).put("compare",compare);

        objects.get(3).put("value",MathUtils.division(totalInkbox,totalPerson,2));
        objects.get(3).put("compare",compare);
        indeicator.setWorthAttention("⽂印管理持续为您服务");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getPrintInfo(IocParam iocParam) {
       /* String[] param = {"中迅","北京规划院","郑分","广分","上分","成分"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);*/
        ThreadLocalRandom random = ThreadLocalRandom.current();
        Company company =  companyMapper.selectById(1);
        Department department = departmentMapper.selectById(company.getOfficeId());
        List<Department> departments =departmentMapper.selectList(new LambdaQueryWrapper<Department>().eq(Department::getParentId,department.getOriId()));
        String[] param = departments.stream().map(Department::getName).collect(Collectors.toList()).toArray(new String[0]);
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            IocParam iocParam1 = new IocParam();
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:59");
            iocParam1.setTimeRange(iocTimeRange);
            JSONObject jsonObject = new JSONObject();
            for (int j = 0; j < departments.size(); j++) {
                iocParam1.setDeptId(departments.get(j).getId());
                double total = printCarbonMapper.getDischargeByTime(iocParam1);
                if(total==0){
                    total = random.nextInt(5,50);
                }
                jsonObject.put(param[j],total);
            }
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        // List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getPrintInfoOrder(IocParam iocParam) {
        String[] param = {"Print"};
        //String[] labKey = {"中旬","规划院","成分","上分","广分","郑分"};
        List<Map<String,Object>> lists = printCarbonMapper.getDischargeOrderByTime(iocParam);
        List<Label> labels1 = new ArrayList<>();
        for(Map<String,Object> map:lists){
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            label.setJsonObject(jsonObject);
            label.setLabel(String.valueOf(map.get("name")));
            labels1.add(label);
            jsonObject.put(param[0],MathUtils.division(Double.parseDouble(String.valueOf(map.get("total"))),1,2));
        }
       // List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }
}
