<?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.zcxk.dao.OperateLogMapper">


    <!-- 结果集 -->
    <resultMap type="com.zcxk.dto.OperateLogDto" id="OperateLogMap">
        <result property="id" column="ID" jdbcType="BIGINT"/>
        <result property="userName" column="USER_NAME" jdbcType="VARCHAR"/>
        <result property="phone" column="PHONE" jdbcType="VARCHAR"/>
        <result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
        <result property="companyId" column="COMPANY_ID" jdbcType="INTEGER"/>
        <result property="departmentId" column="DEPARTMENT_ID" jdbcType="INTEGER"/>
        <result property="operateContent" column="OPERATE_CONTENT" jdbcType="VARCHAR"/>
        <result property="createTime" column="CREATE_TIME" jdbcType="TIMESTAMP"/>
        <result property="companyName" column="companyName" jdbcType="VARCHAR"/>
        <result property="departmentName" column="departmentName" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        a.id,
        a.USER_NAME,
        a.phone,
        a.tenant_id,
        a.company_id,
        a.DEPARTMENT_ID,
        a.operate_content,
        a.create_time,
        com.ORG_NAME as "companyName",
        dep.ORG_NAME as "departmentName"
    </sql>
    <!-- 外联表  -->
    <sql id="loginLogJoins">
        left join uims_org com on com.id = a.company_id
        left join uims_org dep on dep.id = a.DEPARTMENT_ID
    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="OperateLogMap">
        select
        <include refid="Base_Column_List"/>
        from uims_opr_log a
        <include refid="loginLogJoins"/>
        where a.id = #{id}
    </select>
    <!-- 查询全部 -->
    <select id="selectAll" resultMap="OperateLogMap">
        select
        <include refid="Base_Column_List"/>
        from uims_opr_log a
        <include refid="loginLogJoins"/>
    </select>
    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="OperateLogMap">
        select
        <include refid="Base_Column_List"/>
        from uims_opr_log a
        <include refid="loginLogJoins"/>
        <where>
            <if test="id != null">
                and a.id = #{id}
            </if>
            <if test="condition != null and condition != ''">
                and (a.USER_NAME like concat('%', #{condition},'%')
                or a.phone like concat('%', #{condition},'%'))
            </if>
            <if test="companyId != null">
                and a.company_id = #{companyId}
            </if>
            <if test="departmentId != null">
                and a.DEPARTMENT_ID = #{departmentId}
            </if>
            <if test="beginTime != null">
                and a.create_time >= #{beginTime}
            </if>
            <if test="endTime != null">
                and a.create_time &lt;= #{endTime}            </if>
        </where>
    </select>
    <!--通过实体作为筛选条件查询-->
    <select id="selectListByIds" resultMap="OperateLogMap">
        select
        <include refid="Base_Column_List"/>
        from uims_opr_log a
        <include refid="loginLogJoins"/>
        <where>
            a.id in
            <foreach collection = "ids" item = "dramaId" open = "(" close = ")" separator = "," >
                #{dramaId}
            </foreach>
        </where>
    </select>
    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_opr_log( USER_NAME, tenant_id, phone, company_id, DEPARTMENT_ID, operate_content, create_time, operate_parameter)
        values ( #{userName}, #{tenantId}, #{phone}, #{companyId}, #{departmentId}, #{operateContent}, #{createTime}, #{operateParameter})
    </insert>
    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_opr_log( USER_NAME, tenant_id,phone, company_id, DEPARTMENT_ID, operate_content, create_time, operate_parameter )
        values
        <foreach collection="loginLogs" item="item" index="index" separator=",">
            ( #{item.userName}, #{item.tenantId},#{item.phone}, #{item.companyId}, #{item.departmentId}, #{item.operateContent},
            #{item.createTime}, #{item.operateParameter})
        </foreach>
    </insert>
    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_opr_log
        <set>
            <if test="userName != null and userName != ''">
                USER_NAME = #{userName},
            </if>
            <if test="phone != null and phone != ''">
                phone = #{phone},
            </if>
            <if test="companyId != null">
                company_id = #{companyId},
            </if>
            <if test="departmentId != null">
                DEPARTMENT_ID = #{departmentId},
            </if>
            <if test="operateContent != null and operateContent != ''">
                operate_content = #{operateContent},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="operateParameter != null and operateParameter != ''">
                operate_parameter = #{operateParameter},
            </if>
        </set>
        where id = #{id}
    </update>
    <!--通过主键删除-->
    <delete id="deleteById">
        delete from uims_opr_log where id = #{id}
    </delete>
    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_opr_log
    </select>
    <select id="selectPage" resultMap="OperateLogMap">
        select
        <include refid="Base_Column_List"/>
        from uims_opr_log a
        <include refid="loginLogJoins"/>
        <where>
            <if test="operateLogDto.id != null">
                and a.id = #{operateLogDto.id}
            </if>
            <if test="operateLogDto.tenantId != null and operateLogDto.tenantId != ''">
                and a.tenant_id = #{operateLogDto.tenantId}
            </if>
            <if test="operateLogDto.condition != null and operateLogDto.condition != ''">
                and (a.USER_NAME like concat('%', #{operateLogDto.condition},'%')
                or a.phone like concat('%', #{operateLogDto.condition},'%'))
            </if>
            <if test="operateLogDto.companyId != null">
                and a.company_id = #{operateLogDto.companyId}
            </if>
            <if test="operateLogDto.departmentId != null">
                and (a.DEPARTMENT_ID = #{operateLogDto.departmentId} or a.company_id = #{operateLogDto.departmentId})
            </if>
            <if test="operateLogDto.beginTime != null">
                and a.create_time >= #{operateLogDto.beginTime}
            </if>
            <if test="operateLogDto.endTime != null">
                and a.create_time &lt;=  DATE_ADD(#{operateLogDto.endTime},INTERVAL 1 DAY)
            </if>

            <if test="operateLogDto.programItems != null and operateLogDto.programItems.size() > 0">

                    <if test="operateLogDto.permissonType == 5 or operateLogDto.permissonType == 2">
                        and (a.company_id in
                        <foreach item="item" index="index" collection="operateLogDto.programItems"
                                 open="(" separator="," close=")">
                            #{item.orgId}
                        </foreach>
                        or a.DEPARTMENT_ID in
                        <foreach item="item" index="index" collection="operateLogDto.programItems"
                                 open="(" separator="," close=")">
                            #{item.orgId}
                        </foreach>
                        )
                    </if>
                    <if test="operateLogDto.permissonType == 4 or operateLogDto.permissonType == 3">
                        and  a.DEPARTMENT_ID in
                        <foreach item="item" index="index" collection="operateLogDto.programItems"
                                 open="(" separator="," close=")">
                            #{item.orgId}
                        </foreach>
                    </if>
                    <if test="operateLogDto.permissonType == 1">
                        and a.company_id in
                        <foreach item="item" index="index" collection="operateLogDto.programItems"
                                 open="(" separator="," close=")">
                            #{item.orgId}
                        </foreach>
                        and (a.DEPARTMENT_ID is null or a.DEPARTMENT_ID=0)
                    </if>
            </if>
        </where>
    </select>
</mapper>