package cn.iocoder.yudao.module.mes.framework.web;

import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaRecordDO;
import cn.iocoder.yudao.module.iot.dal.mysql.formula.FormulaRecordMapper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Component
@EnableScheduling
public class ScheduledService {
    @Resource
    private FormulaRecordMapper formulaRecordMapper;
    /**定时读取PLC*/
    //@Scheduled(fixedDelay = 1000)
    public void readPLCJob() {
        System.out.println("-------定时读取PLC-------");
    }

    private void dealRecord(Long id, String type, boolean random, double plusValue, int min, int max){
        if(id!=null){
            FormulaRecordDO recordDO = formulaRecordMapper.selectById(id);
            if(recordDO==null)return;
            if(random){
                if(type.equals("int")){
                    int i = RandomUtil.randomInt(min,max);
                    recordDO.setDataValue(recordDO.getDataValue().add(BigDecimal.valueOf(i)));
                }
                if(type.equals("double")){
                    double i = RandomUtil.randomDouble(min,max);
                    recordDO.setDataValue(recordDO.getDataValue().add(BigDecimal.valueOf(i)));
                }
            }
            else{
                recordDO.setDataValue(recordDO.getDataValue().add(BigDecimal.valueOf(plusValue)));
            }
            formulaRecordMapper.updateById(recordDO);
        }
    }
}
