<?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.tmerclub.cloud.multishop.mapper.ShopAuditingMapper">
  <resultMap id="shopAuditingMap" type="com.tmerclub.cloud.multishop.model.ShopAuditing">
    <id column="shop_auditing_id" property="shopAuditingId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="user_id" property="userId"/>
    <result column="shop_id" property="shopId"/>
    <result column="auditor_id" property="auditorId"/>
    <result column="status" property="status"/>
    <result column="remarks" property="remarks"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `shop_auditing_id`,`create_time`,`update_time`,`user_id`,`shop_id`,`auditor_id`,`status`,`remarks`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.multishop.vo.ShopAuditingVO">
    select <include refid="Vo_Column_List"/> from shop_auditing order by shop_auditing_id desc
  </select>
  <select id="getByShopAuditingId" resultType="com.tmerclub.cloud.multishop.vo.ShopAuditingVO">
    select <include refid="Vo_Column_List"/> from shop_auditing where shop_auditing_id = #{shopAuditingId}
  </select>
  <insert id="save">
    insert into shop_auditing (`user_id`,`shop_id`,`auditor_id`,`status`,`remarks`)
    values (#{shopAuditing.userId},#{shopAuditing.shopId},#{shopAuditing.auditorId},#{shopAuditing.status},#{shopAuditing.remarks});
  </insert>
  <update id="update">
    update shop_auditing
    <set>
      <if test="shopAuditing.userId != null">
        `user_id` = #{shopAuditing.userId},
      </if>
      <if test="shopAuditing.shopId != null">
        `shop_id` = #{shopAuditing.shopId},
      </if>
      <if test="shopAuditing.auditorId != null">
        `auditor_id` = #{shopAuditing.auditorId},
      </if>
      <if test="shopAuditing.status != null">
        `status` = #{shopAuditing.status},
      </if>
      <if test="shopAuditing.remarks != null">
        `remarks` = #{shopAuditing.remarks},
      </if>
      <if test="shopAuditing.updateTime != null">
        `update_time` = #{shopAuditing.updateTime},
      </if>
    </set>
    where shop_auditing_id = #{shopAuditing.shopAuditingId}
  </update>
  <delete id="deleteById">
    delete from shop_auditing where shop_auditing_id = #{shopAuditingId}
  </delete>
  <select id="getByshopId" resultType="com.tmerclub.cloud.multishop.vo.ShopAuditingVO">
    select <include refid="Vo_Column_List"/> from shop_auditing where shop_id = #{shopId}
  </select>
  <select id="auditingInfoList" resultType="com.tmerclub.cloud.multishop.vo.ShopAuditingInfoVO">
    SELECT  sa.shop_auditing_id,sa.user_id,sa.`status`,
            sd.shop_id,sd.shop_name,sd.intro,sd.shop_logo,sd.intro,sd.shop_status,sd.notice_mobile,
            IF(sa.update_time IS NULL,sd.create_time,sa.update_time) AS create_time
    FROM shop_detail sd
    LEFT JOIN shop_auditing sa ON sa.shop_id = sd.shop_id
        <trim prefix="where" prefixOverrides="and | or" >
          <if test="auditingInfo.shopAuditingId != null">
            and sa.shop_auditing_id = #{auditingInfo.shopAuditingId}
          </if>
          <if test="auditingInfo.status != null">
            and sa.status = #{auditingInfo.status}
          </if>
          <if test="auditingInfo.intro != null">
            and sd.intro like CONCAT('%',#{auditingInfo.intro},'%')
          </if>
          <if test="auditingInfo.beginTime != null and auditingInfo.endTime != null">
            and date(sa.create_time) between #{auditingInfo.beginTime} and #{auditingInfo.endTime}
          </if>
          <if test="auditingInfo.shopStatus != null">
            and sd.shop_status = #{auditingInfo.shopStatus}
          </if>
          <if test="auditingInfo.shopName != null and auditingInfo.shopName != ''">
            and sd.shop_name like CONCAT('%',#{auditingInfo.shopName},'%')
          </if>
            and sd.shop_status != 1
        </trim>
    ORDER BY sd.create_time DESC
  </select>
  <update id="updateToFail">
    update shop_auditing set status = -1
    where status = 0 and shop_id in
    <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
      #{shopId}
    </foreach>
  </update>
</mapper>
