<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cool.store.mapper.tp.TpApplyFormMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.tp.TpApplyFormDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="type" jdbcType="BIT" property="type" />
    <result column="apply_no" jdbcType="VARCHAR" property="applyNo" />
    <result column="store_id" jdbcType="VARCHAR" property="storeId" />
    <result column="rule_id" jdbcType="BIGINT" property="ruleId" />
    <result column="rule_no" jdbcType="VARCHAR" property="ruleNo" />
    <result column="problem_classification" jdbcType="VARCHAR" property="problemClassification" />
    <result column="project_category" jdbcType="VARCHAR" property="projectCategory" />
    <result column="project_name" jdbcType="VARCHAR" property="projectName" />
    <result column="is_full" jdbcType="BIT" property="isFull" />
    <result column="score" jdbcType="DECIMAL" property="score" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="appeal_end_date" jdbcType="TIMESTAMP" property="appealEndDate" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="pay_status" jdbcType="BIT" property="payStatus" />
    <result column="is_draft" jdbcType="BIT" property="isDraft" />
    <result column="source" jdbcType="TINYINT" property="source" />
    <result column="apply_user_id" jdbcType="VARCHAR" property="applyUserId" />
    <result column="apply_user_name" jdbcType="VARCHAR" property="applyUserName" />
    <result column="approve_user_id" jdbcType="VARCHAR" property="approveUserId" />
    <result column="approve_time" jdbcType="TIMESTAMP" property="approveTime" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="proof_urls" jdbcType="LONGVARCHAR" property="proofUrls" />
    <result column="penalty_id" jdbcType="BIGINT" property="penaltyId" />
    <result column="applicant_name" jdbcType="VARCHAR" property="applicantName" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="appeal_reason" jdbcType="VARCHAR" property="appealReason" />
    <result column="appeal_detail_reason" jdbcType="VARCHAR" property="appealDetailReason" />
  </resultMap>

    <sql id="Base_Column_List">
        id, type, apply_no, store_id, rule_id, rule_no, problem_classification, project_category, project_name,
      is_full, score, remark, amount, appeal_end_date, status, pay_status, is_draft, source, apply_user_id,
      apply_user_name, approve_user_id, approve_time, create_time, update_time, deleted, proof_urls,
      penalty_id, applicant_name, phone, appeal_reason, appeal_detail_reason
    </sql>

    <sql id="Base_Column_List_A">
        a.id,a.type,a.apply_no,a.store_id,a.rule_id,a.rule_no,a.problem_classification,a.project_category,
        a.project_name,a.is_full,a.score,a.remark,a.amount,a.appeal_end_date,a.status,a.pay_status,a.is_draft,
        a.source,a.apply_user_id,a.apply_user_name,a.approve_user_id,a.approve_time,a.create_time,a.update_time,
        a.deleted,a.proof_urls,a.penalty_id,a.applicant_name,a.phone,a.appeal_reason,a.appeal_detail_reason
    </sql>

    <select id="getEffectiveList" parameterType="com.cool.store.request.tp.TpApplyQueryRequest" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List_A" />
        FROM zxjp_tp_apply_form a
        <if test="storeNameOrNum != null and storeNameOrNum != ''">
            INNER JOIN store_${enterpriseId} b ON a.store_id = b.store_id AND b.is_delete = 'effective'
        </if>
        <where>
            deleted = 0
            <if test="applyNo != null and applyNo != ''">
                AND a.apply_no LIKE CONCAT('%', #{applyNo}, '%')
            </if>
            <if test="projectCategory != null and projectCategory != ''">
                AND a.project_category = #{projectCategory}
            </if>
            <if test="ruleId != null and ruleId != ''">
                AND a.rule_id = #{ruleId}
            </if>
            <if test="statusList != null and !statusList.isEmpty()">
                AND a.status IN
                <foreach collection="statusList" item="status" separator="," open="(" close=")">
                    #{status}
                </foreach>
            </if>
            <if test="type != null">
                AND a.type = #{type}
            </if>
            <if test="payStatus != null">
                AND a.pay_status = #{payStatus}
            </if>
            <if test="score != null">
                AND a.score = #{score}
            </if>
            <if test="isPenalty != null and isPenalty">
                AND (a.type = 1 OR a.type = 2)
            </if>
            <if test="storeNameOrNum != null and storeNameOrNum != ''">
                AND (b.store_name LIKE CONCAT('%', #{storeNameOrNum}, '%')
                OR b.store_num LIKE CONCAT('%', #{storeNameOrNum}, '%'))
            </if>
            <if test="appealReason != null and appealReason != ''">
                AND a.appeal_reason LIKE CONCAT('%', #{appealReason}, '%')
            </if>
            <if test="isDraft != null">
                AND a.is_draft = #{isDraft}
            </if>
            <if test="overdue != null and overdue">
                AND a.appeal_end_date &lt; CURRENT_DATE
            </if>
            <if test="startTime != null">
                AND a.create_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND a.create_time &lt;= #{endTime}
            </if>
            <if test="penaltyStatus != null and penaltyStatus == 1">
                AND (a.status = 'pass' OR a.status = 'ineffective') AND (a.pay_status = 0 OR a.pay_status = 1)
            </if>
            <if test="penaltyStatus != null and penaltyStatus == 2">
                AND (a.status = 'effective' AND (a.pay_status = 0 OR a.pay_status = 2) OR a.status = 'cancel')
            </if>
            <if test="storeId != null and storeId != ''">
                AND a.store_id = #{storeId}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

</mapper>