<?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.gree.mall.manager.commonmapper.DailyMapper">
    <update id="bindWorkerNumber2">
        UPDATE
            (
                SELECT
                    u.id_card,
                    wb.websit_id,
                    wb.worker_number,
                    u.mobile,
                    u.nick_name
                FROM
                    user u, websit_user wb
                WHERE
                    u.company_wechat_id = wb.company_wechat_id
                    AND u.user_id = wb.user_id
                    AND u.apply_type = 'WORKER'
                    AND wb.examine_status = 'OK'
                ) a
            JOIN
            settle_daily_reduce_cost c
                ON c.repair_worker_name = a.nick_name AND c.repair_worker_mobile = a.mobile AND c.websit_number = a.websit_id
        SET
            c.worker_number = a.worker_number
        WHERE
            c.company_wechat_id = #{companyWechatId}
            AND c.import_batch_no = #{importBatchNo}
    </update>
    <update id="bindWorkerNumber">
        UPDATE
            (
                SELECT
                    u.id_card,
                    wb.websit_id,
                    wb.worker_number,
                    u.mobile,
                    u.nick_name
                FROM
                    user u, websit_user wb
                WHERE
                    u.company_wechat_id = wb.company_wechat_id
                  AND u.user_id = wb.user_id
                  AND u.apply_type = 'WORKER'
                  AND wb.examine_status = 'OK'
            ) a
            JOIN settle_daily_import_summary_item c
                ON c.repair_worker_name = a.nick_name AND c.repair_worker_mobile = a.mobile AND c.websit_number = a.websit_id
        SET
            c.worker_number = a.worker_number
        WHERE
            c.company_wechat_id = #{companyWechatId}
            AND c.import_batch_no = #{importBatchNo}
    </update>
    <select id="queryDistinctOrderNo" resultType="java.lang.String">
        select dispatch_order_no from settle_daily_import_summary_item
        where
            company_wechat_id = #{companyWechatId}
        AND order_status in ('已转结算待汇总','待退旧件','待总部日审','待总部审批','待总部日结审批','已汇总')
        group by concat(self_no,dispatch_order_no)
        having count(1)>1
    </select>
    <select id="queryDailyTotalCost" resultType="com.gree.mall.manager.bean.settle.repair.DailyTotalCostBean">
        select
        b.idcard as 'idCard',
        a.worker_number ,
        a.repair_worker_name ,
        a.summary_name,
        a.summary_number,
        a.websit_number,
        a.websit_name,
        a.company_wechat_id,
        a.company_wechat_name,
        sum(a.total_fee) AS 'total_fee',
        count(a.dispatch_order_no) AS repairCount,
        a.repair_worker_mobile AS 'mobile'
        from settle_daily_import_summary_item a
        join (
            select DISTINCT a.worker_number, a.idcard from settle_daily_bank_account a
        ) b on a.worker_number=b.worker_number
        where
        company_wechat_id = #{companyWechatId}
        <if test="status != null and status.size()> 0">
            and summary_status in
            <foreach collection="status" item="statusSItem" index="index"
                     open="(" close=")" separator=",">
                #{statusSItem}
            </foreach>
        </if>
        <if test="needSummaryStatus != null and needSummaryStatus.size()> 0">
            and order_status in
            <foreach collection="needSummaryStatus" item="statusItem" index="index"
                     open="(" close=")" separator=",">
                #{statusItem}
            </foreach>
        </if>
        <if test="importBatchNo !=null and importBatchNo !=''">
            and import_batch_no =#{importBatchNo}
        </if>
        group by worker_number
        order by total_fee desc
    </select>

    <select id="queryIncrDecrByBatch"
            resultType="com.gree.mall.manager.bean.settle.repair.DailyIncrDecrCostCountBean">
        select
        sdidc.*,
        sum(cost_amount) totalAmount
        from
        settle_daily_incr_decr_cost sdidc
        where
        company_wechat_id = #{companyWechatId}
        AND do_status = #{status}
        <if test="importBatchNo != '' and importBatchNo != null">
            and import_batch_no = #{importBatchNo}
        </if>
        group by
        service_number,websit_number
    </select>
    <select id="querySummaryNumber" resultType="java.util.Map">
        select distinct summary_number as summaryNumber  from  settle_daily_issue_summary_record  where company_wechat_id = #{companyWechatId} AND summary_batch_no= #{summaryBatchNo} and status in (1,4)
    </select>

    <select id="querySummaryMonthNumber" resultType="java.util.Map">
        select distinct summary_number as summaryNumber  from  settle_daily_issue_summary_record  where company_wechat_id = #{companyWechatId} AND summary_batch_no= #{summaryBatchNo} and status in (1,4)
    </select>

    <select id="queryIssueSalaryMonthBankCardInfo"
            resultType="com.gree.mall.manager.bean.settle.repair.DailyWokerBankCardMonthBean">
        SELECT * FROM settle_daily_issue_summary_month_record a
        JOIN user aa on a.service_number = aa.worker_number AND aa.apply_type = 'WORKER'
        JOIN settle_daily_bank_account b ON aa.id_card = b.idcard
        WHERE
        a.company_wechat_id = #{companyWechatId}
        AND a.status IN(1,4)
          AND is_month_calculate = 1

        <if test="summaryBatchNo != null and summaryBatchNo !=''">
            AND a.summary_batch_no = #{summaryBatchNo}
        </if>

        <if test="summaryNumber != null and summaryNumber !=''">
            AND a.summary_number = #{summaryNumber}
        </if>
    </select>

    <select id="queryIssueSalaryAndBankCardInfo" resultType="com.gree.mall.manager.bean.settle.repair.DailyWorkerBankCardBean">
        select  a.*,b.* from  settle_daily_issue_summary_record a
        JOIN user aa on a.service_number = aa.worker_number AND aa.apply_type = 'WORKER'
        join settle_daily_bank_account b  on  aa.id_card = b.idcard
        where
            a.company_wechat_id = #{companyWechatId}
            AND a.status in (1,4) and a.issue_cost > 0
            and a.service_name = aa.nick_name
            <if test="summaryBatchNo != null and summaryBatchNo !=''">
                and a.summary_batch_no = #{summaryBatchNo}
            </if>

            <if test="summaryNumber != null and summaryNumber !=''">
                and a.summary_number = #{summaryNumber}
            </if>
        group by a.id
    </select>

    <select id="summaryDayList" resultType="com.gree.mall.manager.bean.settle.repair.DailySummaryDataBean">
        SELECT
            id AS 'issueSalaryId',
            summary_batch_no,
            month,
            COUNT(DISTINCT (service_number)) AS 'summaryNum',
            SUM(repair_count) AS 'summaryOrderNum',
            SUM(repair_total_amount) AS 'totalRepairCostC',
            IF(FIND_IN_SET('4', GROUP_CONCAT(DISTINCT status)) > 0 || FIND_IN_SET('1', GROUP_CONCAT(DISTINCT status)) > 0, 1, status) AS 'status',
            issue_time,
            issue_by,
            SUM(should_reduce_cost) AS 'reduceCostC',
            SUM(incr_decr_cost) AS 'incrDecrCostC',
            SUM(should_emp_insurance_cost) AS 'injuryCostC',
            SUM(should_residual_insurance_cost) AS 'residualCostC',
            SUM(withhold_cost) AS 'withholdCostC',
            SUM(issue_cost) AS 'issueCostC',
            summary_number,
            summary_name
        FROM
        settle_daily_issue_summary_record a
        <where>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                AND a.company_wechat_id IN
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                AND a.websit_number IN
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="summaryBatchNo != null and summaryBatchNo != ''">
                AND summary_batch_no = #{summaryBatchNo}
            </if>
        </where>
        GROUP BY
        summary_number
    </select>

    <select id="cwSummaryList" resultType="com.gree.mall.manager.bean.settle.repair.DailySummaryDataBean">
        SELECT
            t.*
        FROM
        (
            SELECT
                summary_batch_no,
                month,
                COUNT(DISTINCT (service_number)) AS 'summaryNum',
                SUM(repair_count) AS 'summaryOrderNum',
                SUM(repair_total_amount) AS 'totalRepairCostC',
                SUM(issue_cost) AS 'issueCostC',
                IF(FIND_IN_SET('4', GROUP_CONCAT(DISTINCT IF(issue_cost > 0, status, 2))) > 0 || FIND_IN_SET('1',GROUP_CONCAT(DISTINCT IF(issue_cost > 0, status, 2))) > 0, 1, status) AS 'status',
                status AS 'groupStatus',
                summary_by,
                summary_time,
                issue_time,
                issue_by,
                create_time
            FROM
                settle_daily_issue_summary_record a
                <where>
                    <if test="companyWechatIds != null and companyWechatIds.size > 0">
                        AND a.company_wechat_id IN
                        <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                    <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                        AND a.websit_number IN
                        <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                    <if test="summaryStartTime != null and summaryStartTime !='' and summaryEndTime != null and summaryEndTime != ''">
                        AND summary_time BETWEEN #{summaryStartTime} AND concat(#{summaryEndTime},' 23:59:59')
                    </if>

                    <if test="issueStartTime != null and issueStartTime !='' and issueEndTime != null and issueEndTime != ''">
                        AND issue_time BETWEEN #{issueStartTime} AND concat(#{issueEndTime},' 23:59:59')
                    </if>

                    <if test="summaryBatchNo != null and summaryBatchNo != ''">
                        AND summary_batch_no = #{summaryBatchNo}
                    </if>
                </where>
            GROUP BY
            summary_batch_no
        ) t
        <where>
            <if test="status !='' and status != null">
                t.status = #{status}
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="workerDetailList" resultType="com.gree.mall.manager.bean.settle.repair.DailyWorkerIssueSalaryBean">
        select
        a.*,b.bank_account ,b.bank_account_name,deposit_bank,c.issue_time ,c.issue_by,c.status as issueStatus
        from settle_daily_import_summary_item a
        left JOIN user aa on a.worker_number = aa.worker_number AND aa.apply_type = 'WORKER'
        left join settle_daily_bank_account b on aa.id_card = b.idcard AND a.websit_number = b.websit_id
        left join settle_daily_issue_summary_record c  on c.id  = a.issue_salary_id

        <where>

            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                AND a.company_wechat_id IN
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                AND a.websit_number IN
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="issueSalaryId != null and issueSalaryId != ''">
                and  a.issue_salary_id = #{issueSalaryId}
            </if>
        </where>
        GROUP BY a.id, a.worker_number
    </select>

    <select id="cwSummaryMonthList" resultType="com.gree.mall.manager.bean.settle.repair.DailyNotMonthSummaryCountBean">
        select
        t.*
        from
        (
            select
                summary_batch_no ,
                `month` 'summaryMonth',
                sum(repair_count) 'summaryOrderCount',
                count(distinct service_number) 'summaryPersonCount',
                sum(repair_total_amount) 'summaryCost',
                sum(month_send_cost) 'issueCostC',
                status 'groupStatus',
                if(find_in_set('4',group_concat(distinct status)) > 0 || find_in_set('1',group_concat(distinct status)) > 0  ,1,status) 'issueStatus',
                summary_by ,
                summary_time ,
                issue_time ,
                issue_by,
                create_time
            from
            settle_daily_issue_summary_month_record a
            <where>

                <if test="companyWechatIds != null and companyWechatIds.size > 0">
                    AND a.company_wechat_id IN
                    <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                    AND a.websit_number IN
                    <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="summaryStartTime != null and summaryStartTime !='' and summaryEndTime != null and summaryEndTime != ''">
                    and summary_time between #{summaryStartTime} and concat(#{summaryEndTime},' 23:59:59')
                </if>

                <if test="issueStartTime != null and issueStartTime !='' and issueEndTime != null and issueEndTime != ''">
                    and issue_time between #{issueStartTime} and concat(#{issueEndTime},' 23:59:59')
                </if>

                <if test="summaryBatchNo != null and summaryBatchNo != ''">
                    and summary_batch_no = #{summaryBatchNo}
                </if>

            </where>
            group by summary_batch_no
        ) t
        <where>
            <if test="status != null and status !=''">
                and t.issueStatus = #{status}
            </if>
        </where>
        order by t.create_time desc

    </select>

    <select id="monthlySummaryList" resultType="com.gree.mall.manager.bean.settle.repair.DailyNotMonthSummaryCountBean">
        select
            summary_number,
            summary_name ,
            summary_batch_no ,
            `month` 'summaryMonth',
            sum(repair_count) 'summaryOrderCount',
            count(distinct service_number) 'summaryPersonCount',
            sum(repair_total_amount) 'summaryCost',
            status 'groupStatus',
            if(find_in_set('4',group_concat(distinct status)) > 0 || find_in_set('1',group_concat(distinct status)) > 0  ,1,status) 'issueStatus',
            summary_by ,
            summary_time ,
            issue_time ,
            issue_by,
            sum(should_emp_insurance_cost +should_residual_insurance_cost) 'empCost',
            sum(withhold_cost ) 'withholdCost',
            sum(incr_decr_cost) 'decrCost',
            sum(reduce_cost) 'reduceCost',
            sum(income_cost) 'incomeCost',
            sum(issue_cost) 'issueCost',
            websit_name,
            websit_number
        from
            settle_daily_issue_summary_month_record a
        <where>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                AND a.company_wechat_id IN
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                AND a.websit_number IN
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="summaryBatchNo != null and summaryBatchNo !=''">
                and a.summary_batch_no = #{summaryBatchNo}
            </if>
        </where>

        group by summary_number
        order by summary_time desc
    </select>

    <select id="monthlyWebsiteList" resultType="com.gree.mall.manager.bean.settle.repair.DailyNotMonthSummaryCountBean">
        select
        summary_number,
        summary_name ,
        summary_batch_no ,
        `month` 'summaryMonth',
        count(1) 'summaryOrderCount',
        count(distinct service_number) 'summaryPersonCount',
        sum(repair_total_amount) 'summaryCost',
        status 'issueStatus',
        summary_by ,
        summary_time ,
        issue_time ,
        issue_by,
        sum(should_emp_insurance_cost +should_residual_insurance_cost) 'empCost',
        sum(withhold_cost ) 'withholdCost',
        sum(incr_decr_cost) 'decrCost',
        sum(reduce_cost) 'reduceCost',
        sum(income_cost) 'incomeCost',
        sum(issue_cost) 'issueCost',
        websit_name,
        websit_number
        from
        settle_daily_issue_summary_month_record a
        <where>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                AND a.company_wechat_id IN
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                AND a.websit_number IN
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="summaryBatchNo != null and summaryBatchNo !=''">
                and a.summary_batch_no = #{summaryBatchNo}
            </if>

        </where>

        group by websit_number
        order by summary_time desc
    </select>

    <select id="monthlyDetailList" resultType="com.gree.mall.manager.bean.settle.repair.DailyWorkerIssueSalaryBean">
        select
        a.*,b.status 'issueStatus' ,b.issue_time ,c.bank_account_name ,c.deposit_bank ,c.bank_account
        from
        settle_daily_import_summary_item a
        left join
        settle_daily_issue_summary_record b on a.issue_salary_id =b.id
        left join user aa on a.worker_number = aa.worker_number AND aa.apply_type = 'WORKER'
        left join settle_daily_bank_account c  on  aa.id_card = c.idcard
        <where>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                AND a.company_wechat_id IN
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                AND a.websit_number IN
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="serviceNumber != null and serviceNumber != ''">
                and b.service_number = #{serviceNumber}
            </if>

            <if test="summaryMonthBatchNo != null and summaryMonthBatchNo != ''">
                and b.summary_month_batch_no = #{summaryMonthBatchNo}
            </if>

            <if test="summaryNumber != null and summaryNumber != ''">
                and a.summary_number = #{summaryNumber}
            </if>

        </where>

    </select>

    <select id="queryMonthTotalCost" resultType="com.gree.mall.manager.bean.settle.repair.DailyMonthTotalBean">
        select
            sum(repair_total_amount) 'repairTotalAmount',
            sum(should_reduce_cost) 'reduceCost',
            sum(incr_decr_cost) 'incrDecrCost',
            sum(issue_cost) 'issueCost',
            sum(withhold_cost) 'withholdTotalCost',
            sum(should_residual_insurance_cost) 'residualInsuranceCostC',
            sum(should_emp_insurance_cost) 'empInsuranceCostC',
            sum(repair_count) 'repairCountC',
            id_card ,
            mobile,
            service_name,
            service_number,
            websit_number ,
            websit_name ,
            summary_name ,
            summary_number,
            company_wechat_id,
            company_wechat_name
        from
            settle_daily_issue_summary_record a
        where
            status = 2 and a.install = 0 AND company_wechat_id = #{companyWechatId}
        group by
            service_number ,summary_number
    </select>

    <select id="queryNotMonthSummaryData" resultType="com.gree.mall.manager.bean.settle.repair.DailyNotMonthSummaryCountBean">
        select
            count(distinct(service_number)) 'summaryPersonCount',
            count(1) 'summaryOrderCount',
            sum(repair_total_amount) 'summaryCost'
        from
            settle_daily_issue_summary_record
        where
            is_month_summary = false
            AND company_wechat_id = #{companyWechatId}
    </select>
</mapper>