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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.Bwcjbl;
import com.tofly.watermeter.mapper.BwcjblMapper;
import com.tofly.watermeter.service.BwcjblService;
import org.springframework.stereotype.Service;

/**
 * 水表抽检比例配置表
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("bwcjblService")
public class BwcjblServiceImpl extends ServiceImpl<BwcjblMapper, Bwcjbl> implements BwcjblService {

    @Override
    public IPage getConfig(Page page, Bwcjbl bwcjbl) {
        return baseMapper.getConfig(page, bwcjbl);
    }

    @Override
    public boolean updateCjblConfig(Bwcjbl bwcjbl) {
        Long id = bwcjbl.getId();
        Integer ratio = bwcjbl.getBwHgsl();
        if (ratio > 100) {
            ratio = 100;
        } else if (ratio < 0) {
            ratio = 0;
        }
        bwcjbl.setBwHgsl(ratio);
        this.updateById(bwcjbl);
        return true;
    }

    @Override
    public IPage<Bwcjbl> getCjblConfig(Page<Bwcjbl> page, Bwcjbl bwcjbl) {
        Long sbkj = bwcjbl.getBwSbkj();
        Long belong = bwcjbl.getBelong();
        if (belong == null){
            throw new ToflyDeniedException("未选择归属");
        }
        return this.page(page, Wrappers.<Bwcjbl>lambdaQuery().eq(StringUtil.isNotNull(sbkj), Bwcjbl::getBwSbkj, sbkj)
                .eq(Bwcjbl::getBelong, belong));
    }
}
