<?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.CompanyAuditingMapper">
    <sql id="Vo_Column_List">
        `company_auditing_id`,`user_id`,`shop_id`,`shop_company_id`,`auditor_id`,`create_time`,`update_time`,`status`,`remarks`
    </sql>
    <select id="list" resultType="com.tmerclub.cloud.multishop.vo.ShopCompanyAuditingVO">
        SELECT ca.`company_auditing_id`, ca.`user_id`, ca.`shop_id`, ca.`shop_company_id`, ca.`auditor_id`,
        ca.`create_time`, ca.`update_time`,ca.`status`, ca.`remarks`, sd.shop_name, sd.intro
        FROM company_auditing ca
        JOIN shop_detail sd ON ca.shop_id = sd.shop_id
        WHERE ca.status = 0 AND sd.shop_status != -1 AND sd.shop_status != 3 AND sd.shop_status != 4
        <if test="shopCompanyAuditingDTO.shopName != null and shopCompanyAuditingDTO.shopName != ''">
            and sd.shop_name like concat('%', #{shopCompanyAuditingDTO.shopName}, '%')
        </if>
        <if test="shopCompanyAuditingDTO.beginTime != null and shopCompanyAuditingDTO.endTime != null">
            and ca.create_time between #{shopCompanyAuditingDTO.beginTime} and #{shopCompanyAuditingDTO.endTime}
        </if>
        order by ca.update_time desc
    </select>
    <update id="update">
        update company_auditing
        <set>
            <if test="shopCompanyAuditing.userId != null">
                `user_id` = #{shopCompanyAuditing.userId},
            </if>
            <if test="shopCompanyAuditing.shopId != null">
                `shop_id` = #{shopCompanyAuditing.shopId},
            </if>
            <if test="shopCompanyAuditing.auditorId != null">
                `auditor_id` = #{shopCompanyAuditing.auditorId},
            </if>
            <if test="shopCompanyAuditing.status != null">
                `status` = #{shopCompanyAuditing.status},
            </if>
            <if test="shopCompanyAuditing.remarks != null">
                `remarks` = #{shopCompanyAuditing.remarks},
            </if>
        </set>
        where company_auditing_id = #{shopCompanyAuditing.companyAuditingId}
    </update>
    <select id="getById" resultType="com.tmerclub.cloud.multishop.model.ShopCompanyAuditing">
        select
        <include refid="Vo_Column_List"/>
        from company_auditing
        where company_auditing_id = #{shopCompanyAuditingId}
    </select>
    <select id="getLatestAuditingByShopId" resultType="com.tmerclub.cloud.multishop.vo.ShopCompanyAuditingVO">
        select ca.`company_auditing_id`,
               ca.`user_id`,
               ca.`shop_id`,
               ca.`shop_company_id`,
               ca.`auditor_id`,
               ca.`create_time`,
               ca.`update_time`,
               ca.`status`,
               ca.`remarks`,
               sd.shop_name,
               sd.intro
        from company_auditing ca
                 join shop_detail sd on ca.shop_id = sd.shop_id
        where ca.shop_id = #{shopId}
        order by create_time desc
        limit 1
    </select>
    <insert id="insert" useGeneratedKeys="true" keyProperty="companyAuditingId">
        insert into company_auditing(`user_id`, `shop_id`, `shop_company_id`, `auditor_id`, `status`, `remarks`)
        values (#{shopCompanyAuditing.userId}, #{shopCompanyAuditing.shopId}, #{shopCompanyAuditing.shopCompanyId},
                #{shopCompanyAuditing.auditorId}, #{shopCompanyAuditing.status}, #{shopCompanyAuditing.remarks});
    </insert>
    <delete id="deleteById">
        delete
        from company_auditing
        where company_auditing_id = #{shopCompanyAuditingId}
    </delete>
    <select id="getNeedAuditList" resultType="com.tmerclub.cloud.multishop.vo.ShopCompanyAuditingVO">
        select `company_auditing_id`,`shop_id`,`shop_company_id`
        from company_auditing where status = 0
    </select>
</mapper>
