<?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="cn.wisenergy.mapper.WorkOrderChangeMapper">


    <select id="getWorkOrderChangeLogDto" resultType="cn.wisenergy.model.dto.WorkOrderChangeLogDto">
        select oc.change_date,CASE oc.content when 1 THEN '新增工单' when 2 THEN '删除工单' when 3 then '修改工单' end as 'content'
        ,oc.u1.name 'operator',d.dept_name,if(oc.project_id is null,t.name,p.project_name) 'project_name',
        oc.work_day,u2.name 'user_name',oc.work_time,oc.des,oc.change_reason
        from work_order_change oc LEFT JOIN work_dept d ON oc.dept_id = d.id
        LEFT JOIN work_project p ON oc.project_id = p.id
        LEFT JOIN work_type t ON oc.type = t.id
        LEFT JOIN (SELECT id,name FROM work_user ) u1 ON u1.id =oc.operator_id
        LEFT JOIN (SELECT id,name FROM work_user ) u2 ON u2.id =oc.user_id
        <where>
            <if test="projectIds !=null and projectIds.size()>0">
            <if test="startDate !=null">
                AND DATE_FORMAT(oc.change_date, '%Y%m%d') >= DATE_FORMAT(#{startDate} ,'%Y%m%d')
            </if>
            <if test="endDate !=null">
                AND DATE_FORMAT(oc.change_date, '%Y%m%d') &lt;= DATE_FORMAT(#{endDate} ,'%Y%m%d')
            </if>
            <if test="userName !=null">
                AND (u2.name like concat('%',#{userName},'%')
                OR u1.name like concat('%',#{userName},'%'))
            </if>

                AND oc.project_id IN
                <foreach collection="projectIds" item="projectId" separator="," open="(" close=")">
                    #{projectId}
                </foreach>
            </if>

            <if test="types !=null and types.size()>0">
                OR (oc.type IN
                <foreach collection="types" item="type" separator="," open="(" close=")">
                    #{type}
                </foreach>

                <if test="deptIds !=null and deptIds.size()>0">
                    AND oc.dept_id IN
                    <foreach collection="deptIds" item="deptId" separator="," open="(" close=")">
                        #{deptId}
                    </foreach>
                </if>
                <if test="startDate !=null">
                    AND DATE_FORMAT(oc.change_date, '%Y%m%d') >= DATE_FORMAT(#{startDate} ,'%Y%m%d')
                </if>
                <if test="endDate !=null">
                    AND DATE_FORMAT(oc.change_date, '%Y%m%d') &lt;= DATE_FORMAT(#{endDate} ,'%Y%m%d')
                </if>
                <if test="userName !=null">
                    AND (u2.name like concat('%',#{userName},'%')
                    OR u1.name like concat('%',#{userName},'%'))
                </if>
)
           </if>
      </where>
      ORDER BY oc.change_date desc

    </select>
</mapper>