<?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.gic.haoban.manage.service.dao.mapper.TabHaobanAuditMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanAudit">
        <id column="audit_id" property="auditId" jdbcType="VARCHAR"/>
        <result column="audit_type" property="auditType" jdbcType="INTEGER"/>
        <result column="commit_name" property="commitName" jdbcType="VARCHAR"/>
        <result column="commit_staff_id" property="commitStaffId" jdbcType="VARCHAR"/>
        <result column="commit_staff_name" property="commitStaffName" jdbcType="VARCHAR"/>
        <result column="commit_staff_img" property="commitStaffImg" jdbcType="VARCHAR"/>
        <result column="commit_store_id" property="commitStoreId" jdbcType="VARCHAR"/>
        <result column="change_field" property="changeField" jdbcType="VARCHAR"/>
        <result column="old_value" property="oldValue" jdbcType="VARCHAR"/>
        <result column="new_value" property="newValue" jdbcType="VARCHAR"/>
        <result column="commit_time" property="commitTime" jdbcType="TIMESTAMP"/>
        <result column="enterprise_id" property="enterpriseId" jdbcType="VARCHAR"/>
        <result column="audit_name" property="auditName" jdbcType="VARCHAR"/>
        <result column="audit_status" property="auditStatus" jdbcType="INTEGER"/>
        <result column="audit_reason" property="auditReason" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="related_id" property="relatedId" jdbcType="VARCHAR"/>
        <result column="audit_staff_id" property="auditStaffId" jdbcType="VARCHAR"/>
        <result column="commit_store_name" property="commitStoreName"/>
    </resultMap>
    <sql id="Base_Column_List">
        audit_id
        , audit_type, commit_name, commit_staff_id, commit_staff_name, commit_staff_img,
    commit_store_id, change_field, old_value, new_value, commit_time, enterprise_id, 
    audit_name, audit_status, audit_reason, create_time, update_time, status_flag, wx_enterprise_id,related_id,audit_staff_id , commit_store_name
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where audit_id = #{auditId,jdbcType=VARCHAR}
    </select>

    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanAudit">
        insert into tab_haoban_audit (audit_id, audit_type, commit_name,
                                      commit_staff_id, commit_staff_name, commit_staff_img,
                                      commit_store_id, change_field, old_value,
                                      new_value, commit_time, enterprise_id,
                                      audit_name, audit_status, audit_reason,
                                      create_time, update_time, status_flag,
                                      wx_enterprise_id, related_id, audit_staff_id, commit_store_name)
        values (#{auditId,jdbcType=VARCHAR}, #{auditType,jdbcType=INTEGER}, #{commitName,jdbcType=VARCHAR},
                #{commitStaffId,jdbcType=VARCHAR}, #{commitStaffName,jdbcType=VARCHAR},
                #{commitStaffImg,jdbcType=VARCHAR},
                #{commitStoreId,jdbcType=VARCHAR}, #{changeField,jdbcType=VARCHAR}, #{oldValue,jdbcType=VARCHAR},
                #{newValue,jdbcType=VARCHAR}, #{commitTime,jdbcType=TIMESTAMP}, #{enterpriseId,jdbcType=VARCHAR},
                #{auditName,jdbcType=VARCHAR}, #{auditStatus,jdbcType=INTEGER}, #{auditReason,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{statusFlag,jdbcType=INTEGER},
                #{wxEnterpriseId,jdbcType=VARCHAR}, #{relatedId,jdbcType=VARCHAR}, #{auditStaffId,jdbcType=VARCHAR},
                #{commitStoreName})
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanAudit">
        update tab_haoban_audit
        <set>
            <if test="auditType != null">
                audit_type = #{auditType,jdbcType=INTEGER},
            </if>
            <if test="commitName != null">
                commit_name = #{commitName,jdbcType=VARCHAR},
            </if>
            <if test="commitStaffId != null">
                commit_staff_id = #{commitStaffId,jdbcType=VARCHAR},
            </if>
            <if test="commitStaffName != null">
                commit_staff_name = #{commitStaffName,jdbcType=VARCHAR},
            </if>
            <if test="commitStaffImg != null">
                commit_staff_img = #{commitStaffImg,jdbcType=VARCHAR},
            </if>
            <if test="commitStoreId != null">
                commit_store_id = #{commitStoreId,jdbcType=VARCHAR},
            </if>
            <if test="changeField != null">
                change_field = #{changeField,jdbcType=VARCHAR},
            </if>
            <if test="oldValue != null">
                old_value = #{oldValue,jdbcType=VARCHAR},
            </if>
            <if test="newValue != null">
                new_value = #{newValue,jdbcType=VARCHAR},
            </if>
            <if test="commitTime != null">
                commit_time = #{commitTime,jdbcType=TIMESTAMP},
            </if>
            <if test="enterpriseId != null">
                enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="auditName != null">
                audit_name = #{auditName,jdbcType=VARCHAR},
            </if>
            <if test="auditStatus != null">
                audit_status = #{auditStatus,jdbcType=INTEGER},
            </if>
            <if test="auditReason != null">
                audit_reason = #{auditReason,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="relatedId != null">
                related_id = #{relatedId,jdbcType=VARCHAR},
            </if>
            <if test="auditStaffId != null">
                audit_staff_id = #{auditStaffId,jdbcType=VARCHAR},
            </if>
        </set>
        where audit_id = #{auditId,jdbcType=VARCHAR}
    </update>

    <sql id="storeSql">
        <if test="null != storeIds and storeIds.size gt 0">
            and commit_store_id in
            <foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>
    <sql id="staffSql">
        <if test="null != staffIds and staffIds.size gt 0">
            and commit_staff_id in
            <foreach collection="staffIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>
    <select id="page" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where wx_enterprise_id=#{wxEnterpriseId} and enterprise_id = #{enterpriseId,jdbcType=VARCHAR} and status_flag = 1
        <if test="auditType != null">
            and audit_type = #{auditType,jdbcType=INTEGER}
        </if>
        <if test="auditFlag != null and auditFlag == 0">
            and audit_status = 0
        </if>
        <if test="auditFlag != null and auditFlag != 0">
            and audit_status in (1,2,4,5)
        </if>
        <if test="auditStatus != null">
            and audit_status = #{auditStatus,jdbcType=INTEGER}
        </if>
        <if test=" null != searchParams and  '' !=searchParams ">
            and (commit_staff_name like '%${searchParams}%' or commit_store_name like '%${searchParams}%')
        </if>
        <include refid="storeSql"/>
        order by commit_time desc
    </select>

    <select id="listByStoreId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where 1=1
        and commit_store_id = #{storeId,jdbcType=VARCHAR}
        and audit_status = 0
        and audit_type = 1
    </select>
    <select id="findByStoreIdAndChangeField" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where 1=1
        and commit_store_id = #{storeId,jdbcType=VARCHAR}
        and audit_status = 0
        and change_field = #{changeField,jdbcType=VARCHAR}
    </select>

    <select id="listByStaffId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where commit_staff_id = #{staffId,jdbcType=VARCHAR}
        and status_flag = 1
        <if test="auditType == null">
            and audit_type in (2,3,4,5)
        </if>
        <if test="auditType != null">
            and audit_type = #{auditType}
        </if>
        <if test="auditStatus != null">
            and audit_status = #{auditStatus}
        </if>
        <if test="enterpriseId != null">
            and enterprise_id = #{enterpriseId}
        </if>
        order by create_time desc
    </select>

    <select id="pageStoreListByParams" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where
        status_flag = 1
        <if test="clerkType==1">
            <if test="auditType == null || auditType == -1">
                and audit_type in (2,3,4,5)
            </if>
        </if>
        <if test="auditType != null and auditType != -1">
            and audit_type = #{auditType}
        </if>
        <if test="auditStatus != null and auditStatus != -1">
            and audit_status = #{auditStatus}
        </if>
        <if test="wxEnterpriseId != null">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="enterpriseId != null">
            and enterprise_id = #{enterpriseId}
        </if>
        <include refid="storeSql"></include>
        order by update_time desc
    </select>


    <select id="findByBindRelatedIdAndAuditType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where
        status_flag = 1
        and related_Id = #{relatedId}
        and audit_type = #{auditType}
        and audit_status = 0
        <if test="wxEnterpriseId != null">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="enterpriseId != null">
            and enterprise_id = #{enterpriseId}
        </if>
    </select>

    <select id="judgeHavePhoneNumberOrCode" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_audit
        where
        status_flag = 1
        and audit_type = 3
        and audit_status = 0
        and enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
        and (related_Id = #{clerkCode}
        <if test="phoneNumber != null and phoneNumber != ''">
            or old_value like concat ('%',#{phoneNumber},'%')
        </if>
        )
        <if test="wxEnterpriseId != null">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
    </select>

    <select id="getAuditCount" resultType="int">
        select count(*) from tab_haoban_audit where wx_enterprise_id=#{wxEnterpriseId} and enterprise_id=#{enterpriseId} and status_flag = 1
        <if test="null != storeIds">
            and
            ( commit_store_id in
            <foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
        and audit_status = 0
        <if test="relatedId != null and relatedId != ''">
            and related_Id = #{relatedId}
        </if>
        <if test="clerkType == 1">
            and audit_type in (2,3,4,5)
        </if>
    </select>

</mapper>