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

import com.alibaba.fastjson.JSONObject;
import com.sky.ioc.entity.Environment;
import com.sky.ioc.entity.Indeicator;
import com.sky.ioc.entity.Label;
import com.sky.ioc.entity.params.IocParam;
import com.sky.ioc.entity.params.IocTimeRange;
import com.sky.ioc.mapper.FitnesMapper;
import com.sky.ioc.service.fitness.HealthService;
import com.sky.ioc.tool.DateUtil;
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.concurrent.ThreadLocalRandom;

/**
 * TODO 健康小屋
 * @Description
 * @Author LunCe
 * @Date 2023/2/10 17:35
 **/

@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    FitnesMapper fitnesMapper;

    @Override
    public ReturnMsg getCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        ThreadLocalRandom random = ThreadLocalRandom.current();
        Integer totalPerson = fitnesMapper.getTotalPersonByCompanyIdAndDeptId(iocParam,1);
        Integer totalHealthPerson = fitnesMapper.getTotalPersonByCompanyIdAndDeptIdAndFitnesType(iocParam,1);
//        Integer totalSubHealthPerson = fitnesMapper.getTotalPersonByCompanyIdAndDeptIdAndFitnesType(iocParam,2);
        // 体检转化率
        Integer convertPercent = 0;
        double compare = random.nextDouble(-1, 1);
        compare = Double.parseDouble(decimalFormat.format(compare));
        objects.get(0).put("value",totalPerson);
        objects.get(0).put("compare",compare);

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

        if (totalPerson>0) {
            convertPercent = random.nextInt(70,95);
        }
        objects.get(2).put("value", convertPercent);
        objects.get(2).put("compare",compare);
        objects.get(3).put("value","全面体检");
        indeicator.setWorthAttention("员⼯使⽤意愿较低，建议调整⼩屋布置策略喔");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getHutAnalysis(IocParam iocParam) {
        String[] param = {"warehousing","physicalExamination"};
       // String[] labKey = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        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<>();
        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);
            Integer checkPerson = fitnesMapper.getTotalPersonByCompanyIdAndDeptId(iocParam1,1);
            ThreadLocalRandom random = ThreadLocalRandom.current();
            Integer add = random.nextInt(0,20);
            jsonObject.put(param[0],checkPerson+add);
            jsonObject.put(param[1],checkPerson);
            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 getEnvironmentalDetection(IocParam iocParam) {
        List<Environment> arrayList = new ArrayList<>();
        ThreadLocalRandom current = ThreadLocalRandom.current();
        arrayList.add(new Environment("pm","pm2.5","1", current.nextInt(50,100)+""));
        arrayList.add(new Environment("pm","pm2.5","2", current.nextInt(100,150)+""));

        arrayList.add(new Environment("hm","湿度","1", current.nextInt(35,50)+""));
        arrayList.add(new Environment("hm","湿度","2", current.nextInt(20,35)+""));

        arrayList.add(new Environment("tm","温度","1", current.nextInt(20,26)+""));
        arrayList.add(new Environment("tm","温度","2", current.nextInt(15,30)+""));
        return ReturnMsg.ok(arrayList);
    }
}
