<?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">

    <resultMap id="BaseResultMap" type="com.sckw.manage.model.entity.KwmCooperate">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="entId" column="ent_id" jdbcType="BIGINT"/>
            <result property="inviterEntId" column="inviter_ent_id" jdbcType="BIGINT"/>
            <result property="inviteeEntId" column="invitee_ent_id" jdbcType="BIGINT"/>
            <result property="inviterContacts" column="inviter_contacts" jdbcType="VARCHAR"/>
            <result property="inviterPhone" column="inviter_phone" jdbcType="VARCHAR"/>
            <result property="inviteeContacts" column="invitee_contacts" jdbcType="VARCHAR"/>
            <result property="inviteePhone" column="invitee_phone" jdbcType="VARCHAR"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="INTEGER"/>
            <result property="createBy" column="create_by" jdbcType="BIGINT"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateBy" column="update_by" jdbcType="BIGINT"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
            <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,ent_id,inviter_ent_id,
        invitee_ent_id,inviter_contacts,inviter_phone,
        invitee_contacts,invitee_phone,remark,
        status,create_by,create_time,
        update_by,update_time,del_flag
    </sql>

    <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) = min(a.invitee_ent_id) then min(a.inviter_ent_id) else min(a.invitee_ent_id) end targetEntId,
               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>
                OR a.inviter_ent_id in
                <foreach collection="reqDto.currentEntIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
               )
           <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 a.ent_id = #{reqDto.currentEntId}
           </if>
           <if test="reqDto.applyTypeCode != null and reqDto.applyTypeCode == 2">
               and a.ent_id != #{reqDto.currentEntId}
           </if>
           <if test="reqDto.cooperateTypes != null and reqDto.cooperateTypes.size() > 0">
               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 in
                            <foreach collection="reqDto.cooperateTypes" item="item" open="(" close=")" separator=",">
                               #{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.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>
                ))
    </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,
                min(a.remark) remark,
                min(a.approval_remark) approvalRemark,
                case when min(a.ent_id) = min(a.invitee_ent_id) then min(a.inviter_ent_id) else min(a.invitee_ent_id) end targetEntId,
                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>
            OR a.inviter_ent_id in
            <foreach collection="reqDto.currentEntIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            )
        <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 a.ent_id = #{reqDto.currentEntId}
        </if>
        <if test="reqDto.applyTypeCode != null and reqDto.applyTypeCode == 2">
            and a.ent_id != #{reqDto.currentEntId}
        </if>
        <if test="reqDto.cooperateTypes != null and reqDto.cooperateTypes.size() > 0">
            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 in
                        <foreach collection="reqDto.cooperateTypes" item="item" open="(" close=")" separator=",">
                            #{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.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>

</mapper>
