package com.fastbee.rechargecard.service.impl;


import com.fastbee.common.utils.DateUtils;
import com.fastbee.rechargecard.domain.NgInformationWaterUser;
import com.fastbee.rechargecard.mapper.NgInformationWaterUserMapper;
import com.fastbee.rechargecard.service.INgInformationWaterUserService;
import net.bytebuddy.utility.JavaConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;

/**
 * 用水户信息Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-12-19
 */
@Service
public class NgInformationWaterUserServiceImpl implements INgInformationWaterUserService
{
    @Autowired
    private NgInformationWaterUserMapper ngInformationWaterUserMapper;

    /**
     * 查询用水户信息
     * 
     * @param waterUserId 用水户信息主键
     * @return 用水户信息
     */
    @Override
    public NgInformationWaterUser selectNgInformationWaterUserByWaterUserId(Long waterUserId)
    {
        return ngInformationWaterUserMapper.selectNgInformationWaterUserByWaterUserId(waterUserId);
    }

    /**
     * 查询用水户信息列表
     * 
     * @param ngInformationWaterUser 用水户信息
     * @return 用水户信息
     */
    @Override
    public List<NgInformationWaterUser> selectNgInformationWaterUserList(NgInformationWaterUser ngInformationWaterUser)
    {
        return ngInformationWaterUserMapper.selectNgInformationWaterUserList(ngInformationWaterUser);
    }

    /**
     * 新增用水户信息
     * 
     * @param ngInformationWaterUser 用水户信息
     * @return 结果
     */
    @Override
    public int insertNgInformationWaterUser(NgInformationWaterUser ngInformationWaterUser)
    {
        BigDecimal waterConsumption = ngInformationWaterUser.getWaterConsumption();
        BigDecimal waterConsumptionIndex = ngInformationWaterUser.getWaterConsumptionindex();
        // 使用 divide 方法进行除法运算，并指定精度和舍入模式
        BigDecimal ngInformationWater = waterConsumption.divide(waterConsumptionIndex, new MathContext(10, RoundingMode.HALF_UP));
        BigDecimal green = new BigDecimal("0.9");
        BigDecimal yellow = new BigDecimal("1.0");;
        if (ngInformationWater.compareTo(green) < 0){
            ngInformationWaterUser.setWarningStatus(1L);
        } else if (ngInformationWater.compareTo(yellow)<0) {
            ngInformationWaterUser.setWarningStatus(2L);
        }else {
            ngInformationWaterUser.setWarningStatus(3L);
        }
        return ngInformationWaterUserMapper.insertNgInformationWaterUser(ngInformationWaterUser);
    }

    /**
     * 修改用水户信息
     * 
     * @param ngInformationWaterUser 用水户信息
     * @return 结果
     */
    @Override
    public int updateNgInformationWaterUser(NgInformationWaterUser ngInformationWaterUser)
    {
        ngInformationWaterUser.setUpdateTime(DateUtils.getNowDate());
        return ngInformationWaterUserMapper.updateNgInformationWaterUser(ngInformationWaterUser);
    }

    /**
     * 批量删除用水户信息
     * 
     * @param waterUserIds 需要删除的用水户信息主键
     * @return 结果
     */
    @Override
    public int deleteNgInformationWaterUserByWaterUserIds(Long[] waterUserIds)
    {
        return ngInformationWaterUserMapper.deleteNgInformationWaterUserByWaterUserIds(waterUserIds);
    }

    /**
     * 删除用水户信息信息
     * 
     * @param waterUserId 用水户信息主键
     * @return 结果
     */
    @Override
    public int deleteNgInformationWaterUserByWaterUserId(Long waterUserId)
    {
        return ngInformationWaterUserMapper.deleteNgInformationWaterUserByWaterUserId(waterUserId);
    }
}
