<?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.tuoheng.admin.mapper.WorkOrderMapper">
    
    <resultMap type="com.tuoheng.admin.entity.WorkOrder" id="WorkOrderResult">
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="code"    column="code"    />
        <result property="status"    column="status"    />
        <result property="assignDeptId"    column="assign_dept_id"    />
        <result property="assignUser"    column="assign_user"    />
        <result property="assignContact"    column="assign_contact"    />
        <result property="assignNote"    column="assign_note"    />
        <result property="assignTime"    column="assign_time"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
    </resultMap>

    <sql id="selectThWorkOrderVo">
        select id, tenant_id, dept_id, code, status, assign_dept_id, assign_user, assign_contact, assign_note, assign_time, create_user, create_time, update_user, update_time, mark
        from th_work_order
    </sql>

    <update id="update" parameterType="com.tuoheng.admin.entity.WorkOrder">
        update th_work_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="assignDeptId != null and assignDeptId !='' "> assign_dept_id = #{assignDeptId}, </if>
            <if test="assignUser != null and assignUser !='' "> assign_user = #{assignUser}, </if>
            <if test="assignContact != null and assignContact !='' "> assign_contact = #{assignContact}, </if>
            <if test="assignNote != null and assignNote !='' "> assign_note = #{assignNote}, </if>
            <if test="assignTime != null"> assign_time = #{assignTime}, </if>
            <if test="status != null"> status = #{status}, </if>
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser}, </if>
            <if test="updateTime != null"> update_time = #{updateTime}, </if>
        </trim>
        where id = #{id}
    </update>

    <select id="selectPageList" parameterType="com.tuoheng.admin.request.workorder.QueryWorkOrderPageListRequest" resultMap="WorkOrderResult">
        <include refid="selectThWorkOrderVo"/>
        <where>
            <if test="1 == 1"> and mark = 1 </if>
            <if test="request.tenantId != null and request.tenantId != ''"> and tenant_id = #{request.tenantId} </if>
            <if test="request.code != null and request.code != ''"> and code = #{request.code} </if>
            <if test="request.status != null and request.status != ''"> and status = #{request.status} </if>
            <if test="(request.userDeptId != null and request.userDeptId != '') or (request.deptIdList != null and request.deptIdList.size() > 0)">
                and (
                <trim prefixOverrides="or">
                    <if test="(request.userDeptId != null and request.userDeptId != '')">
                        or assign_dept_id = #{request.userDeptId}
                    </if>
                    <if test="request.deptIdList != null and request.deptIdList.size() > 0">
                        or dept_id in
                        <foreach item="deptId" collection="request.deptIdList" open="(" separator="," close=")">
                            #{deptId}
                        </foreach>
                    </if>
                </trim>
                )
            </if>
            <if test="request.beginTime != null">
                and create_time &gt;= #{request.beginTime}
            </if>
            <if test="request.endTime != null">
                and create_time &lt;= #{request.endTime}
            </if>
        </where>
        order by create_time desc
    </select>

</mapper>