package com.yuchen.saas.park.api.entity.property;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yuchen.saas.park.api.enums.property.YesOrNoEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Data
@TableName("property_bill")
@ApiModel(value = "物业账单表", description = "物业账单表")
public class PropertyBill implements Serializable {
    /**
     * id主键
     */
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(name = "id主键", notes = "")
    private Long id;
    /**
     * 园区id
     */
    @ApiModelProperty(name = "园区id", notes = "")
    private Long parkId;

    @ApiModelProperty(name = "楼栋id", notes = "")
    private Long buildingId;

    /**
     * 房源id
     */
    @ApiModelProperty(name = "房源id", notes = "")
    private Long houseId;
    /**
     * 收费项目id
     */
    @ApiModelProperty(name = "收费项目id", notes = "")
    private Long feeItemId;
    /**
     * 收票标准id
     */
    @ApiModelProperty(name = "收票标准id", notes = "")
    private Long feeId;
    /**
     * 账单状态;1-未出账单，2-已出账单，3-冻结账单，4-已结账单
     * BillStatusEnum
     */
    @ApiModelProperty(name = "是否出账", notes = "0-未出账单，1-已出账单")
    private Integer billStatus;
    /**
     * 缴费状态 1-未收费，2-部份收费，3-已欠费，4-已收费
     * PayStatusEnum
     */
    @ApiModelProperty(name = "缴费状态", notes = "")
    private Integer payStatus;
    /**
     * 记录账单生成时归属的客户
     */
    @ApiModelProperty(name = "客户id", notes = "")
    private Long customerId;
    /**
     * 价格;单位：分
     */
    @ApiModelProperty(name = "价格", notes = "单位：分")
    private Long priceFen;
    /**
     * 计价单位
     */
    @ApiModelProperty(name = "计价单位", notes = "")
    private Integer priceUnitType;
    /**
     * 应收金额;单位：分
     */
    @ApiModelProperty(name = "应收金额", notes = "单位：分")
    private long receivableAmtFen;
    /**
     * 计费数量
     */
    private BigDecimal billableQuantity;
    /**
     * 账单开始时间
     */
    @ApiModelProperty(name = "账单开始时间", notes = "")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    private Date startDate;
    /**
     * 账单结束时间
     */
    @ApiModelProperty(name = "账单结束时间", notes = "")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    private Date endDate;
    /**
     * 应收日期 (如果为空，给个默认值 2999-01-01)
     */
    @ApiModelProperty(name = "应收日期", notes = "")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    private Date receivableDate;
    public Date getReceivableDate() {
        if (this.receivableDate == null) {
            return DateUtil.parseDate("2999-01-01");
        }
        if (DateUtil.compare(DateUtil.parseDate("2999-01-01"),this.receivableDate) == 0) {
            return null;
        }
        return this.receivableDate;
    }

    /**
     * 仪表名称
     */
    private String meterFullName;
    /**
     * 上次抄表读数
     */
    private BigDecimal lastMeterReadingNum;
    /**
     * 本次抄表读数
     */
    private BigDecimal meterReadingNum;
    /**
     * 仪表单位
     */
    private String meterUnit;

    /**
     * 违约金计算起始日
     */
    @ApiModelProperty(name = "违约金计算起始日", notes = "")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    private Date breachDate;
    /**
     * 直接减免金额;单位：分
     */
    @ApiModelProperty(name = "直接减免金额", notes = "单位：分")
    private long directReductionAmtFen;
    /**
     * 缴费减免金额;单位：分
     */
    @ApiModelProperty(name = "缴费减免金额", notes = "单位：分")
    private long payReductionAmtFen;
    /**
     * 优惠券减免金额;单位：分
     */
    @ApiModelProperty(name = "优惠券减免金额", notes = "单位：分")
    private long couponAmtFen;
    /**
     * 应收滞纳金;单位：分
     */
    @ApiModelProperty(name = "应收滞纳金", notes = "单位：分")
    private long breachAmtFen;
    /**
     * 增减滞纳金; 1-减，0-增
     */
    private int isReduceBreach;
    /**
     * 减免的滞纳金;单位：分
     */
    @ApiModelProperty(name = "减免的滞纳金", notes = "单位：分")
    private long reductionBreachAmtFen;
    /**
     * 增减调整金额; 1-减，0-增
     */
    private int isReduceReadjust;
    /**
     * 调整金额;单位：分
     */
    @ApiModelProperty(name = "调整金额", notes = "单位：分")
    private long readjustAmtFen;
    /**
     * 税费金额;单位：分
     */
    @ApiModelProperty(name = "税费金额", notes = "单位：分")
    private long taxAmtFen;
    /**
     * 已缴金额;单位：分
     */
    @ApiModelProperty(name = "已缴金额", notes = "单位：分")
    private long payAmtFen;
    /**
     * 退款金额;单位：分
     */
    private long refundAmtFen;
    /**
     * 缴费时间
     */
    private Date payTime;
    /**
     * 缴费类型
     * BillPayTypeEnum
     */
    private Integer payType;
    /**
     * 账单生成类型;1-系统周期性生成，2-抄表生成，3-交押金，4-预收，5-临时，6-账单拆分，7-固定金额分摊，8-经营性收费
     */
    @ApiModelProperty(name = "账单生成类型", notes = "1-系统周期性生成，2-抄表生成，3-交押金，4-预收，5-临时，6-账单拆分，7-固定金额分摊，8-经营性收费")
    private Integer createBillType;
    /**
     * 出账时间
     */
    @ApiModelProperty(name = "出账时间", notes = "")
    private Date outBillTime;

    /**
     * 追溯id
     */
    private Long retraceId;
    /**
     * 账单是否待审核; 1-是，0-否
     */
    private int isProcess;
    /**
     * 是否删除，1-是，0-否
     */
    @TableLogic
    private Integer isDeleted;
    /**
     * 乐观锁
     */
    @ApiModelProperty(name = "乐观锁", notes = "")
    private Integer revision;
    /**
     * 创建人
     */
    @ApiModelProperty(name = "创建人", notes = "")
    private Long createUser;
    /**
     * 创建时间
     */
    @ApiModelProperty(name = "创建时间", notes = "")
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty(name = "更新人", notes = "")
    private Long updateUser;
    /**
     * 更新时间
     */
    @ApiModelProperty(name = "更新时间", notes = "")
    private Date updateTime;


    /**
     * 合计应收金额; 单位：分
     * 合计应收金额 = 应收金额 + 税费 + 应收滞纳金 - 直接减免金额 + 减免滞纳金（有加减） + 调整优惠（有加减）
     */
    @TableField(exist = false)
    private long totalPayAmtFen;
    public long getTotalPayAmtFen() {
        // 合计应收金额 = 应收金额 + 税费 + 应收滞纳金 - 直接减免金额 + 减免滞纳金（有正负） + 调整优惠（有正负）
        // 合计应收金额是需要给用户看到的缴费金额，所以没算上缴费优惠
       long amt = this.receivableAmtFen + this.taxAmtFen + this.breachAmtFen - this.directReductionAmtFen + this.getSignedReductionBreachAmtFen() + this.getSignedReadjustAmtFen();
       return amt < 0 ? 0 : amt;
    }

    /**
     * 未缴金额;单位：分
     * 未缴金额 = 合计应收金额 - 实付金额 - 支付优惠金额
     */
    @TableField(exist = false)
    private long noPayAmtFen;
    public long getNoPayAmtFen() {
        long amt = this.getTotalPayAmtFen() - this.payAmtFen - this.payReductionAmtFen - this.couponAmtFen;
        return amt < 0 ? 0 : amt;
    }

    /**
     * 减免的滞纳金（带正负的）
     */
    @TableField(exist = false)
    private long signedReductionBreachAmtFen;
    public long getSignedReductionBreachAmtFen() {
        if (this.isReduceBreach == YesOrNoEnum.YES.getValue()) {
            return this.reductionBreachAmtFen * -1;
        }
        return this.reductionBreachAmtFen;
    }

    /**
     * 费用调整金额（带正负的）
     */
    @TableField(exist = false)
    private long signedReadjustAmtFen;
    public long getSignedReadjustAmtFen() {
        if (this.isReduceReadjust == YesOrNoEnum.YES.getValue()) {
            return this.readjustAmtFen * -1;
        }
        return this.readjustAmtFen;
    }


    /**计费整月数*/
    @TableField(exist = false)
    private Long calcCycleMonths;
    /**计费天数*/
    @TableField(exist = false)
    private Long calcCycleDays;


}
