package com.mvc.service.aiiden;

import com.mvc.dao.TaskReviewPushMapper;
import com.mvc.entity.TaskReviewPush;
import com.mvc.entity.TaskReviewPushExample;
import com.mvc.model.TAireadModel;
import com.mvc.model.TaskReviewModel;
import com.mvc.service.copy.TAireadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TaskReviewPushService {

    @Resource
    private TaskReviewPushMapper taskReviewPushMapper;

    @Resource
    private TAireadService tAireadService;

    private final long MIN_MCUVERSION = 220901000000L;        //支持审核数据的最低MCU版本号

    /**
     * 根据审核对象生成审核数据推送记录
     *
     * @param taskReview
     * @return
     */
    public TaskReviewPush generateTaskReviewPush(TaskReviewModel taskReview) {
        if (taskReview.getReviewValue() == null) return null; //未写审核值的不推送 2023-06-20 guozhao

        //先检查审核的数据中,MCU版本是否已经支持处理审核数据
        TAireadModel tAireadModel = tAireadService.getTaireadByAireadId(taskReview.getAireadId());
        if (tAireadModel != null && StringUtils.isNotEmpty(tAireadModel.getSoftVer())) {
            //添加数字判断,新的免配置版本号中是带有字母的,且支持审核下发 2024-06-15 guozhao
            if(StringUtils.isNumeric(tAireadModel.getSoftVer())){
                long mv = Long.parseLong(tAireadModel.getSoftVer());
                if (mv < MIN_MCUVERSION) {
                    System.out.println("生成审核任务:SN:" + taskReview.getDeviceSn() + "[MCU版本不支持审核报文]");
                    return null;
                }
            }
        } else {
            System.out.println("生成审核任务:SN:" + taskReview.getDeviceSn() + "[无MCU版本]");
            return null;
        }

        TaskReviewPush pushModel = new TaskReviewPush();
        pushModel.setTaskReviewId(taskReview.getTaskId());
        pushModel.setDeviceSn(taskReview.getDeviceSn());
        pushModel.setStationCode(taskReview.getStationCode());
        pushModel.setSampleTime(taskReview.getSampleTime());
        pushModel.setReviewValue(taskReview.getReviewValue());
        pushModel.setFinishedFlag("0");
        pushModel.setPushNum(0);

        //将以前未推送成功的数据状态修改为2;
        TaskReviewPushExample example = new TaskReviewPushExample();
        TaskReviewPushExample.Criteria criteria = example.createCriteria();
        criteria.andStationCodeEqualTo(taskReview.getStationCode());
        criteria.andFinishedFlagEqualTo("0");
        TaskReviewPush updateModel = new TaskReviewPush();
        updateModel.setFinishedFlag("2");
        updateModel.setFinishedTime(new Date());
        taskReviewPushMapper.updateByExampleSelective(updateModel, example);         //更新历史数据
        taskReviewPushMapper.insert(pushModel);
        return pushModel;
    }

    /**
     * 根据SN或站代码查询未推送的任务(SN优先)
     *
     * @param deviceSn
     * @param stationCode
     * @return
     */
    public TaskReviewPush findTaskReviewPushByStationCode(String deviceSn, String stationCode) {
        TaskReviewPushExample example = new TaskReviewPushExample();
        TaskReviewPushExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(deviceSn)) {
            criteria.andDeviceSnEqualTo(deviceSn);
        } else {
            criteria.andStationCodeEqualTo(stationCode);
        }
        criteria.andReviewValueIsNotNull();//值空会引发后续问题 2023-06-20 guozhao
        criteria.andFinishedFlagEqualTo("0");
        List<TaskReviewPush> list = taskReviewPushMapper.selectByExample(example);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public TaskReviewPush saveTaskReviewPush(TaskReviewPush model) {
        //减少数据库空间占用,不保存原文了,需要再打开 2022.10.09 guozhao
        model.setReplyMsg("");
        model.setPushRawMsg("");
        TaskReviewPush TaskReviewPush = new TaskReviewPush();
        BeanUtils.copyProperties(model, TaskReviewPush);
        int insert = taskReviewPushMapper.insert(TaskReviewPush);
        model.setId(TaskReviewPush.getId());
        return model;
    }

    public TaskReviewPush updateTaskReviewPush(TaskReviewPush model) {
        TaskReviewPush TaskReviewPush = taskReviewPushMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TaskReviewPush);
        int i = taskReviewPushMapper.updateByPrimaryKey(TaskReviewPush);
        return model;
    }

    public int deleteTaskReviewPush(int id) {
        int i = taskReviewPushMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据审核ID删除审核推送记录
     * @param reviewId
     * @return
     */
    public int deleteTaskReviewPushByReviewId(String reviewId){
        TaskReviewPushExample example = new TaskReviewPushExample();
        example.createCriteria().andTaskReviewIdEqualTo(reviewId);
        return taskReviewPushMapper.deleteByExample(example);
    }
}
