<?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.sckw.manage.dao.KwmCooperateMapper">

    <select id="findList" parameterType="com.sckw.manage.model.dto.req.CooperateManageQueryReqDto"
            resultType="com.sckw.manage.model.dto.res.CooperateManageQueryResDto">
        SELECT a.id,
            b.status,
            group_concat(b.type) types,
            min(a.ent_id) entId,
            min(a.invitee_ent_id) inviteeEntId,
            min(a.inviter_ent_id) inviterEntId,
            min(a.invitee_contacts) inviteeContacts,
            min(a.inviter_contacts) inviterContacts,
            min(a.invitee_contacts_id) inviteeContactsId,
            min(a.inviter_contacts_id) inviterContactsId,
            min(a.invitee_phone) inviteePhone,
            min(a.inviter_phone) inviterPhone,
            min(b.create_time) createTime,
            min(b.create_by) createBy,
            min(a.remark) remark,
            min(a.approval_remark) approvalRemark,
            case when min(a.ent_id) = #{reqDto.currentEntId} then min(a.invitee_ent_id) else min(a.inviter_ent_id) end targetEntId,
            case when min(a.ent_id) = #{reqDto.currentEntId} then min(a.inviter_ent_id) else min(a.invitee_ent_id) end ourEntId,
            case when min(a.ent_id) = #{reqDto.currentEntId} then 1 else 2 end applyTypeCode
        FROM kwm_cooperate a
        LEFT JOIN kwm_cooperate_type b ON a.id = b.cooperate_id
        WHERE a.del_flag = 0
          and ((a.invitee_ent_id in
                <foreach collection="reqDto.currentEntIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>

                <if test="reqDto.targetEntIdList != null and reqDto.targetEntIdList.size() > 0">
                    and (a.inviter_ent_id in
                    <foreach collection="reqDto.targetEntIdList" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                          or a.inviter_contacts like concat('%', #{reqDto.keywords}, '%') or
                            a.inviter_phone like concat('%', #{reqDto.keywords}, '%')
                        )
                </if>
                ) OR
               (a.inviter_ent_id in
                <foreach collection="reqDto.currentEntIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                <if test="reqDto.targetEntIdList != null and reqDto.targetEntIdList.size() > 0">
                    and (a.invitee_ent_id in
                    <foreach collection="reqDto.targetEntIdList" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                        or a.invitee_contacts like concat('%', #{reqDto.keywords}, '%') or
                        a.invitee_phone like concat('%', #{reqDto.keywords}, '%')
                        )
                </if>
               ))
           <if test="reqDto.startTime != null">
               and b.create_time >= #{reqDto.startTime}
           </if>
           <if test="reqDto.endTime != null">
               and b.create_time &lt; date_add(#{reqDto.endTime}, INTERVAL 1 DAY)
           </if>
           <if test="reqDto.applyTypeCode != null and reqDto.applyTypeCode == 1">
               and b.create_by in
               <foreach collection="reqDto.currentUserIdList" item="item" open="(" close=")" separator=",">
                   #{item}
               </foreach>
           </if>
           <if test="reqDto.applyTypeCode != null and reqDto.applyTypeCode == 2">
               and b.create_by not in
               <foreach collection="reqDto.currentUserIdList" item="item" open="(" close=")" separator=",">
                   #{item}
               </foreach>
           </if>
            <if test="reqDto.cooperateTypes != null and reqDto.cooperateTypes.size() > 0">
                <foreach collection="reqDto.cooperateTypes" item="item">
                    and exists (select 1
                                  from kwm_cooperate_type c
                                 where c.cooperate_id = a.id
                                   and c.status = b.status
                                   and c.del_flag = 0
                                   and c.type = #{item}
                                )
                </foreach>
           </if>

         <if test="reqDto.status != null and reqDto.status.size() > 0">
             and b.status in
             <foreach collection="reqDto.status" item="item" open="(" close=")" separator=",">
                 #{item}
             </foreach>
         </if>
        <if test="reqDto.keywords != null and (reqDto.targetEntIdList == null or reqDto.targetEntIdList.size() == 0)">
            and (a.invitee_contacts like concat('%', #{reqDto.keywords}, '%') or
                 a.inviter_contacts like concat('%', #{reqDto.keywords}, '%') or
                 a.invitee_phone like concat('%', #{reqDto.keywords}, '%') or
                 a.inviter_phone like concat('%', #{reqDto.keywords}, '%')
                )
        </if>
         <if test="reqDto.ids != null and reqDto.ids.size() > 0">
             and a.id in
             <foreach collection="reqDto.ids" item="item" open="(" close=")" separator=",">
                 #{item}
             </foreach>
         </if>
        group by a.id, b.status
        order by a.create_time desc
    </select>

    <select id="findCooperateByEnt" resultType="com.sckw.manage.model.entity.KwmCooperate">
        select a.*
          from kwm_cooperate a
         where a.del_flag = 0
           and a.status = 1
           and ((a.inviter_ent_id = #{entId}
                <if test="targetEntId != null">
                    and a.invitee_ent_id = #{targetEntId}
                </if>
                )
                or
                (a.invitee_ent_id = #{entId}
                <if test="targetEntId != null">
                    and a.inviter_ent_id = #{targetEntId}
                </if>
                ))

<!--            <if test="cooperateType != null">-->
<!--                and exists (select 1-->
<!--                              from kwm_cooperate_type c-->
<!--                             where c.cooperate_id = a.id-->
<!--                               and c.del_flag = 0-->
<!--                               and c.type = #{cooperateType}-->
<!--                            )-->
<!--            </if>-->
    </select>

    <select id="findManageList" resultType="com.sckw.manage.model.dto.res.CooperateManageQueryResDto">
        SELECT a.id,
                min(a.status) status,
                group_concat(b.type) types,
                min(a.ent_id) entId,
                min(a.invitee_ent_id) inviteeEntId,
                min(a.inviter_ent_id) inviterEntId,
                min(a.invitee_contacts) inviteeContacts,
                min(a.inviter_contacts) inviterContacts,
                min(a.invitee_contacts_id) inviteeContactsId,
                min(a.inviter_contacts_id) inviterContactsId,
                min(a.invitee_phone) inviteePhone,
                min(a.inviter_phone) inviterPhone,
                min(b.create_time) createTime,
                min(b.create_by) createBy,
                group_concat(b.create_by) createByStr,
                min(a.remark) remark,
                min(a.approval_remark) approvalRemark,
                case when min(a.ent_id) = #{reqDto.currentEntId} then min(a.invitee_ent_id) else min(a.inviter_ent_id) end targetEntId,
                case when min(a.ent_id) = #{reqDto.currentEntId} then min(a.inviter_ent_id) else min(a.invitee_ent_id) end ourEntId,
                case when min(a.ent_id) = #{reqDto.currentEntId} then 1 else 2 end applyTypeCode
        FROM kwm_cooperate a
        LEFT JOIN kwm_cooperate_type b ON a.id = b.cooperate_id and b.status = 1 and b.del_flag = 0
        WHERE a.del_flag = 0
        and ((a.invitee_ent_id in
                <foreach collection="reqDto.currentEntIdList" item="item" open="(" close=")" separator=",">
                  #{item}
                </foreach>
                <if test="reqDto.targetEntIdList != null and reqDto.targetEntIdList.size() > 0">
                    and (a.inviter_ent_id in
                    <foreach collection="reqDto.targetEntIdList" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                            or a.inviter_contacts like concat('%', #{reqDto.keywords}, '%') or
                            a.inviter_phone like concat('%', #{reqDto.keywords}, '%')
                            )
                </if>
            )
            OR (a.inviter_ent_id in
                <foreach collection="reqDto.currentEntIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                <if test="reqDto.targetEntIdList != null and reqDto.targetEntIdList.size() > 0">
                    and (a.invitee_ent_id in
                    <foreach collection="reqDto.targetEntIdList" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                        or a.invitee_contacts like concat('%', #{reqDto.keywords}, '%') or
                        a.invitee_phone like concat('%', #{reqDto.keywords}, '%')
                       )
                </if>
            ))
        <if test="reqDto.startTime != null">
            and b.create_time >= #{reqDto.startTime}
        </if>
        <if test="reqDto.endTime != null">
            and b.create_time &lt; date_add(#{reqDto.endTime}, INTERVAL 1 DAY)
        </if>
        <if test="reqDto.applyTypeCode != null and reqDto.applyTypeCode == 1">
            and b.create_by in
            <foreach collection="reqDto.currentUserIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="reqDto.applyTypeCode != null and reqDto.applyTypeCode == 2">
            and b.create_by not in
            <foreach collection="reqDto.currentUserIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="reqDto.cooperateTypes != null and reqDto.cooperateTypes.size() > 0">
            <foreach collection="reqDto.cooperateTypes" item="item">
                and exists (select 1
                              from kwm_cooperate_type c
                             where c.cooperate_id = a.id
                               and c.status = 1
                               and c.del_flag = 0
                               and c.type = #{item}
                            )
            </foreach>
        </if>
        <if test="reqDto.status != null and reqDto.status.size() > 0">
            and a.status in
            <foreach collection="reqDto.status" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="reqDto.keywords != null and (reqDto.targetEntIdList == null or reqDto.targetEntIdList.size() == 0)">
            and (a.invitee_contacts like concat('%', #{reqDto.keywords}, '%') or
                 a.inviter_contacts like concat('%', #{reqDto.keywords}, '%') or
                 a.invitee_phone like concat('%', #{reqDto.keywords}, '%') or
                 a.inviter_phone like concat('%', #{reqDto.keywords}, '%')
                )
        </if>
        <if test="reqDto.ids != null and reqDto.ids.size() > 0">
            and a.id in
            <foreach collection="reqDto.ids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by a.id
        order by a.create_time desc
    </select>

    <select id="findEntCooperate" resultType="com.sckw.manage.model.entity.KwmCooperate">
        select a.* from kwm_cooperate a
        where a.del_flag = 0
            and a.status = 1
            and ((a.inviter_ent_id = #{id}
                    <if test="targetEntId != null">
                        and a.invitee_ent_id = #{targetEntId}
                    </if>) or
                 (a.invitee_ent_id = #{id}
                    <if test="targetEntId != null">
                        and a.inviter_ent_id = #{targetEntId}
                    </if>)
                )
    </select>

    <select id="queryAllCooperateInfoList"
            resultType="com.sckw.manage.model.dto.res.CooperateManageQueryResDto">
        SELECT a.id,
               min(a.status) status,
               group_concat(b.type) types,
               min(a.ent_id) entId,
               min(a.invitee_ent_id) inviteeEntId,
               min(a.inviter_ent_id) inviterEntId,
               min(a.invitee_contacts) inviteeContacts,
               min(a.inviter_contacts) inviterContacts,
               min(a.invitee_contacts_id) inviteeContactsId,
               min(a.inviter_contacts_id) inviterContactsId,
               min(a.invitee_phone) inviteePhone,
               min(a.inviter_phone) inviterPhone,
               min(b.create_time) createTime,
               min(b.update_time) updateTime,
               min(b.create_by) createBy,
               min(a.remark) remark
          FROM kwm_cooperate a
          LEFT JOIN kwm_cooperate_type b ON a.id = b.cooperate_id and b.status = 1 and b.del_flag = 0
         WHERE a.del_flag = 0
           and a.status in (1,2)
        <if test="reqDto.cooperateTypes != null and reqDto.cooperateTypes.size() > 0">
            <foreach collection="reqDto.cooperateTypes" item="item">
                and exists (select 1
                            from kwm_cooperate_type c
                            where c.cooperate_id = a.id
                            and c.status = b.status
                            and c.del_flag = 0
                            and c.type = #{item}
                )
            </foreach>
        </if>
        <if test="reqDto.startTime != null">
            and b.create_time >= #{reqDto.startTime}
        </if>
        <if test="reqDto.endTime != null">
            and b.create_time &lt; date_add(#{reqDto.endTime}, INTERVAL 1 DAY)
        </if>
        <if test="reqDto.ids != null and reqDto.ids.size() > 0">
             and a.id in
            <foreach collection="reqDto.ids" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        <if test="reqDto.queryEntIdList != null and reqDto.queryEntIdList.size() > 0">
            and (a.inviter_ent_id in
                    <foreach collection="reqDto.queryEntIdList" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                    or
                a.invitee_ent_id in
                    <foreach collection="reqDto.queryEntIdList" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                )
        </if>
        <if test="reqDto.keywords != null and (reqDto.queryEntIdList == null or reqDto.queryEntIdList.size() == 0)">
            and (a.invitee_contacts like concat('%', #{reqDto.keywords}, '%') or
                 a.inviter_contacts like concat('%', #{reqDto.keywords}, '%') or
                 a.invitee_phone like concat('%', #{reqDto.keywords}, '%') or
                 a.inviter_phone like concat('%', #{reqDto.keywords}, '%')
                )
        </if>
        group by a.id
        order by a.create_time desc
    </select>

</mapper>
