package com.yuchen.saas.park.service.mapper.property;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.property.PropertyBill;
import com.yuchen.saas.park.api.vo.property.PropertyAppPayBilListVo;
import com.yuchen.saas.park.api.vo.property.PropertyBillReductionListVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface PropertyBillMapper extends BaseMapper<PropertyBill> {


    /**
     * 计算未缴费金额字段
     */
    String NO_PAY_AMT_FEN = " (receivable_amt_fen + tax_amt_fen + breach_amt_fen - direct_reduction_amt_fen + IF(is_reduce_breach=1,reduction_breach_amt_fen * -1,reduction_breach_amt_fen) + IF(is_reduce_readjust=1,readjust_amt_fen * -1,readjust_amt_fen) - pay_amt_fen - pay_reduction_amt_fen - coupon_amt_fen) ";

    /**
     * APP 缴费账单查询 - 强制连续账单缴费
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT MAX(end_date) AS end_date, SUM(" + NO_PAY_AMT_FEN + ") AS no_pay_amt_fen FROM property_bill ${ew.customSqlSegment}")
    Page<PropertyAppPayBilListVo> selectPageAppBillAll(Page<PropertyAppPayBilListVo> page, @Param(Constants.WRAPPER) LambdaQueryWrapper<PropertyBill> wrapper);


    /**
     * APP 缴费账单查询 - 允许选择费用项目缴费
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT fee_item_id, MAX(end_date) AS end_date, SUM(" + NO_PAY_AMT_FEN + ") AS no_pay_amt_fen FROM property_bill ${ew.customSqlSegment}")
    Page<PropertyAppPayBilListVo> selectPageAppBillFeeItem(Page<PropertyAppPayBilListVo> page, @Param(Constants.WRAPPER) LambdaQueryWrapper<PropertyBill> wrapper);

    /**
     * 更新欠费账单状态
     * @return
     */
    @Update("UPDATE property_bill SET pay_status = 3 where pay_status in (1,2) and receivable_date < DATE(NOW())")
    int updateArrearsBill();


    @Select(" select a.fee_id as feeId, MAX(a.building_id) as building_id,a.house_id,MAX(b.park_name) as park_name,MAX(e.customer_name) as customer_name, " +
            " MAX(d.house_name) as house_name,MAX(d.resource_code) as resource_code,MAX(d.construction_area) as construction_area,  " +
            " SUM(direct_reduction_amt_fen) as reduction_amt_fen,'直接减免' as reduction_type   " +
            " from property_bill a  " +
            " left join property_houses d on a.house_id=d.id   " +
            " left join pmg_bat_park_info b on a.park_id=b.id  " +
            " left join property_house_enterprise c on c.house_id=d.id  " +
            " left join property_customer_archives e on c.customer_archives_id=e.id " +
            " ${ew.customSqlSegment}" +
            " group by a.house_id,a.fee_id " +
            " UNION ALL " +
            " select a.fee_id as feeId, MAX(a.building_id) as building_id,a.house_id,MAX(b.park_name) as park_name,MAX(e.customer_name) as customer_name, " +
            " MAX(d.house_name) as house_name,MAX(d.resource_code) as resource_code,MAX(d.construction_area) as construction_area,  " +
            " SUM(pay_reduction_amt_fen) as reduction_amt_fen,'缴费减免' as reduction_type   " +
            " from property_bill a  " +
            " left join property_houses d on a.house_id=d.id   " +
            " left join pmg_bat_park_info b on a.park_id=b.id  " +
            " left join property_house_enterprise c on c.house_id=d.id  " +
            " left join property_customer_archives e on c.customer_archives_id=e.id " +
            " ${ew.customSqlSegment} " +
            " group by a.house_id,a.fee_id " )
    Page<PropertyBillReductionListVo> billReductionPage(Page<PropertyBillReductionListVo> page, @Param(Constants.WRAPPER) Wrapper<String> wrapper);

}
