<?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.qmth.sop.business.mapper.SysDingGroupMapper">

    <select id="query" resultType="com.qmth.sop.business.bean.result.SysDingGroupResult">
        select DISTINCT a.*,s.name supplier,t.name service,u.real_name create_name,t.status from sys_ding_group a
        left join sys_user u on u.id = a.create_id
        left join sys_supplier s on s.id = a.supplier_id
        left join t_b_service t on t.id = a.service_id
        left join sys_ding_obj do on do.ding_group_id=a.id
        <where>
            AND t.status != 'FINISH'
            AND t.status != 'CANCEL'
            <if test="supplierIds != null and supplierIds != ''">
                and a.supplier_id in
                <foreach collection="supplierIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dingRoleIds != null and dingRoleIds != ''">
                and (do.type='DING' and do.role_id in
                <foreach collection="dingRoleIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                )
            </if>
            <if test="approveRoleIds != null and approveRoleIds != ''">
                and (do.type='APPROVE' and do.role_id in
                <foreach collection="approveRoleIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                )
            </if>
            <if test="serviceId != null and serviceId != ''">
                and a.service_id = #{serviceId}
            </if>
            <if test="status != null">
                AND t.status = #{status}
            </if>

            <if test="faceOpen != null">
                <choose>
                    <when test="faceOpen">
                        AND a.face_open = 1
                    </when>
                    <otherwise>
                        AND a.face_open = 0
                    </otherwise>
                </choose>
            </if>
            <if test="createStartTime != null and createStartTime != '' ">
                <![CDATA[
               and a.create_time >= #{createStartTime}
               ]]>
            </if>
            <if test="createEndTime != null and createEndTime != ''">
                <![CDATA[
                and a.create_time <= #{createEndTime}
                ]]>
            </if>
        </where>
        order by a.create_time desc
    </select>
</mapper>