<?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.dayou.mapper.ItemMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.vo.ItemVO">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="business_no" property="businessNo" />
        <result column="oa_no" property="oaNo" />
        <result column="name" property="name" />
        <result column="cate" property="cate" />
        <result column="business_source" property="businessSource" />
        <result column="belong_to" property="belongTo" />
        <result column="client_unit" property="clientUnit" />
        <result column="client_name" property="clientName" />
        <result column="mobile" property="mobile" />
        <result column="client_manager" property="clientManager" />
        <result column="skiller" property="skiller" />
        <result column="sign_date" property="signDate" />
        <result column="payment_method" property="paymentMethod" />
        <result column="amount" property="amount" />
        <result column="state" property="state" />
        <result column="department_id" property="departmentId" />
        <result column="contract_url" property="contractUrl" />
        <result column="user_id" property="userId" />
        <result column="customer_id" property="customerId" />
        <result column="upload_date" property="uploadDate" />
        <collection property="userIds" ofType="java.lang.Long" select="selectItemUserId" column="id"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        i.id,
        i.deleted,
        i.created,
        i.modified,
        i.business_no, i.oa_no, i.name,i.cate, i.business_source,i.belong_to,i.client_unit,
        c.name as client_name, i.mobile, u.name as client_manager, i.skiller, i.sign_date,
            i.payment_method, i.amount, i.state,i.department_id,i.contract_url,i.user_id,i.customer_id , i.upload_date
    </sql>

    <sql id="Common_query_item">
        SELECT
        <include refid="Base_Column_List"/>
        , d.name as departmentName,dd1.name as cateName,dd2.name as businessSourceName,
        (select name from item_stage s where curDate() &gt;= s.start_date and curDate() &lt;= s.end_date and s.item_id = i.id) as stageName,
        ( SELECT
        s.id
        FROM
        item_stage s
        WHERE
        curDate() &gt;= s.start_date
        AND curDate() &lt;= s.end_date
        AND s.item_id = i.id
        ) AS stageId,

        (	CASE when (
        i.state = '暂停'
        ) then '暂停'
        when (
        i.state = '终止'
        ) then '终止'
        when  ((SELECT count(*) FROM item_stage s
        WHERE
        s.item_id = i.id
        AND curDate() &gt;= ( SELECT min( start_date ) FROM item_stage WHERE item_id = i.id )
        AND curDate() &lt;= ( SELECT max( end_date ) FROM item_stage WHERE item_id = i.id )
        )=0

        AND

        ( SELECT count(*) FROM item_stage s WHERE s.item_id = i.id ) !=0 )

        THEN
        '已完成'

        WHEN
        (SELECT
        count(*)
        FROM
        item_stage s
        WHERE
        s.item_id = i.id
        AND curDate() &gt;= ( SELECT min( start_date ) FROM item_stage WHERE item_id = i.id )
        AND curDate() &lt;= ( SELECT max( end_date ) FROM item_stage WHERE item_id = i.id ) )
        !=0
        THEN
        '进行中'
        WHEN
        (SELECT
        count(*)
        FROM
        item_stage s
        WHERE
        s.item_id = i.id)= 0
        THEN
        '未开始'
        ELSE ''
        END
        ) AS itemStatus
        FROM
        item i left join department d on i.department_id = d.id  left join dict_data dd1 on i.cate = dd1.id and dd1.deleted =0
        left join dict_data dd2 on i.business_source = dd2.id and dd2.deleted = 0 left join user u on u.id = i.user_id left join customer c on c.id = i.customer_id
        where i.deleted = 0
    </sql>

    <select id="stat" resultType="com.dayou.vo.ItemStatVO">
        SELECT
                ( SELECT count(*) FROM item WHERE state = '已完成' AND deleted = 0 ) AS doneNum,
                ( SELECT count(*) FROM item WHERE state = '进行中' AND deleted = 0 ) AS pendingNum,
                ( SELECT count(*) FROM item WHERE state = '未开始' AND deleted = 0 ) AS unplayedNum
        FROM
            item
        WHERE
            deleted =0
    </select>

    <select id="xDetail" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>, d.name as departmentName
        from item i left join department d on d.id = i.department_id left join user u on u.id = i.user_id
                                           left join customer c on c.id = i.customer_id
                                           where i.deleted = 0  and i.id =#{id}
    </select>

    <select id="selectItemUserId" parameterType="java.lang.Long" resultType="java.lang.Long">
        select user_id from item_user where item_id = #{id}
    </select>

    <select id="getPage" resultType="com.dayou.vo.ItemVO">
        <include refid="Common_query_item"/>
        <if test="item!=null and item.name!=null and item.name!='' ">
            and i.name like concat ('%',#{item.name},'%')
        </if>
        <if test="item!=null and item.cate!=null and item.cate!='' ">
            and i.cate =#{item.cate}
        </if>
        <if test="item!=null and item.businessNo!=null and item.businessNo!='' ">
            and i.business_no like concat (#{item.businessNo},'%')
        </if>
        <if test="item!=null and item.isCurYear!=null and item.isCurYear==true">
            AND i.upload_date &gt;= concat( YEAR ( now())- 1, '-12-23' )
            AND i.upload_date &lt;= concat( YEAR ( now()), '-12-22' )
        </if>
        <if test="item!=null and item.clientManager!=null and item.clientManager!='' ">
            and u.name like concat ('%',#{item.clientManager},'%')
        </if>
        <if test="item!=null and item.skiller!=null and item.skiller!='' ">
            and i.skiller =#{item.skiller}
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
            and i.user_id in
            <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            and i.department_id in
            <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                #{departmentId}
            </foreach>
        </if>
        <if test="item!=null and item.state!=null and item.state!='' ">
            GROUP BY i.id
            HAVING itemStatus =#{item.state}
        </if>
        order by i.upload_date desc ,i.id DESC
    </select>

    <select id="getXPage" resultType="com.dayou.vo.ItemVO">
        <include refid="Common_query_item"/>
        <if test="item!=null and item.name!=null and item.name!='' ">
            and i.name like concat ('%',#{item.name},'%')
        </if>
        <if test="item!=null and item.cate!=null and item.cate!='' ">
            and i.cate =#{item.cate}
        </if>
        <if test="item!=null and item.businessNo!=null and item.businessNo!='' ">
            and i.business_no like concat (#{item.businessNo},'%')
        </if>
        <if test="item!=null and item.clientManager!=null and item.clientManager!='' ">
            and u.name like concat ('%',#{item.clientManager},'%')
        </if>
        <if test="item!=null and item.skiller!=null and item.skiller!='' ">
            and i.skiller =#{item.skiller}
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0 and item!=null and item.departmentIds!=null and item.departmentIds.size==0">
            <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
                and EXISTS ( SELECT item_id FROM item_user iu WHERE iu.item_id = i.id AND iu.user_id in
                <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
                )
            </if>
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size==0 and item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
                and i.department_id in
                <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>
            </if>
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0 and item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            and (
            <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
                EXISTS ( SELECT item_id FROM item_user iu WHERE iu.item_id = i.id AND iu.user_id in
                <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
                )
            </if>
            <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
                or i.department_id in
                <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>
            </if>
            )
        </if>
        <if test="item!=null and item.state!=null and item.state!='' ">
            GROUP BY i.id
            HAVING itemStatus =#{item.state}
        </if>
        order by i.upload_date desc ,i.id DESC
    </select>

    <select id="xSelectList" parameterType="com.dayou.vo.ItemStageVO" resultType="com.dayou.vo.ItemVO">
        <include refid="Common_query_item"/>
        and i.state is null
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0 and item!=null and item.departmentIds!=null and item.departmentIds.size==0">
            <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
                and EXISTS ( SELECT item_id FROM item_user iu WHERE iu.item_id = i.id AND iu.user_id in
                <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
                )
            </if>
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size==0 and item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
                and i.department_id in
                <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>
            </if>
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0 and item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            and (
            <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
                EXISTS ( SELECT item_id FROM item_user iu WHERE iu.item_id = i.id AND iu.user_id in
                <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
                )
            </if>
            <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
                or i.department_id in
                <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>
            </if>
            )
        </if>
        <if test="item!=null and item.state!=null and item.state!='' ">
            GROUP BY i.id
            HAVING itemStatus =#{item.state}
        </if>
        order by i.upload_date desc ,i.id DESC
    </select>


    <select id="getLastMonthItems" resultType="com.dayou.entity.ItemBrokerageGeneral">
        select ibg.*
        FROM
        item i
            left join item_brokerage_general ibg on ibg.item_id = i.id
         WHERE
            i.deleted = 0
        and i.upload_date is not null
        AND i.upload_date &gt;= #{lastMonth23}
        AND i.upload_date &lt;= concat( date_format( LAST_DAY( now()), '%Y-%m-' ), '22' )
        AND i.user_id = #{userId}
        and ibg.marketer_status=#{marketerStatus}
    </select>

    <select id="getLastMonthItemsInStatus"  resultType="com.dayou.entity.ItemBrokerageGeneral">
        select ibg.*
        FROM
            item i
                left join item_brokerage_general ibg on ibg.item_id = i.id
        WHERE
            i.deleted = 0
          and i.upload_date is not null
          AND i.upload_date &gt;= #{lastMonth23}
          AND i.upload_date &lt;= concat( date_format( LAST_DAY( now()), '%Y-%m-' ), '22' )
          AND i.user_id = #{userId}
          and ibg.marketer_status in ('TO_PREDICTING','NOT_PAYMENT')
    </select>

    <select id="xList"  resultType="com.dayou.vo.ItemVO">
        <include refid="Common_query_item"/>
        <if test="item!=null and item.name!=null and item.name!='' ">
            and i.name like concat ('%',#{item.name},'%')
        </if>
        <if test="item!=null and item.cate!=null and item.cate!='' ">
            and i.cate =#{item.cate}
        </if>
        <if test="item!=null and item.businessNo!=null and item.businessNo!='' ">
            and i.business_no like concat (#{item.businessNo},'%')
        </if>
        <if test="item!=null and item.clientManager!=null and item.clientManager!='' ">
            and u.name like concat ('%',#{item.clientManager},'%')
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0 and item!=null and item.departmentIds!=null and item.departmentIds.size==0">
            <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
                and EXISTS ( SELECT item_id FROM item_user iu WHERE iu.item_id = i.id AND iu.user_id in
                <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
                )
            </if>
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size==0 and item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
                and i.department_id in
                <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>
            </if>
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0 and item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            and (
            <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
                EXISTS ( SELECT item_id FROM item_user iu WHERE iu.item_id = i.id AND iu.user_id in
                <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
                )
            </if>
            <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
                or i.department_id in
                <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>
            </if>
            )
        </if>
        <if test="item!=null and item.state!=null and item.state!='' ">
            GROUP BY i.id
            HAVING itemStatus =#{item.state}
        </if>
        order by i.upload_date desc ,i.id DESC
    </select>

    <select id="xMineList" resultType="com.dayou.vo.ItemVO">
        <include refid="Common_query_item"/>
        <if test="item!=null and item.name!=null and item.name!='' ">
            and i.name like concat ('%',#{item.name},'%')
        </if>
        <if test="item!=null and item.cate!=null and item.cate!='' ">
            and i.cate =#{item.cate}
        </if>
        <if test="item!=null and item.businessNo!=null and item.businessNo!='' ">
            and i.business_no like concat (#{item.businessNo},'%')
        </if>
        <if test="item!=null and item.isCurYear!=null and item.isCurYear==true">
            AND i.upload_date &gt;= concat( YEAR ( now())- 1, '-12-23' )
            AND i.upload_date &lt;= concat( YEAR ( now()), '-12-22' )
        </if>
        <if test="item!=null and item.clientManager!=null and item.clientManager!='' ">
            and u.name like concat ('%',#{item.clientManager},'%')
        </if>
        <if test="item!=null and item.userIds!=null and item.userIds.size!=0">
            and i.user_id in
            <foreach collection="item.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        <if test="item!=null and item.departmentIds!=null and item.departmentIds.size!=0">
            and i.department_id in
            <foreach collection="item.departmentIds" open="(" close=")" separator="," item="departmentId">
                #{departmentId}
            </foreach>
        </if>
        <if test="item!=null and item.state!=null and item.state!='' ">
            GROUP BY i.id
            HAVING itemStatus =#{item.state}
        </if>
        order by i.upload_date desc ,i.id DESC
    </select>

    <select id="getItemByBrokerageGeneralId" parameterType="java.lang.Long" resultType="com.dayou.dto.ItemDTO">
        select <include refid="Base_Column_List"/>,ibg.brokerage_rate as ratio
        from item_brokerage_general ibg left join item i on i.id = ibg.item_id
                                                         left join customer c on c.id = i.customer_id left join user u on u.id = i.user_id
                                                         where i.deleted = 0 and ibg.deleted = 0 and ibg.id = #{generalId}
    </select>

    <select id="getLastMonthTeamItems" resultType="com.dayou.dto.ItemBrokerageGeneralDTO">
        select ibg.*,i.amount,i.user_id
        FROM
            item i
                left join item_brokerage_general ibg on ibg.item_id = i.id
        WHERE
            i.deleted = 0
          and i.upload_date is not null
          AND i.upload_date &gt;= #{lastMonth23}
          AND i.upload_date &lt;= concat( date_format( LAST_DAY( now()), '%Y-%m-' ), '22' )
          AND i.user_id in (
              <foreach collection="juniorUserIds" index="index" item="item" separator=",">
                  #{item}
              </foreach>
            )
          and ibg.marketer_status != 'CLOSED'
    </select>

    <select id="getCompletedNotEvaluateItem" resultType="com.dayou.vo.ItemVO">
        SELECT
        i.id,
        i.user_id,
        i.name,
        (	CASE when  ((SELECT count(*) FROM item_stage s
                          WHERE
                              s.item_id = i.id
                            AND curDate() &gt;= ( SELECT min( start_date ) FROM item_stage WHERE item_id = i.id )
                            AND curDate() &lt;= ( SELECT max( end_date ) FROM item_stage WHERE item_id = i.id )
                         )=0

            AND

                         ( SELECT count(*) FROM item_stage s WHERE s.item_id = i.id ) !=0 )

            THEN
        '已完成'

        WHEN
        (SELECT
        count(*)
        FROM
        item_stage s
        WHERE
        s.item_id = i.id
        AND curDate() &gt;= ( SELECT min( start_date ) FROM item_stage WHERE item_id = i.id )
        AND curDate() &lt;= ( SELECT max( end_date ) FROM item_stage WHERE item_id = i.id ) )
        !=0
        THEN
        '进行中'
        WHEN
        (SELECT
        count(*)
        FROM
        item_stage s
        WHERE
        s.item_id = i.id)= 0
        THEN
        '未开始'
        ELSE ''
        END
        ) AS itemStatus
        FROM
        item i
        WHERE
        i.deleted = 0  and i.state is null
        GROUP BY
        i.id
        HAVING
        itemStatus = '已完成'
        AND NOT EXISTS ( SELECT id FROM item_evaluate WHERE item_id = i.id AND deleted = 0 )
    </select>
</mapper>
