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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
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.PropertyPrepayment;
import com.yuchen.saas.park.api.vo.property.PropertyPrepaymentBalanceVo;
import com.yuchen.saas.park.api.vo.property.PropertyPrepaymentDetailListVo;
import com.yuchen.saas.park.api.vo.property.PropertyPrepaymentListVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface PropertyPrepaymentMapper extends BaseMapper<PropertyPrepayment> {

    /**
     * 预存款余额查询
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT p1.park_id,p1.customer_id,p1.house_id,p1.fee_item_id, (p1.amt - IFNULL(p2.amt,0)) as balance_amt_fen from " +
            " (" +
            " SELECT park_id,customer_id,house_id,fee_item_id,SUM(amt_fen) as amt " +
            " from property_prepayment " +
            " where record_type = 1 and `status` = 1" +
            " GROUP BY park_id,customer_id,house_id,fee_item_id,record_type" +
            " ) p1" +
            " LEFT JOIN (" +
            " SELECT park_id,customer_id,house_id,fee_item_id,SUM(amt_fen) as amt " +
            " from property_prepayment " +
            " where record_type = 2" +
            " GROUP BY park_id,customer_id,house_id,fee_item_id,record_type" +
            " ) p2 on p1.park_id = p2.park_id and p1.customer_id = p2.customer_id " +
            " and p1.house_id = p2.house_id and p1.fee_item_id =p2.fee_item_id" +
            " ${ew.customSqlSegment}")
    Page<PropertyPrepaymentBalanceVo> selectBalancePage(Page<PropertyPrepaymentBalanceVo> page, @Param(Constants.WRAPPER) Wrapper<String> wrapper);


    /**
     * 收支明细
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT p.id,p.park_id,p.customer_id,p.house_id,h.resource_code,h.house_name,p.fee_item_id,fi.fee_item_name" +
            " ,p.record_time,p.record_type,p.amt_fen,p.is_refund,prai.pay_type,prai.refund_type,prai.create_user,p.remark" +
            " from property_prepayment p" +
            " LEFT JOIN property_payment_refund_account_info prai on p.id = prai.retrace_id" +
            " LEFT JOIN property_houses h on p.house_id = h.id" +
            " LEFT JOIN property_fee_item fi on p.fee_item_id = fi.id" +
            " ${ew.customSqlSegment}")
    Page<PropertyPrepaymentListVo> selectPrepaymentPage(Page<PropertyPrepaymentListVo> page, @Param(Constants.WRAPPER) Wrapper<String> wrapper);

    /**
     * 预存款核销明细
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT pd.id,p.house_id,h.resource_code,fi.id as fee_item_id,fi.fee_item_name,pd.need_pay_amt_fen,pd.deduct_amt_fen" +
            " ,p.record_time,b.start_date,b.end_date from property_prepayment_detail pd " +
            " LEFT JOIN property_prepayment p on pd.prepayment_id = p.id " +
            " LEFT JOIN property_houses h on p.house_id = h.id " +
            " LEFT JOIN property_fee_item fi on p.fee_item_id = fi.id " +
            " LEFT JOIN property_bill b on pd.bill_id = b.id" +
            " ${ew.customSqlSegment}")
    Page<PropertyPrepaymentDetailListVo> prepaymentDetailList(Page<PropertyPrepaymentDetailListVo> page, @Param(Constants.WRAPPER) Wrapper<String> wrapper);

 }
