<?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.casic.missiles.mapper.system.SystemSignMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.system.SystemSign">
        <id column="id" property="id" />
        <result column="sign_no" property="signNo" />
        <result column="sign_name" property="signName" />
        <result column="sign_desc" property="signDesc" />
        <result column="sign_director" property="signDirector" />
        <result column="sign_user_id" property="signUserId" />
        <result column="sign_type" property="signType" />
        <result column="minio_file_name" property="minioFileName" />
        <result column="is_del" property="isDel" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, sign_no, sign_name, sign_desc, sign_director, sign_type, minio_file_name, is_del, create_time, update_time
    </sql>

    <resultMap id="SignDetailResultMap" type="com.casic.missiles.dto.system.SignDetailResponse">
        <id column="id" property="id" />
        <result column="sign_no" property="signNo" />
        <result column="sign_name" property="signName" />
        <result column="sign_desc" property="signDesc" />
        <result column="sign_director" property="signDirector" />
        <result column="sign_type" property="signType" />
        <result column="sign_user_id" property="signUserId" />
        <result column="minio_file_name" property="minioFileName" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />

        <collection property="userList" javaType="list" ofType="com.casic.missiles.dto.system.SysUserDTO">
            <result column="USER_ID" property="id"/>
            <result column="ACCOUNT" property="account"/>
            <result column="NAME" property="name"/>
            <result column="DEPT_ID" property="deptId"/>
        </collection>
    </resultMap>

    <select id="selectMaxSignNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(sign_no, 12)), 0) from system_sign
    </select>

    <select id="selectSignDetailById" resultMap="SignDetailResultMap">
        SELECT ss.id, ss.sign_no, ss.sign_name, ss.sign_desc, ss.sign_director, ss.sign_type, ss.sign_user_id,
        ss.minio_file_name, ss.create_time, ss.update_time, su.ID AS USER_ID, su.ACCOUNT AS ACCOUNT,
        su.NAME AS NAME, su.DEPT_ID AS DEPT_ID
        FROM system_sign ss
        LEFT JOIN system_sign_user_relation ssur ON ss.id = ssur.sign_id
        LEFT JOIN sys_user su ON su.id = ssur.user_id
        WHERE ss.id = #{id} AND ss.is_del = 0
    </select>

    <select id="selectUsableSignList" resultMap="BaseResultMap">
        SELECT ss.*
        FROM system_sign ss
        JOIN system_sign_user_relation ssur ON ss.id = ssur.sign_id
        WHERE ssur.user_id = #{userId} AND ss.is_del = 0
    </select>

    <select id="selectSignListPage" resultType="com.casic.missiles.model.system.SystemSign">
        SELECT ss.*, sd.SIMPLE_NAME AS deptName
        FROM system_sign ss
        LEFT JOIN sys_user su ON su.id = ss.sign_user_id
        LEFT JOIN sys_dept sd ON sd.id = su.dept_id
        WHERE ss.is_del = 0
        <if test="request.signType != null and request.signType !=''">
            AND ss.sign_type = #{request.signType}
        </if>
        <if test="request.signNo != null and request.signNo !=''">
            AND ss.sign_no LIKE concat('%',#{request.signNo},'%')
        </if>
        <if test="request.signName != null and request.signName !=''">
            AND ss.sign_name LIKE concat('%',#{request.signName},'%')
        </if>
        <if test="request.signDirector != null and request.signDirector !=''">
            AND ss.sign_director LIKE concat('%',#{request.signDirector},'%')
        </if>
        <if test="request.signUserId != null and request.signUserId !=''">
            AND ss.sign_user_id = #{request.signUserId}
        </if>
        <if test="request.createStartTime != null and request.createStartTime !=''">
            AND DATE_FORMAT(ss.create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{request.createStartTime},'%Y-%m-%d')
        </if>
        <if test="request.createEndTime != null and request.createEndTime !=''">
            AND DATE_FORMAT(ss.create_time,'%Y-%m-%d')  &lt;= DATE_FORMAT(#{request.createEndTime},'%Y-%m-%d')
        </if>
        ORDER BY ss.create_time DESC
    </select>

    <select id="selectSignList" resultType="com.casic.missiles.model.system.SystemSign">
        SELECT ss.*, sd.SIMPLE_NAME AS deptName
        FROM system_sign ss
        LEFT JOIN sys_user su ON su.id = ss.sign_user_id
        LEFT JOIN sys_dept sd ON sd.id = su.dept_id
        WHERE ss.is_del = 0
        <if test="request.signType != null and request.signType !=''">
            AND ss.sign_type = #{request.signType}
        </if>
        <if test="request.signNo != null and request.signNo !=''">
            AND ss.sign_no LIKE concat('%',#{request.signNo},'%')
        </if>
        <if test="request.signName != null and request.signName !=''">
            AND ss.sign_name LIKE concat('%',#{request.signName},'%')
        </if>
        <if test="request.signDirector != null and request.signDirector !=''">
            AND ss.sign_director LIKE concat('%',#{request.signDirector},'%')
        </if>
        <if test="request.signUserId != null and request.signUserId !=''">
            AND ss.sign_user_id = #{request.signUserId}
        </if>
        <if test="request.createStartTime != null and request.createStartTime !=''">
            AND DATE_FORMAT(ss.create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{request.createStartTime},'%Y-%m-%d')
        </if>
        <if test="request.createEndTime != null and request.createEndTime !=''">
            AND DATE_FORMAT(ss.create_time,'%Y-%m-%d')  &lt;= DATE_FORMAT(#{request.createEndTime},'%Y-%m-%d')
        </if>
        ORDER BY ss.create_time DESC
    </select>
</mapper>
