package org.jeecg.modules.warningThreshold.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.modules.warningThreshold.entity.YjPumpingWarnValue;
import org.jeecg.modules.warningThreshold.entity.YjTorqueWarnValue;
import org.jeecg.modules.warningThreshold.mapper.YjTorqueWarnValueMapper;
import org.jeecg.modules.warningThreshold.params.YjTorqueWarnValuePageParams;
import org.jeecg.modules.warningThreshold.service.YjTorqueWarnValueService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 扭矩预警阈值;(yj_torque_warn_value)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2025-1-7
 */
@Service
public class YjTorqueWarnValueServiceImpl extends ServiceImpl<YjTorqueWarnValueMapper, YjTorqueWarnValue> implements YjTorqueWarnValueService {


    @Override
    public Page<YjTorqueWarnValue> page(YjTorqueWarnValuePageParams pageParams){
        QueryWrapper<YjTorqueWarnValue> wrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(pageParams.getJh())){
            wrapper.lambda().like(YjTorqueWarnValue::getJh,pageParams.getJh());
        }
        wrapper.lambda().orderByAsc(YjTorqueWarnValue::getJh,YjTorqueWarnValue::getSortCode);
        Page<YjTorqueWarnValue> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        return  this.page(page, wrapper);
    }


    @Override
    public List<YjTorqueWarnValue> getListByJh(String jh){
        QueryWrapper<YjTorqueWarnValue> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(YjTorqueWarnValue::getJh,jh);
        wrapper.lambda().orderByAsc(YjTorqueWarnValue::getSortCode);
        List<YjTorqueWarnValue> list = this.list(wrapper);
        if(list.size()==0){
            list = createList(jh);
            this.saveBatch(list);
        }
        return list;
    }

    public List<YjTorqueWarnValue> createList(String jh){
        List<YjTorqueWarnValue> list = new ArrayList<>();
        YjTorqueWarnValue model=new YjTorqueWarnValue();
        model.setJh(jh);
        model.setBitdepth(BigDecimal.valueOf(500));
        model.setTorqueMin(BigDecimal.valueOf(0));
        model.setTorqueMax(BigDecimal.valueOf(0));
        model.setVarianceWarnValue(BigDecimal.valueOf(0.2));
        model.setFluctuateWarn(BigDecimal.valueOf(1));
        model.setSortCode(1);
        list.add(model);
        return list;
    }

    @Override
    public void saveOrUpd(YjTorqueWarnValue model){
        this.saveOrUpdate(model);
    }
}
