/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.entity.Qfcbarithmetic;
import com.tofly.watermeter.mapper.QfcbarithmeticMapper;
import com.tofly.watermeter.service.QfcbarithmeticService;
import org.springframework.stereotype.Service;

/**
 * 算法配置表
 *
 * @author wangjieliu
 * @date Thu Oct 21 00:00:00 CST 2021
 */
@Service("qfcbarithmeticService")
public class QfcbarithmeticServiceImpl extends ServiceImpl<QfcbarithmeticMapper, Qfcbarithmetic> implements QfcbarithmeticService {


    @Override
    public boolean updateArith(Qfcbarithmetic qfcbarithmetic) {
        Long id = qfcbarithmetic.getId();
        Integer startKjcode = qfcbarithmetic.getStartKjcode();
        Integer endKjcode = qfcbarithmetic.getEndKjcode();
        Integer arithCount = qfcbarithmetic.getArithCount();
        String isForbidden = qfcbarithmetic.getIsForbidden();
        String comment = qfcbarithmetic.getIscomment();
        //算法配置是定死的，不能删除也不能新增，所以可以直接使用ID判断
        //0水量模型 ID 为 1
        if (id == 1) {
            if (arithCount > 4L) {
                throw new ToflyDeniedException("0水量模型：最大限制4次抄表0水量");
            }
            //拼接配置说明
            comment = "0水量模型：连续" + arithCount + "次抄表水量为0";
        }
        //居民用水
        else if (2L <= id & id <= 3L) {
            if (!(0 <= arithCount && arithCount <= 100)) {
                throw new ToflyDeniedException("请正确填写比例范围");
            }
            comment = "居民生活用水：口径为DN" + startKjcode + "--DN" + endKjcode + "水表，本次抄表水量与上次抄表水量升降幅超过" + arithCount + "%";
        }
        //非居民用水
        else if (4L <= id & id <= 6L) {
            if (!(0 <= arithCount && arithCount <= 100)) {
                throw new ToflyDeniedException("请正确填写比例范围");
            }
            comment = "非居民生活用水：口径为DN" + startKjcode + "--DN" + endKjcode + "水表，本次抄表水量与上次抄表水量升降幅超过" + arithCount + "%";
        }
        //特种用水
        else if (7L <= id & id <= 8L) {
            if (!(0 <= arithCount && arithCount <= 100)) {
                throw new ToflyDeniedException("请正确填写比例范围");
            }
            comment = "特种用水：口径为DN" + startKjcode + "--DN" + endKjcode + "水表，本次抄表水量与上次抄表水量升降幅超过" + arithCount + "%";
        }
        //连续非正常抄表
        else if (id == 9L) {
            comment = "前三次水量平均值大于" + arithCount + "吨参与异常提醒";
        }
        this.update(Wrappers.<Qfcbarithmetic>lambdaUpdate()
                .eq(Qfcbarithmetic::getId, id)
                .set(StringUtil.isNotEmpty(isForbidden), Qfcbarithmetic::getIsForbidden, isForbidden)
                .set(StringUtil.isNotEmpty(comment), Qfcbarithmetic::getIscomment, comment)
                .set(StringUtil.isNotNull(startKjcode), Qfcbarithmetic::getStartKjcode, startKjcode)
                .set(StringUtil.isNotNull(endKjcode), Qfcbarithmetic::getEndKjcode, endKjcode)
                .set(StringUtil.isNotNull(arithCount), Qfcbarithmetic::getArithCount, arithCount));
        return true;
    }
}
