<?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="cn.roadm.mapper.SmsSpWriteOffMapper">

    <resultMap type="cn.roadm.bo.SmsSpWriteOff" id="SmsSpWriteOffResultMap">
        <id column="id" property="id"/>
        <result column="month" property="month"/>
        <result column="sp_id" property="spId"/>
        <result column="amount" property="amount"/>
        <result column="sp_price" property="spPrice"/>
        <result column="income" property="income"/>
        <result column="bill_picture_path" property="billPicturePath"/>
        <result column="createtime" property="createTime"/>
    </resultMap>

    <resultMap type="SmsSpProvinceSummary" id="SmsSpProvinceSummaryResultMap">
        <id column="id" property="id"/>
        <result column="adjust_date" property="adjust_date"/>
        <result column="month" property="month"/>
        <result column="spid" property="spId"/>
        <result column="sp_name" property="spName"/>
        <result column="submit_succ" property="submit_succ"/>
        <result column="arrive_succ" property="arrive_succ"/>
        <result column="arrive_fail" property="arrive_fail"/>
        <result column="submit_fail" property="submit_fail"/>
        <result column="sp_price" property="sp_price"/>
        <result column="channel_price" property="channel_price"/>
        <result column="channelid" property="channelid"/>
        <result column="channel_name" property="channelName"/>
        <result column="is_api_billing" property="is_api_billing"/>
        <result column="supplier_name" property="supplierName"/>
        <result column="full_name" property="companyName"/>
        <result column="income" property="income"/>
        <result column="writeAmt" property="writeAmt"/>
        <result column="writeOffStatus" property="writeOffStatus"/>
    </resultMap>

    <insert id="insertSmsSpWriteOff" parameterType="cn.roadm.bo.SmsSpWriteOff">
        insert into sms_sp_write_off
        (sp_id, `month`, amount, sp_price, income, bill_picture_path, status, createtime, remark)
        values (#{spId}, #{month}, #{amount}, #{spPrice}, #{income}, #{billPicturePath}, 0, #{createTime}, #{remark})
    </insert>

    <select id="selectSmsSpWriteOff" resultMap="SmsSpWriteOffResultMap">
        select id,
               sp_id,
               `month`,
               amount,
               sp_price,
               income,
               bill_picture_path,
               createtime,
               remark
        from sms_sp_write_off
        where sp_id = #{spId}
          and `month` like concat(#{month}, '%')
          and sp_price = #{price}
          and status = 0
    </select>

    <update id="deleteSmsSpWriteOff" parameterType="integer">
        update sms_sp_write_off
        set status = 1
        where id = #{id}
    </update>

    <update id="updateSmsSpWriteOff">
        update sms_sp_write_off
        set amount = #{amount},
            remark = #{remark}
        where id = #{id}
    </update>

    <select id="selectSmsClientBillInfo" resultMap="SmsSpProvinceSummaryResultMap">
        select sp_id, `month`, amount, sp_price, income, bill_picture_path, createtime
        from sms_client_bill_info
        where spid = #{spId}
          and `month` = #{month}
          and sp_price = #{price}
    </select>

    <delete id="delSmsClientBillInfo">
        delete
        from sms_client_bill_info
        where spid in
        <foreach collection="spIdList" close=")" open="(" separator="," item="item">
            #{item}
        </foreach>
        and `month` = #{month}
    </delete>

    <delete id="delChannelBillInfo">
        delete
        from sms_channel_bill_info
        where channelid in
        <foreach collection="channelIdList" close=")" open="(" separator="," item="item">
            #{item}
        </foreach>
        and `month` = #{month}
    </delete>

    <insert id="insertSmsClientBillInfo" parameterType="SmsSpProvinceSummary">
        INSERT INTO `sms_client_bill_info` (`spid`, `sp_name`, `company_name`, `month`, `adjust_date`, `is_api_billing`,
                                            `submit_succ`, `submit_fail`, `arrive_succ`, `arrive_fail`, `sp_price`,
                                            `income`)
        VALUES (#{spId}, #{spName}, #{companyName}, #{month}, #{adjust_date}, #{is_api_billing}, #{submit_succ},
                #{submit_fail}, #{arrive_succ}, #{arrive_fail}, #{sp_price}, #{income})
    </insert>

    <insert id="insertSmsChannelBillInfo" parameterType="SmsSpProvinceSummary">
        INSERT INTO `sms_channel_bill_info` (`channelid`, `channel_name`, `supplier_name`, `month`, `adjust_date`,
                                             `submit_succ`, `submit_fail`, `arrive_succ`, `arrive_fail`,
                                             `channel_price`, `income`)
        VALUES (#{channelid}, #{channelName}, #{supplierName}, #{month}, #{adjust_date}, #{submit_succ}, #{submit_fail},
                #{arrive_succ}, #{arrive_fail}, #{channel_price}, #{income})
    </insert>


    <select id="getClientBills" resultMap="SmsSpProvinceSummaryResultMap">
        select mm.* from (
        SELECT
        sps.spid,
        sp.sp_name,
        DATE_FORMAT(min(sps.stat_time),'%Y-%m-%d') adjust_date,
        DATE_FORMAT(min(sps.stat_time),'%Y-%m') `month`,
        sp.is_api_billing,
        se.full_name,
        SUM(ifnull(sps.submit_succ, 0 )) submit_succ,
        SUM(ifnull(sps.arrive_succ, 0 )) arrive_succ,
        SUM(ifnull(sps.arrive_fail, 0 )) arrive_fail,
        SUM(ifnull(sps.submit_fail, 0 )) submit_fail,
        sps.sp_price
        FROM
        sms_sp_province_summary sps left join
        sms_sp_info sp on sps.spid = sp.id left join
        sms_enterprise se on se.id = sp.eid
        <where>
            sps.stat_time &gt;= #{startTime}
            AND sps.stat_time &lt; #{endTime}
            <if test="spIdList!=null">
                AND sps.spid in
                <foreach collection="spIdList" close=")" open="(" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY sps.spid,sps.sp_price,sp.sp_name,sp.is_api_billing,se.full_name
        ) mm
        <where>
            <if test="writeOffStatus!=null and writeOffStatus!=''">
                and mm.writeOffStatus = #{writeOffStatus}
            </if>
        </where>
        ORDER BY mm.arrive_succ desc,mm.spid desc,mm.adjust_date desc
    </select>

    <select id="getWeekClientBills" resultMap="SmsSpProvinceSummaryResultMap">
        select mm.* from (
        SELECT
        sps.spid,
        sp.sp_name,
        DATE_FORMAT(min(sps.stat_time),'%Y-%m-%d') adjust_date,
        DATE_FORMAT(min(sps.stat_time),'%Y-%m') `month`,
        sp.is_api_billing,
        se.full_name,
        SUM(ifnull(sps.submit_succ, 0 )) submit_succ,
        SUM(ifnull(sps.arrive_succ, 0 )) arrive_succ,
        SUM(ifnull(sps.arrive_fail, 0 )) arrive_fail,
        SUM(ifnull(sps.submit_fail, 0 )) submit_fail,
        sps.sp_price
        FROM
        sms_sp_province_summary sps inner join
        sms_sp_info sp on sps.spid = sp.id left join
        sms_enterprise se on se.id = sp.eid
        <where>
            sps.stat_time &gt;= #{startTime}
            AND sps.stat_time &lt; #{endTime}
            AND sp.billing_type = 1
            <if test="spIdList!=null">
                AND sps.spid in
                <foreach collection="spIdList" close=")" open="(" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY sps.spid,sps.sp_price,sp.sp_name,sp.is_api_billing,se.full_name
        ) mm
        <where>
            <if test="writeOffStatus!=null and writeOffStatus!=''">
                and mm.writeOffStatus = #{writeOffStatus}
            </if>
        </where>
        ORDER BY mm.arrive_succ desc,mm.spid desc,mm.adjust_date desc
    </select>

    <select id="getChannelBills" parameterType="SmsSpProvinceSummary" resultMap="SmsSpProvinceSummaryResultMap">
        SELECT
        sps.channelid,
        m.channel_name,
        DATE_FORMAT(min(sps.total_time),'%Y-%m-%d') adjust_date,
        DATE_FORMAT(min(sps.total_time),'%Y-%m') `month`,
        min(sps.total_time) total_time,
        m.supplier_name,
        SUM(
        ifnull( sps.SUBMITRESP_SUCC, 0 )) submit_succ,
        SUM(
        ifnull( sps.REPORT_SUCC, 0 )) arrive_succ,
        SUM(
        ifnull( sps.REPORT_FAIL, 0 )) arrive_fail,
        SUM(
        ifnull( sps.SUBMITRESP_FAIL, 0 )) submit_fail,
        sps.channel_price
        FROM
        (
        SELECT
        channelid,
        total_time,
        IFNULL(price,0) channel_price,
        SUM( SUBMITRESP_SUCC ) SUBMITRESP_SUCC,
        SUM( REPORT_SUCC ) REPORT_SUCC,
        SUM( REPORT_FAIL ) REPORT_FAIL,
        SUM( SUBMITRESP_FAIL ) SUBMITRESP_FAIL
        FROM
        pa_day_count
        WHERE
        total_time &gt;= #{startTime}
        AND total_time &lt; #{endTime}
        <if test="channelIdList!=null">
            AND channelid in
            <foreach collection="channelIdList" close=")" open="(" separator="," item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY channelid, total_time,channel_price
        ) sps
        LEFT JOIN ( SELECT sc.id, cs.supplier_name, sc.channel_name FROM (SELECT id,channel_name,supplier_id FROM sms_send_channel_info UNION SELECT id,channel_name,NULL as supplier_id FROM  mms_send_channel_info ) sc LEFT JOIN
        channel_supplier cs ON sc.supplier_id = cs.id ) AS m ON sps.channelid = m.id
        GROUP BY
        sps.channelid,
        sps.channel_price,
        m.channel_name,
        m.supplier_name
        ORDER BY
        arrive_succ DESC,
        sps.channelid DESC,
        total_time DESC;
    </select>

</mapper>