package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ResidentInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.houseInfo.PropertyFee;
import com.ruoyi.system.mapper.ResidentInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PropertyFeeMapper;
import com.ruoyi.system.service.IPropertyFeeService;

/**
 * 物业费管理Service业务层处理
 * 
 * @author boman
 * @date 2025-03-03
 */
@Service
public class PropertyFeeServiceImpl implements IPropertyFeeService 
{
    @Autowired
    private PropertyFeeMapper propertyFeeMapper;

    @Autowired
    private ResidentInfoMapper residentInfoMapper;

    /**
     * 查询物业费管理
     * 
     * @param propertyId 物业费管理主键
     * @return 物业费管理
     */
    @Override
    public PropertyFee selectPropertyFeeByPropertyId(Long propertyId)
    {
        return propertyFeeMapper.selectPropertyFeeByPropertyId(propertyId);
    }

    /**
     * 查询物业费管理列表
     * 
     * @param propertyFee 物业费管理
     * @return 物业费管理
     */
    @Override
    public List<PropertyFee> selectPropertyFeeList(PropertyFee propertyFee)
    {
        return propertyFeeMapper.selectPropertyFeeList(propertyFee);
    }

    /**
     * 新增物业费管理
     * 
     * @param propertyFee 物业费管理
     * @return 结果
     */
    @Override
    public int insertPropertyFee(PropertyFee propertyFee)
    {
        propertyFee.setCreateTime(DateUtils.getNowDate());
        return propertyFeeMapper.insertPropertyFee(propertyFee);
    }

    /**
     * 修改物业费管理
     * 
     * @param propertyFee 物业费管理
     * @return 结果
     */
    @Override
    public int updatePropertyFee(PropertyFee propertyFee)
    {
        propertyFee.setUpdateTime(DateUtils.getNowDate());
        return propertyFeeMapper.updatePropertyFee(propertyFee);
    }

    /**
     * 批量删除物业费管理
     * 
     * @param propertyIds 需要删除的物业费管理主键
     * @return 结果
     */
    @Override
    public int deletePropertyFeeByPropertyIds(Long[] propertyIds)
    {
        return propertyFeeMapper.deletePropertyFeeByPropertyIds(propertyIds);
    }

    /**
     * 删除物业费管理信息
     * 
     * @param propertyId 物业费管理主键
     * @return 结果
     */
    @Override
    public int deletePropertyFeeByPropertyId(Long propertyId)
    {
        return propertyFeeMapper.deletePropertyFeeByPropertyId(propertyId);
    }

    @Override
    public AjaxResult statistics(PropertyFee propertyFee) {
        Map<String,BigDecimal> map = new HashMap<>();
        //物业费
        BigDecimal yjwyf = new BigDecimal("0.00");
        BigDecimal wjwyf = new BigDecimal("0.00");

        //车位费
        BigDecimal yjcwf = new BigDecimal("0.00");
        BigDecimal wjcwf = new BigDecimal("0.00");

        //能耗费
        BigDecimal yjnhf = new BigDecimal("0.00");
        BigDecimal wjnhf = new BigDecimal("0.00");

        //查询本年份所有数据并统计
        if(StringUtils.isEmpty(propertyFee.getYear())){
            propertyFee.setYear(DateUtils.getYear());
        }

        List<PropertyFee> propertyFees = propertyFeeMapper.selectPropertyFeeList(propertyFee);
        if(propertyFees!=null && propertyFees.size()>0){
            for (PropertyFee fee : propertyFees) {
                if("Y".equals(fee.getIsExpense())){
                    yjwyf = yjwyf.add(fee.getTenementExpense());
                    yjcwf = yjcwf.add(fee.getParkingExpense());
                    yjnhf = yjnhf.add(fee.getEnergyExpense());
                }else{
                    wjwyf = wjwyf.add(fee.getTenementExpense());
                    wjcwf = wjcwf.add(fee.getParkingExpense());
                    wjnhf = wjnhf.add(fee.getEnergyExpense());
                }
            }
        }
        map.put("yjwyf",yjwyf);
        map.put("wjwyf",wjwyf);
        map.put("yjcwf",yjcwf);
        map.put("wjcwf",wjcwf);
        map.put("yjnhf",yjnhf);
        map.put("wjnhf",wjnhf);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult CallPayment(Long houseId) {
        //查询户主信息
        ResidentInfo residentInfo = new ResidentInfo();
        residentInfo.setHouseId(houseId);
        residentInfo.setIsHouseholder("Y");
        List<ResidentInfo> residentInfos = residentInfoMapper.selectResidentInfoList(residentInfo);
        if(residentInfos!=null && residentInfos.size()>0){
            //todo 拿到户主手机号发送短信

            return AjaxResult.success("催缴成功，已通知户主缴费");
        }
        return AjaxResult.error("催缴失败，当前房屋无户主信息");
    }

    @Override
    public AjaxResult CallPaymentAll(PropertyFee propertyFee) {
        //查询所有未缴费信息
        propertyFee.setIsExpense("N");
        List<PropertyFee> propertyFees = propertyFeeMapper.selectPropertyFeeList(propertyFee);
        //todo 通过房屋id拿到户主手机号发送短信


        return AjaxResult.success("催缴成功，已通知户主缴费");
    }
}
