<?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.dgtly.companyext.mapper.CompanyExamineMapper">
    
    <resultMap type="CompanyExamine" id="CompanyExamineResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="companyType"    column="company_type"    />
        <result property="detailAddress"    column="detail_address"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="legalPerson"    column="legal_person"    />
        <result property="contactNumber"    column="contact_number"    />
        <result property="establishDate"    column="establish_date"    />
        <result property="unifiedSocialCreditCode"    column="unified_social_credit_code"    />
        <result property="businessLicenseUrl"    column="business_license_url"    />
        <result property="defaultImgUrl"    column="default_img_url"    />
        <result property="propagandaImgUrl"    column="propaganda_img_url"    />
        <result property="companyIntroduce"    column="company_introduce"    />
        <result property="versionNum"    column="version_num"    />
        <result property="status"    column="status"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="examineDate"    column="examine_date"    />
        <result property="rejectReason"    column="reject_reason"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCompanyExamineVo">
        select id, company_id, status, apply_date, examine_date, reject_reason, create_by, create_time, update_by, update_time, remark from company_examine
    </sql>

    <select id="selectCompanyExamineList" parameterType="CompanyExamine" resultMap="CompanyExamineResult">
        SELECT
        ce.id,
        ce.company_id,
        cei.company_name,
        cei.company_type,
        cei.legal_person,
        cei.contact_number,
        ce.STATUS,
        date_format(ce.apply_date,'%Y-%m-%d') as 'apply_date',
        date_format(ce.examine_date,'%Y-%m-%d') as 'examine_date',
        ce.reject_reason
        FROM
        company_examine ce,company_ext_info cei
        WHERE ce.company_id = cei.company_id AND ce.del_flag  = '0' AND cei.del_flag = '0'
        AND cei.version_num = (SELECT MAX(a.version_num) FROM company_ext_info a WHERE a.company_id = ce.company_id)
        <if test="companyName != null and companyName != ''"> and cei.company_name like concat('%', #{companyName}, '%')</if>
        <if test="companyType != null and companyType != ''"> and cei.company_type = #{companyType}</if>
        <if test="legalPerson != null and legalPerson != ''"> and cei.legal_person like concat('%', #{legalPerson}, '%')</if>
        <if test="status != null and status != 3"> and ce.status = #{status}</if>
        <if test="status != null and status == 3"> and (ce.status = 1 or ce.status = 2)</if>
        <if test="applyDate != null and applyDate != ''"> and date_format(ce.apply_date,'%Y-%m-%d') = #{applyDate}</if>
        <if test="examineDate != null and examineDate != ''"> and date_format(ce.examine_date,'%Y-%m-%d') = #{examineDate}</if>
    </select>

    <select id="selectCompanyExamineById" parameterType="Long" resultMap="CompanyExamineResult">
         SELECT
            ce.id,
            ce.company_id,
            cei.company_name,
            cei.company_type,
            cei.legal_person,
            cei.detail_address,
            cei.longitude,
            cei.latitude,
            cei.legal_person,
            cei.contact_number,
            date_format(cei.establish_date,'%Y-%m-%d') as 'establish_date',
            cei.unified_social_credit_code,
            cei.business_license_url,
            cei.default_img_url,
            cei.propaganda_img_url,
            cei.company_introduce,
            cei.version_num,
            cei.contact_number,
            ce.STATUS,
            ce.apply_date,
            ce.examine_date,
            ce.reject_reason
        FROM
            company_examine ce
        LEFT JOIN	company_ext_info cei on ce.company_id = cei.company_id
        WHERE
        ce.del_flag = '0'
        AND cei.del_flag = '0'
        AND cei.version_num = (
            SELECT
                MAX(a.version_num)
            FROM
                company_ext_info a
            WHERE
                a.company_id = ce.company_id
        )
        AND ce.id = #{id}
    </select>
        
    <insert id="insertCompanyExamine" parameterType="CompanyExamine" useGeneratedKeys="true" keyProperty="id">
        insert into company_examine
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null ">company_id,</if>
            <if test="status != null ">status,</if>
            <if test="applyDate != null ">apply_date,</if>
            <if test="examineDate != null ">examine_date,</if>
            <if test="rejectReason != null  and rejectReason != ''">reject_reason,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="remark != null  and remark != ''">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null ">#{companyId},</if>
            <if test="status != null ">#{status},</if>
            <if test="applyDate != null ">#{applyDate},</if>
            <if test="examineDate != null ">#{examineDate},</if>
            <if test="rejectReason != null  and rejectReason != ''">#{rejectReason},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="remark != null  and remark != ''">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCompanyExamine" parameterType="CompanyExamine">
        update company_examine
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null ">status = #{status},</if>
            examine_date = NOW(),
            <if test="rejectReason != null  and rejectReason != ''">reject_reason = #{rejectReason},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            update_time = NOW(),
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyExamineById" parameterType="Long">
        delete from company_examine where id = #{id}
    </delete>

    <delete id="deleteCompanyExamineByIds" parameterType="String">
        delete from company_examine where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
</mapper>