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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.PaymentCollection">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="item_id" property="itemId" />
        <result column="name" property="name" />
        <result column="amount" property="amount" />
        <result column="payment_date" property="paymentDate" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        pc.id,
        pc.deleted,
        pc.created,
        pc.modified,
        pc.item_id, pc.name, pc.amount, pc.payment_date
    </sql>

    <sql id="paymentCollectionSql">
        select <include refid="Base_Column_List"/>,i.name as itemName
        from payment_collection pc left join item i on pc.item_id = i.id
        where i.deleted = 0 and pc.deleted = 0
        <if test="dto!=null and dto.itemId!=null">
            and pc.item_id = #{dto.itemId}
        </if>
        <if test="dto!=null and dto.itemName!=null and dto.itemName!=''">
            and i.name like concat ('%',#{dto.itemName},'%')
        </if>
        <if test="dto!=null and dto.name!=null and dto.name!=''">
            and pc.name like concat ('%',#{dto.name},'%')
        </if>
        <if test="dto!=null and dto.isCurYear!=null and dto.isCurYear==true">
            AND pc.payment_date &gt;= concat( YEAR ( now())- 1, '-12-23' )
            AND pc.payment_date &lt;= concat( YEAR ( now()), '-12-22' )
        </if>
        <if test="dto!=null and dto.isCurMonth!=null and dto.isCurMonth==true">
            AND pc.payment_date &gt;= #{dto.lastMonth23}
            AND pc.payment_date &lt;= concat( date_format( LAST_DAY( now()), '%Y-%m-' ), '22' )
        </if>
        <if test="dto!=null and dto.year!=null">
            and DATE_FORMAT(pc.payment_date,'%Y') = #{dto.year}
        </if>
        <if test="dto!=null and dto.month!=null">
            and DATE_FORMAT(pc.payment_date,'%c') = #{dto.month}
        </if>
        <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
            and i.user_id in
            <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        <if test="dto!=null and dto.startDate!=null and dto.endDate!=null">
            AND pc.created BETWEEN #{dto.startDate} AND #{dto.endDate}
        </if>
        order by pc.payment_date DESC
    </sql>

    <select id="page" parameterType="com.dayou.vo.PaymentCollectionVO" resultType="com.dayou.vo.PaymentCollectionVO">
        <include refid="paymentCollectionSql"/>
    </select>

    <select id="exportPaymentCollection" resultType="com.dayou.vo.PaymentCollectionVO">
        <include refid="paymentCollectionSql"/>
    </select>

    <sql id="itemPaymentList">
        SELECT
        i.id,
        i.business_no,
        i.oa_no,
        i.NAME,
        i.client_unit,
        u.name as client_manager,
        i.sign_date,
        i.amount,
        off.id as orderFundId,
        ifnull(( SELECT sum( amount ) FROM payment_collection WHERE item_id = i.id AND deleted = 0 ), 0 ) AS payedAmount,
        (
        i.amount -(
        ifnull(( SELECT sum( amount ) FROM payment_collection WHERE item_id = i.id AND deleted = 0 ), 0 ))) AS NotPayedAmount
        FROM
        item i
        left join (select id,business_id from order_fund where business_type='ITEM_BUSINESS' AND deleted = 0) off on off.business_id = i.id
        left join user u on u.id = i.user_id
        WHERE
        i.deleted =0
        <if test="dto!=null and dto.itemName!=null and dto.itemName!=''">
            and i.name like concat ('%',#{dto.itemName},'%')
        </if>
        <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
            and i.user_id in
            <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by i.id DESC
    </sql>
    <select id="getItemPayment" parameterType="com.dayou.vo.PaymentCollectionVO" resultType="com.dayou.vo.ItemPaymentVO">
        <include refid="itemPaymentList"/>
    </select>

    <select id="getDeleted" parameterType="java.lang.Long" resultType="com.dayou.entity.PaymentCollection">
        select * from payment_collection where id = #{id} and deleted = 1
    </select>

    <select id="getPaymentDetailListByItemId" resultType="com.dayou.dto.PaymentDetailDTO">
        SELECT
        i.id AS itemId,
        pc.id AS paymentId,
        pc.amount AS paymentAmount,
        i.upload_date
        FROM
        `payment_collection` pc
        LEFT JOIN item i ON i.id = pc.item_id
        LEFT JOIN brokerage_variable bv ON bv.payment_id = pc.id
        WHERE
        pc.payment_date &gt;= concat( YEAR ( now())- 1, '-12-23' )
        AND pc.payment_date &lt;=  concat( YEAR ( now()), '-12-22' )
        AND i.id = #{itemId}
        AND NOT EXISTS(select * from brokerage_variable where user_id = #{userId} and payment_id= pc.id and deleted = 0)
        AND pc.deleted = 0
        AND i.deleted = 0
    </select>

    <select id="getList" parameterType="com.dayou.vo.PaymentCollectionVO" resultType="com.dayou.dto.PaymentExportDTO">
        <include refid="itemPaymentList"/>
    </select>
</mapper>
