<?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.yanzhu.jh.project.mapper.SurProjectAuditinfoMapper">
    
    <resultMap type="SurProjectAuditinfo" id="SurProjectAuditinfoResult">
        <result property="id"    column="id"    />
        <result property="fromType"    column="from_type"    />
        <result property="fromId"    column="from_id"    />
        <result property="approveStatus"    column="approve_status"    />
        <result property="isDel"    column="is_del"    />
        <result property="comment"    column="comment"    />
        <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="selectSurProjectAuditinfoVo">
        select id, from_type, from_id, approve_status, is_del, comment, create_by, create_time, update_by, update_time, remark from sur_project_auditinfo
    </sql>

    <select id="selectSurProjectAuditinfoList" parameterType="SurProjectAuditinfo" resultMap="SurProjectAuditinfoResult">
        <include refid="selectSurProjectAuditinfoVo"/>
        <where>  
            <if test="fromType != null  and fromType != ''"> and from_type = #{fromType}</if>
            <if test="fromId != null "> and from_id = #{fromId}</if>
            <if test="approveStatus != null  and approveStatus != ''"> and approve_status = #{approveStatus}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>

    <select id="selectProjectAuditinfoList" parameterType="SurProjectAuditinfo" resultType="map">
        SELECT
            pa.approve_status as approveStatus,
            CASE pa.approve_status
            WHEN '1' THEN
                '提交申请'
            ELSE
                sdd.dict_label
            END AS approveStatusName,
            DATE_FORMAT(pa.create_time, '%Y-%m-%d %H:%i') as createTime,
            u.nick_name as userName,
            d.dept_name as deptName,
            pa.`comment`
        FROM
            sur_project_auditinfo pa
            LEFT JOIN sys_user u ON u.phonenumber = pa.create_by
            left JOIN sys_dept d on d.dept_id = u.dept_id
            LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'project_check_status'
            AND sdd.dict_value = pa.approve_status
        <where>
            is_del = 0
            <if test="fromType != null  and fromType != ''"> and pa.from_type = #{fromType}</if>
            <if test="fromId != null "> and pa.from_id = #{fromId}</if>
            <if test="approveStatus != null  and approveStatus != ''"> and pa.approve_status = #{approveStatus}</if>
        </where>
        order by pa.create_time desc
    </select>

    <select id="selectMyAwaitFlowTask" parameterType="com.ruoyi.system.domain.FlowTaskEntity"  resultType="map">
        select a.id,sp.id as projectId,sp.projectName,a.approve_status,a.type,a.typeName,u.nick_name,d.dept_name,DATE_FORMAT(a.create_time, '%Y-%m-%d %H:%i') as create_time,a.witness_user
        from vw_project_audit_all a
        left join sur_project sp on sp.id = a.project_id
        left JOIN sys_user u on a.create_by = u.user_name
        left JOIN sys_dept d on d.dept_id = u.dept_id
        where 1=1
        <!--管理员放开查询条件-->
        <if test='nowRole != "1" and nowRole != "2"'> and a.witness_user like concat('%', #{nowUserName}, '%')</if>
        <if test='activeName == "await"'> and a.approve_status='1'</if>
        <if test='activeName == "finished"'> and a.approve_status in ('3','4')</if>
        <if test="businessKey != null  and businessKey != ''"> and sp.id = #{businessKey}</if>
        <if test="businessKeyName != null  and businessKeyName != ''"> and sp.projectName like concat('%', #{businessKeyName}, '%')</if>
        <if test="startDeptName != null  and startDeptName != ''"> and d.dept_name like concat('%', #{startDeptName}, '%')</if>
        <if test="startUserName != null  and startUserName != ''"> and (u.nick_name like concat('%', #{startUserName}, '%') or u.user_name like concat('%', #{startUserName}, '%'))</if>
        <if test="category != null  and category != ''"> and a.type = #{category}</if>
        <if test="params.beginDate != null and params.beginDate != '' and params.endDate != null and params.endDate != ''"> and a.create_time between #{params.beginDate} and #{params.endDate}</if>
        order by a.create_time desc
    </select>
    
    <select id="selectSurProjectAuditinfoById" parameterType="Long" resultMap="SurProjectAuditinfoResult">
        <include refid="selectSurProjectAuditinfoVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSurProjectAuditinfo" parameterType="SurProjectAuditinfo" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_auditinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fromType != null">from_type,</if>
            <if test="fromId != null">from_id,</if>
            <if test="approveStatus != null">approve_status,</if>
            <if test="isDel != null">is_del,</if>
            <if test="comment != null">comment,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fromType != null">#{fromType},</if>
            <if test="fromId != null">#{fromId},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="comment != null">#{comment},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSurProjectAuditinfo" parameterType="SurProjectAuditinfo">
        update sur_project_auditinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="fromType != null">from_type = #{fromType},</if>
            <if test="fromId != null">from_id = #{fromId},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectAuditinfoById" parameterType="Long">
        delete from sur_project_auditinfo where id = #{id}
    </delete>

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

    <insert id="batch">
        insert into sur_project_auditinfo( from_type, from_id, approve_status, comment, create_by, create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.fromType}, #{item.fromId}, #{item.approveStatus}, #{item.comment}, #{item.createBy}, #{item.createTime})
        </foreach>
    </insert>
</mapper>