package com.clx.performance.sqlProvider.loan;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clx.performance.enums.loan.OwnerLoanAccountRunningWaterRecordEnum;
import com.clx.performance.param.pc.PageOwnerCaseOutListParam;
import com.clx.performance.param.pc.loan.carrier.PageOwnerLoanAccountWaterRecordOfOwnerParam;
import com.clx.performance.vo.pc.loan.carrier.OwnerLoanAccountRunningWaterRecordVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

public class OwnerLoanAccountRunningWaterRecordSqlProvider {

    public String pageByParam(@Param("page") Page<OwnerLoanAccountRunningWaterRecordVO> page, @Param("param") PageOwnerLoanAccountWaterRecordOfOwnerParam param) {
        return new SQL(){{

            SELECT(" id, owner_user_no as ownerUserNo, owner_user_name as ownerUserName, mobile, " +
                    " running_water_no as runningWaterNo, loan_no as loanNo, child_no as childNo, " +
                    " running_water_type as runningWaterType, " +
                    " alteration_balance as alterationBalance, use_able_balance as useAbleBalance, frozen_balance as frozenBalance, account_balance as accountBalance, " +
                    " create_by as createBy, " +
                    " date_format(create_time, '%Y-%m-%d %H:%i:%s') as createTime, " +
                    " date_format(modified_time, '%Y-%m-%d %H:%i:%s') as modifiedTime "
            );

            FROM("owner_loan_account_running_water_record");
            WHERE("owner_user_no = #{param.ownerUserNo}");

            if (StringUtils.isNotBlank(param.getChildNo())) {WHERE("child_no = #{param.childNo}");}
            if (StringUtils.isNotBlank(param.getRunningWaterNo())) {WHERE("running_water_no = #{param.runningWaterNo}");}
            if (Objects.nonNull(param.getRunningWaterType())) {WHERE("running_water_type = #{param.runningWaterType}");}

            ORDER_BY("id desc");
        }}.toString();
    }

    public String selectLoanRunningWatterRecordByLoanNoForRepay(@Param("loanNo") Long loanNo) {
        String inSql ="(select distinct child_no from owner_loan_account_running_water_record where loan_no = #{loanNo} and running_water_type =" + OwnerLoanAccountRunningWaterRecordEnum.RunWaterType.APPROVE_CHILD_WRITE_OFF.getCode() + ")";

        String sql = "select id, owner_user_no as ownerUserNo, owner_user_name as ownerUserName, mobile, " +
                " running_water_no as runningWaterNo, loan_no as loanNo, child_no as childNo, " +
                " running_water_type as runningWaterType, " +
                " alteration_balance as alterationBalance, use_able_balance as useAbleBalance, frozen_balance as frozenBalance, account_balance as accountBalance, " +
                " create_by as createBy, " +
                " date_format(create_time, '%Y-%m-%d %H:%i:%s') as createTime, " +
                " date_format(modified_time, '%Y-%m-%d %H:%i:%s') as modifiedTime  from  owner_loan_account_running_water_record where running_water_type = " + OwnerLoanAccountRunningWaterRecordEnum.RunWaterType.APPROVE_CHILD_WRITE_OFF.getCode()
                +" and child_no in " + inSql;
        return sql;
    }

}
