<?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.CloseStoreAuditRecordMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.CloseStoreAuditRecordDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="close_store_id" jdbcType="BIGINT" property="closeStoreId" />
    <result column="record_type" jdbcType="TINYINT" property="recordType" />
    <result column="action_remark" jdbcType="VARCHAR" property="actionRemark" />
    <result column="handler_user_id" jdbcType="VARCHAR" property="handlerUserId" />
    <result column="handler_user_name" jdbcType="VARCHAR" property="handlerUserName" />
    <result column="handler_user_ids" jdbcType="VARCHAR" property="handlerUserIds" />
    <result column="receive_task_time" jdbcType="TIMESTAMP" property="receiveTaskTime" />
    <result column="finish_task_time" jdbcType="TIMESTAMP" property="finishTaskTime" />
    <result column="audit_status" jdbcType="TINYINT" property="auditStatus" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" />
  </resultMap>


  <select id="getRecordListByCloseStoreId" resultMap="BaseResultMap">
    select
    *
    from zxjp_close_store_audit_record
    where close_store_id = #{closeStoreId}
  </select>

  <select id="getCloseStoreAuditList" resultType="com.cool.store.vo.close.store.CloseStoreInfoListVO">
    SELECT a.id audit_record_id, a.close_store_id, b.plan_close_data, b.close_type, b.close_status, b.close_store_no, b.store_id
    <if test="request.queryStore">
        , c.store_name, c.store_num, c.join_brand, c.store_address
    </if>
    <if test="request.querySigner">
        , d.signer1_name, d.signer1_mobile, d.signer2_name, d.signer2_mobile
    </if>
    FROM zxjp_close_store_audit_record a
    LEFT JOIN zxjp_close_store_info b ON a.close_store_id = b.id
    <if test="request.queryStore">
        LEFT JOIN store_${enterpriseId} c ON b.store_id = c.store_id
    </if>
    <if test="request.querySigner">
        LEFT JOIN store_master_signer_info_${enterpriseId} d ON b.store_id = d.store_id
    </if>
    <where>
        <if test="isAdmin == null or !isAdmin">
            AND handler_user_ids LIKE CONCAT('%,', #{userId}, ',%')
        </if>
        <if test="request.auditStatus != null">
            AND a.audit_status = #{request.auditStatus}
        </if>
        <if test="request.closeType != null">
            AND b.close_type = #{request.closeType}
        </if>
        <if test="request.closeStatus != null">
            <choose>
                <when test="request.closeStatus == 1">
                    AND (b.close_status = 1 OR b.close_status = 2)
                </when>
                <otherwise>
                    AND b.close_status = #{request.closeStatus}
                </otherwise>
            </choose>
        </if>
        <if test="request.closeStoreNo != null and request.closeStoreNo != ''">
            AND b.close_store_no LIKE CONCAT('%', #{request.closeStoreNo}, '%')
        </if>
        <if test="request.queryStore and request.storeNameOrNum != null and request.storeNameOrNum != ''">
            AND (c.store_name LIKE CONCAT('%', #{request.storeNameOrNum}, '%') OR c.store_num LIKE CONCAT('%', #{request.storeNameOrNum}, '%'))
        </if>
        <if test="request.queryStore and request.joinBrands != null and !request.joinBrands.isEmpty()">
            AND c.join_brand IN
            <foreach item="item" collection="request.joinBrands" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="request.querySigner and request.signerName != null and request.signerName != ''">
            AND (d.signer1_name LIKE CONCAT('%', #{request.signerName}, '%') OR d.signer2_name LIKE CONCAT('%', #{request.signerName}, '%'))
        </if>
        <if test="request.querySigner and request.signerMobile != null and request.signerMobile != ''">
            AND (d.signer1_mobile LIKE CONCAT('%', #{request.signerMobile}, '%') OR d.signer2_mobile LIKE CONCAT('%', #{request.signerMobile}, '%'))
        </if>
        AND a.deleted = 0
    </where>
    ORDER BY b.create_time DESC
  </select>
</mapper>