/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.dev.modular.job.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.unis.common.timer.CommonTimerTaskRunner;
import com.unis.common.util.TempUtils;
import com.unis.intelligentManagement.modular.tLqData.vo.TLqDataTaskVO;
import com.unis.intelligentManagement.modular.rgb.entity.TPoints;
import com.unis.intelligentManagement.modular.tLqData.entity.TLqData;
import com.unis.intelligentManagement.modular.tLqData.mapper.TLqDataMapper;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoGrain.entity.BizPssMoGrain;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoGrain.mapper.BizPssMoGrainMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 购销监管-粮情监管定时任务
 *
 * @author xuyuxiang
 * @date 2022/8/5 15:52
 **/
@Slf4j
@Component
public class LqSupervisionRunner implements CommonTimerTaskRunner {

    @Autowired
    private TLqDataMapper tLqDataMapper;

    @Autowired
    private BizPssMoGrainMapper bizPssMoGrainMapper;

    @Override
    public void action() {
        //获取开启的粮情模型数据
        List<BizPssMoGrain> bizPssMoGrainList = bizPssMoGrainMapper.selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                .eq(BizPssMoGrain::getIsEnabled, 1));
        if (CollectionUtil.isEmpty(bizPssMoGrainList)) {
            //空的，就结束程序
            return;
        }

        //有值就进行判断昨天的创建时间是否在模型时间内
        BizPssMoGrain bizPssMoGrain = bizPssMoGrainList.get(0);
        //获取模型时间--天数
        Integer detectionIntervalDay = bizPssMoGrain.getDetectionIntervalDay();
        //获取模型坏点百分比值
        Double badPointThreshold = Double.valueOf(bizPssMoGrain.getBadPointThreshold());

        //获取每个仓房最新的那条时间以及库区和仓房编号
        List<TLqDataTaskVO> tLqDataTaskVOList = tLqDataMapper.selectTLqData();

        for (TLqDataTaskVO tLqDataTaskVO : tLqDataTaskVOList) {
            //获取上次检索的时间
            Date createdate = tLqDataTaskVO.getCreatedate();
            //判断上次检索时间是否超出模型时间
            Date date = new Date();

            long dateTime = date.getTime() - createdate.getTime();

            //判断天数
            long dateNumber = dateTime / 24 / 60 / 60 / 1000;

            if (dateNumber > detectionIntervalDay) {
                tLqDataMapper.update(null, new LambdaUpdateWrapper<TLqData>()
                        .eq(TLqData::getOrgId, tLqDataTaskVO.getOrgId())
                        .eq(TLqData::getStorehouseCode, tLqDataTaskVO.getStorehouseCode())
                        .set(TLqData::getIsAccordModel, 0)
                        .eq(TLqData::getCreatedate, createdate));
                continue;
            }

            //获取到最新那条数据
            List<TLqData> tLqDataList = tLqDataMapper.selectList(new LambdaQueryWrapper<TLqData>()
                    .eq(TLqData::getOrgId, tLqDataTaskVO.getOrgId())
                    .eq(TLqData::getStorehouseCode, tLqDataTaskVO.getStorehouseCode())
                    .eq(TLqData::getCreatedate, createdate));

            if (CollectionUtil.isEmpty(tLqDataList)) {
                log.error("未找到对应的数据，orgId{},StorehouseCode{},Createdate{}", tLqDataTaskVO.getOrgId()
                        , tLqDataTaskVO.getStorehouseCode()
                        , tLqDataTaskVO.getCreatedate());
            }

            TLqData tLqData1 = tLqDataList.get(0);


            //在符合时间范围之内，判断坏点数是否符合
            String tempPoints = tLqData1.getTempPoints();
            List<TPoints> points = JSONArray.parseArray(tempPoints, TPoints.class);
            int count = (int) points.stream().map(TPoints::getTemp).filter(temp -> TempUtils.isBad(BigDecimal.valueOf(temp))).count();
            int size = points.size();

            double num = 0;
            if (size != 0) {
                num = ((double) count / size) * 100;
            }
            tLqDataMapper.update(null, new LambdaUpdateWrapper<TLqData>()
                    .set(TLqData::getIsAccordModel, num <= badPointThreshold ? 1 : 0)
                    .eq(TLqData::getId, tLqData1.getId()));
        }
    }


/*    public static void main(String[] args) throws ParseException {
*//*
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = format.parse("2024-06-11 00:00:00");
        Date date = format.parse("2024-06-12 23:59:59");
        Long starTime = date1.getTime();
        long time = date.getTime();
        long l = time - starTime;

        System.out.println(l / 60 / 60 / 1000);*//*


        String tempPoints = "[{\"temp\":250,\"x\":\"1\",\"y\":\"1\",\"z\":\"1\"},{\"temp\":10.3,\"x\":\"2\",\"y\":\"1\",\"z\":\"1\"},{\"temp\":7,\"x\":\"3\",\"y\":\"1\",\"z\":\"1\"},{\"temp\":11,\"x\":\"4\",\"y\":\"1\",\"z\":\"1\"},{\"temp\":8.3,\"x\":\"1\",\"y\":\"2\",\"z\":\"1\"},{\"temp\":7.3,\"x\":\"2\",\"y\":\"2\",\"z\":\"1\"},{\"temp\":12,\"x\":\"3\",\"y\":\"2\",\"z\":\"1\"},{\"temp\":10.3,\"x\":\"4\",\"y\":\"2\",\"z\":\"1\"},{\"temp\":7.3,\"x\":\"1\",\"y\":\"3\",\"z\":\"1\"},{\"temp\":15.3,\"x\":\"2\",\"y\":\"3\",\"z\":\"1\"},{\"temp\":10.5,\"x\":\"3\",\"y\":\"3\",\"z\":\"1\"},{\"temp\":9.8,\"x\":\"4\",\"y\":\"3\",\"z\":\"1\"},{\"temp\":13.8,\"x\":\"1\",\"y\":\"4\",\"z\":\"1\"},{\"temp\":13.5,\"x\":\"2\",\"y\":\"4\",\"z\":\"1\"},{\"temp\":13.3,\"x\":\"3\",\"y\":\"4\",\"z\":\"1\"},{\"temp\":16.8,\"x\":\"4\",\"y\":\"4\",\"z\":\"1\"},{\"temp\":11.3,\"x\":\"1\",\"y\":\"1\",\"z\":\"2\"},{\"temp\":12.5,\"x\":\"2\",\"y\":\"1\",\"z\":\"2\"},{\"temp\":16.8,\"x\":\"3\",\"y\":\"1\",\"z\":\"2\"},{\"temp\":10.8,\"x\":\"4\",\"y\":\"1\",\"z\":\"2\"},{\"temp\":14.8,\"x\":\"1\",\"y\":\"2\",\"z\":\"2\"},{\"temp\":15,\"x\":\"2\",\"y\":\"2\",\"z\":\"2\"},{\"temp\":12,\"x\":\"3\",\"y\":\"2\",\"z\":\"2\"},{\"temp\":13.3,\"x\":\"4\",\"y\":\"2\",\"z\":\"2\"},{\"temp\":13.8,\"x\":\"1\",\"y\":\"3\",\"z\":\"2\"},{\"temp\":11,\"x\":\"2\",\"y\":\"3\",\"z\":\"2\"},{\"temp\":9.3,\"x\":\"3\",\"y\":\"3\",\"z\":\"2\"},{\"temp\":14.5,\"x\":\"4\",\"y\":\"3\",\"z\":\"2\"},{\"temp\":12,\"x\":\"1\",\"y\":\"4\",\"z\":\"2\"},{\"temp\":7.5,\"x\":\"2\",\"y\":\"4\",\"z\":\"2\"},{\"temp\":12.8,\"x\":\"3\",\"y\":\"4\",\"z\":\"2\"},{\"temp\":13.8,\"x\":\"4\",\"y\":\"4\",\"z\":\"2\"},{\"temp\":10,\"x\":\"1\",\"y\":\"1\",\"z\":\"3\"},{\"temp\":15.8,\"x\":\"2\",\"y\":\"1\",\"z\":\"3\"},{\"temp\":11.5,\"x\":\"3\",\"y\":\"1\",\"z\":\"3\"},{\"temp\":10.8,\"x\":\"4\",\"y\":\"1\",\"z\":\"3\"},{\"temp\":15,\"x\":\"1\",\"y\":\"2\",\"z\":\"3\"},{\"temp\":11.8,\"x\":\"2\",\"y\":\"2\",\"z\":\"3\"},{\"temp\":8.3,\"x\":\"3\",\"y\":\"2\",\"z\":\"3\"},{\"temp\":16,\"x\":\"4\",\"y\":\"2\",\"z\":\"3\"},{\"temp\":8.8,\"x\":\"1\",\"y\":\"3\",\"z\":\"3\"},{\"temp\":6.8,\"x\":\"2\",\"y\":\"3\",\"z\":\"3\"},{\"temp\":15,\"x\":\"3\",\"y\":\"3\",\"z\":\"3\"},{\"temp\":9.5,\"x\":\"4\",\"y\":\"3\",\"z\":\"3\"},{\"temp\":6.8,\"x\":\"1\",\"y\":\"4\",\"z\":\"3\"},{\"temp\":17,\"x\":\"2\",\"y\":\"4\",\"z\":\"3\"},{\"temp\":12.8,\"x\":\"3\",\"y\":\"4\",\"z\":\"3\"},{\"temp\":13.5,\"x\":\"4\",\"y\":\"4\",\"z\":\"3\"}]";
        List<TPoints> points = JSONArray.parseArray(tempPoints, TPoints.class);
        int count = (int) points.stream().map(TPoints::getTemp).filter(temp -> TempUtils.isBad(BigDecimal.valueOf(temp))).count();
        int size = points.size();

        if (size != 0) {
            double num = ((double) count / size) * 100;
            System.out.println(num);
        }


    }*/

}

