<?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.zcxk.dao.RevenueMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.dto.MonthRevenueDto" id="revenueDtoMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="collectDateString" column="collect_date_string" jdbcType="VARCHAR"/>
        <result property="receivableTotalAmount" column="receivable_total_amount" jdbcType="VARCHAR"/>
        <result property="receivedTotalAmount" column="received_total_amount" jdbcType="VARCHAR"/>
        <result property="meterReadingUsage" column="meter_reading_usage" jdbcType="VARCHAR"/>
        <result property="generalUserUsage" column="general_user_usage" jdbcType="VARCHAR"/>
        <result property="bigUserUsage" column="big_user_usage" jdbcType="VARCHAR"/>
        <result property="generalUserUsageReceived" column="general_user_usage_received" jdbcType="VARCHAR"/>
        <result property="bigUserUsageReceived" column="big_user_usage_received" jdbcType="VARCHAR"/>
        <collection property="monthSellList" ofType="com.zcxk.dto.MonthSellwaterDto" javaType="list">
            <result property="id" column="sell_id" jdbcType="INTEGER"/>
            <result property="propertyId" column="property_id" jdbcType="INTEGER"/>
            <result property="waterUsage" column="water_usage" jdbcType="VARCHAR"/>
            <result property="propertyName" column="property_name" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

    <!--查询本月售水信息-->
    <select id="selectMonthSell" resultType="com.zcxk.dto.MonthSellwaterDto">
        select t2.property_name ,sum(t1.water_usage) water_usage
        from sms_water_property t2
        left join sms_month_sellwater t1 on t1.property_id=t2.id and t1.collect_date>=date_format(curdate(), '%Y-%m' )
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
        <if test="companyOrgId != null and companyOrgId !=''">
            and t1.company_org_id=#{companyOrgId}
        </if>
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
                and t1.company_org_id in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
        </if>
        inner join (
            select distinct property_id
            from sms_month_sellwater
            where 1=1
            <if test="tenantId != null and tenantId != ''">
                and tenant_id=#{tenantId}
            </if>
            <if test="companyOrgId != null and companyOrgId !=''">
                and company_org_id=#{companyOrgId}
            </if>
        )t3 on t2.id=t3.property_id
        group by t2.property_name
        order by water_usage desc,t2.property_name
    </select>

    <!--查询近6个月的售水量-->
    <select id="selectMonthSellTotal" resultType="com.zcxk.dto.MonthSellwaterDto">
        select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,sum(t1.water_usage) water_usage
        from sms_month_sellwater t1
        where t1.collect_date>=date_sub(date_format(curdate(), '%Y-%m-1' ), interval 5 month)
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
        <if test="companyOrgId != null and companyOrgId !=''">
            and t1.company_org_id=#{companyOrgId}
        </if>
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
                and t1.company_org_id in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
        </if>
        group by collect_date_string
        order by collect_date_string
    </select>

    <!--查询月营收情况-->
    <select id="selectMonthRevenue" resultType="com.zcxk.dto.MonthRevenueDto">
        select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,sum(user_meter_count) user_meter_count,
            sum(receivable_total_amount) receivable_total_amount,sum(received_total_amount) received_total_amount,
            sum(general_user_usage) general_user_usage,sum(big_user_usage) big_user_usage,
            if(sum(receivable_total_amount)=0,null,sum(received_total_amount)/sum(receivable_total_amount)*100) water_fee_recovery_rate,
            if(sum(receivable_total_amount)=0,null,sum(general_user_usage)/sum(receivable_total_amount)*100) general_user_percent,
            if(sum(receivable_total_amount)=0,null,sum(big_user_usage)/sum(receivable_total_amount)*100) big_user_percent
        from sms_month_revenue t1
        where t1.collect_date>=date_sub(date_format(curdate(), '%y-%m-1' ), interval #{months}-1 month)
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
        <if test="companyOrgId != null and companyOrgId !=''">
            and t1.company_org_id=#{companyOrgId}
        </if>
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
                and t1.company_org_id in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
        </if>
        group by collect_date_string
        order by collect_date_string
    </select>

    <!--查询本月各分公司的营收情况-->
    <select id="selectCompanyRevenue" resultType="com.zcxk.dto.MonthRevenueDto">
        select company_org_id,sum(receivable_total_amount) receivable_total_amount,sum(received_total_amount) received_total_amount,
            if(sum(receivable_total_amount)=0,null,sum(received_total_amount)/sum(receivable_total_amount)*100) water_fee_recovery_rate
        from sms_month_revenue t1
        where t1.collect_date>=date_format(curdate(), '%y-%m-1' )
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
                and t1.company_org_id in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
        </if>
        group by company_org_id
        order by company_org_id
    </select>

    <!--App总览-生产-售水量(本月，本年)-->
    <select id="selectOverviewRevenue" resultType="com.zcxk.dto.MonthRevenueDto">
        select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,
        sum(METER_READING_USAGE) meter_reading_usage,
        sum(RECEIVABLE_TOTAL_AMOUNT) receivable_total_amount,
        sum(RECEIVED_TOTAL_AMOUNT) received_total_amount,
        sum(GENERAL_USER_USAGE) general_user_usage,
        sum(BIG_USER_USAGE) big_user_usage
        from sms_month_revenue t1
        where t1.collect_date>=date_sub(date_format(curdate(), '%y-%m-1' ), interval #{months}-1 month)
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
        <if test="companyOrgId != null and companyOrgId !=''">
            and t1.company_org_id=#{companyOrgId}
        </if>
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
                and t1.company_org_id in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
        </if>
        group by collect_date_string
        order by collect_date_string
    </select>

    <!--App总览-生产数据-按时间段查询(环比)-->
    <select id="selectRevenueByDate" resultType="com.zcxk.dto.MonthRevenueDto">
        select date_format(t1.collect_date, '%Y-%m' ) collect_date_string,
        sum(METER_READING_USAGE) meter_reading_usage,
        sum(RECEIVABLE_TOTAL_AMOUNT) receivable_total_amount,
        sum(RECEIVED_TOTAL_AMOUNT) received_total_amount,
        sum(GENERAL_USER_USAGE) general_user_usage,
        sum(BIG_USER_USAGE) big_user_usage,
        from sms_month_revenue t1
        where t1.collect_date>=date_format(#{startDate}, '%y-%m-1' ) and t1.collect_date &lt;= date_format(#{endDate}, '%y-%m-1')
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
        <if test="companyOrgId != null and companyOrgId !=''">
            and t1.company_org_id=#{companyOrgId}
        </if>
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2 or permissonType == 1">
                and t1.company_org_id in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
        </if>
        group by collect_date_string
        order by collect_date_string
    </select>

    <!--查询是否当月有营收数据-->
    <select id="isExistThisMonthRevenue" resultType="java.lang.Integer">
        select count(1)
        from  sms_month_revenue t1
        where t1.collect_date>=date_format(curdate(), '%Y-%m' )
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id=#{tenantId}
        </if>
    </select>
    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into sms_month_revenue(`tenant_id`, `company_org_id`, `collect_date`, `user_meter_count`, `user_meter_add_count`,
        `meter_reading_count`, `meter_reading_usage`, `charging_usage`, `meter_reading_finished_rate`, `meter_reading_arrival_rate`,
        `meter_reading_error_rate`, `receivable_total_amount`, `received_total_amount`, `invoiced_total_amount`,
        `arrears_total_amount`, `general_user_usage`, `big_user_usage`, `remark`, `date_create`, `create_by`, `date_update`, `update_by`,
        `general_user_usage_received`, `big_user_usage_received`)
        values (#{tenantId},#{companyOrgId},#{collectDate},#{userMeterCount},#{userMeterAddCount},#{meterReadingCount},
        #{meterReadingUsage},#{chargingUsage},#{meterReadingFinishedRate},#{meterReadingArrivalRate},#{meterReadingErrorRate},
        #{receivableTotalAmount},#{receivedTotalAmount},#{invoicedTotalAmount},#{arrearsTotalAmount},#{generalUserUsage},
        #{bigUserUsage},#{remark},now(),null,now(),null,#{generalUserUsageReceived},#{bigUserUsageReceived})
    </insert>
    <!-- 通过租户id修改当月数据 -->
    <update id="update">
        update  sms_month_revenue
        <set>
            <if test="userMeterCount != null and userMeterCount != 0">
                user_meter_count  = #{userMeterCount},
            </if>
            <if test="userMeterAddCount != null and userMeterAddCount != 0">
                user_meter_add_count  = #{userMeterAddCount},
            </if>
            date_update  = now()
        </set>
        where tenant_id  = #{tenantId} and collect_date>=date_format(curdate(), '%Y-%m-1' )
    </update>
    <!--查询该租户一级公司id-->
    <select id="selectCompanyIdByTenant" resultType="java.lang.Integer">
        select id
        from  uims.uims_org
        where parent_org_id=0 and tenant_id=#{tenantId}
    </select>
    <!-- 新增售水信息-->
    <insert id="insertSellWater">
        insert into sms_month_sellwater (tenant_id,company_org_id,property_id,collect_date,water_usage,remark,create_by)
        values(#{tenantId}, #{companyOrgId}, #{propertyId}, #{collectDate}, #{waterUsage},#{remark},#{createBy})
    </insert>
    <!-- 通过id修改营收数据 -->
    <update id="updateRevenue">
        update  sms_month_revenue
        <set>
            <if test="collectDate != null">
                collect_date  = #{collectDate},
            </if>
            <if test="userMeterAddCount != null">
                user_meter_add_count  = #{userMeterAddCount},
            </if>
            <if test="userMeterCount != null">
                user_meter_count  = #{userMeterCount},
            </if>
            <if test="meterReadingCount != null">
                meter_reading_count  = #{meterReadingCount},
            </if>
            meter_reading_usage  = #{meterReadingUsage},
            <if test="chargingUsage != null">
                charging_usage  = #{chargingUsage},
            </if>
            <if test="meterReadingFinishedRate != null">
                meter_reading_finished_rate  = #{meterReadingFinishedRate},
            </if>
            <if test="meterReadingArrivalRate != null">
                meter_reading_arrival_rate  = #{meterReadingArrivalRate},
            </if>
            <if test="meterReadingErrorRate != null">
                meter_reading_error_rate  = #{meterReadingErrorRate},
            </if>
            receivable_total_amount  = #{receivableTotalAmount},
            received_total_amount  = #{receivedTotalAmount},
            <if test="invoicedTotalAmount != null">
                invoiced_total_amount  = #{invoicedTotalAmount},
            </if>
            <if test="arrearsTotalAmount != null">
                arrears_total_amount  = #{arrearsTotalAmount},
            </if>
            general_user_usage  = #{generalUserUsage},
            big_user_usage  = #{bigUserUsage},
            <if test="remark != null">
                remark  = #{remark},
            </if>
            <if test="updateBy != null">
                update_by  = #{updateBy},
            </if>
            general_user_usage_received  = #{generalUserUsageReceived},
            big_user_usage_received  = #{bigUserUsageReceived},
        </set>
        where id  = #{id}
    </update>
    <!-- 通过id修改售水数据 -->
    <update id="updateSellWater">
        update  sms_month_sellwater
        <set>
            <if test="collectDate != null">
                collect_date  = #{collectDate},
            </if>
            water_usage  = #{waterUsage},
            <if test="remark != null">
                remark  = #{remark},
            </if>
            <if test="updateBy != null">
                update_by  = #{updateBy},
            </if>
        </set>
        where id  = #{id}
    </update>
    <!-- 通过公司id查询营收数据 -->
    <select id="selectList" resultMap="revenueDtoMap">
        select t1.id,date_format(t1.collect_date,'%Y-%m') as collect_date_string,meter_reading_usage*10000 as meter_reading_usage
               ,receivable_total_amount*10000 as receivable_total_amount,received_total_amount*10000 as received_total_amount,big_user_usage*10000 as big_user_usage
               ,general_user_usage*10000 as general_user_usage,t2.id as property_id,t3.id as sell_id,t3.water_usage*10000 as water_usage,t2.property_name
               ,general_user_usage_received*10000 as general_user_usage_received,big_user_usage_received*10000 as big_user_usage_received
        from sms_month_revenue t1
        left join sms_water_property t2 on t1.company_org_id=t2.company_org_id and t1.tenant_id=t2.tenant_id and t2.status=1
        left join sms_month_sellwater t3 on t2.id=t3.property_id and date_format(t1.collect_date,'%Y-%m')= date_format(t3.collect_date,'%Y-%m')
        where t1.company_org_id = #{companyOrgId} and t1.collect_date >= concat(#{startDate}, '-1')
            and t1.collect_date  &lt; concat(#{endDate}, '-1') + interval 1 month
        order by t1.collect_date
    </select>
    <!--查询某月是否有营收数据-->
    <select id="isExistMonthRevenue" resultType="java.lang.Integer">
        select count(1)
        from  sms_month_revenue t1
        where date_format(t1.collect_date, '%Y-%m' ) = #{collectDateString}
            and t1.company_org_Id=#{companyOrgId}
            <if test="id != null">
                and id != #{id}
            </if>
    </select>
</mapper>