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.mapper.YjPumpingWarnValueMapper;
import org.jeecg.modules.warningThreshold.params.YjPumpingWarnValuePageParams;
import org.jeecg.modules.warningThreshold.service.YjPumpingWarnValueService;
import org.springframework.stereotype.Service;

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

/**
 * 泵压预警阈值;(yj_pumping_warn_value)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2025-1-7
 */
@Service
public class YjPumpingWarnValueServiceImpl extends ServiceImpl<YjPumpingWarnValueMapper, YjPumpingWarnValue> implements YjPumpingWarnValueService {


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

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

    public  List<YjPumpingWarnValue> createList(String jh){
        List<YjPumpingWarnValue> list =new ArrayList<>();
        YjPumpingWarnValue model=new YjPumpingWarnValue();
        model.setJh(jh);
        model.setSortCode(1);
        model.setPumpingMin(BigDecimal.valueOf(0));
        model.setPumpingMax(BigDecimal.valueOf(30));
        model.setPumpingWarnValue(BigDecimal.valueOf(0.05));
        model.setSppWarnValue(BigDecimal.valueOf(0.1));
        YjPumpingWarnValue model1=new YjPumpingWarnValue();
        model1.setJh(jh);
        model1.setSortCode(2);
        model1.setPumpingMin(BigDecimal.valueOf(30));
        model1.setPumpingMax(BigDecimal.valueOf(0));
        model1.setPumpingWarnValue(BigDecimal.valueOf(0.05));
        model1.setSppWarnValue(BigDecimal.valueOf(0.05));
        list.add(model);
        list.add(model1);
        return list;
    }

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