P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?xml version="1.0" encoding="UTF-8" ?>
<?DOCTYPE sqls SYSTEM "../config/sql.dtd" ?>
 
<sqls>
    <dataSpace name="discount">
        <sql name="lockDiscountBook">
            update @{documentTable} document set update_time = now()
            where document.id = '@{document_id}'
        </sql>
        
        <sql name="getDiscountBookFlowBasicFields">
        <![CDATA[
               select 
                   discount.bu_id,discount.org_id, md_bu.name bu_name, '@{batch_mark}' batchMark,
                discount.id discount_id, discount.oa_no discount_oa_no,
                   discount.code discount_code, discount.item_name discount_name,
                   discount.year, discount.season, discount.doc_date valid_from, discount.expire_date valid_to,discount.batch_mark,
                   discount.type_code, discount.type_name,
                   discount.biz_id, discount.biz_code, discount.biz_name,
                   discount.hospital_id, discount.hospital_code, discount.hospital_name,
                   discount.product_id, discount.product_code, discount.product_name,
                   @{documentTypeCode} operate_name, document.doc_date, discount.company_id, md_org.short_name company_name, 
                   document.code doc_code, @{region} region, document_detail.id document_detail_id, @{document_num} as amt                                          
            from @{detailTableName} document_detail
            left join @{tableName} document on @{documentDetailParentId} = document.id
            inner join discount_detail discount on discount.id = @{discountId} 
               left join md_bu on md_bu.id = discount.bu_id
               left join md_org on md_org.is_master = 'T' and md_org.id = discount.company_id 
               where @{filter}
        ]]>
        </sql>
        
        <sql name="writeUpdateDiscountDetail">
        <![CDATA[
            update discount_detail 
            set discount_detail.batch_mark = '@{batch_mark}', discount_detail.update_time = @{update_time} ,
            discount_detail.amt_used = discount_detail.amt_used - (
            select (sum(@{IfEmpty}(num_return, 0)) - sum(@{IfEmpty}(num_used, 0))) as num_used_change
                        from discount_flow 
                        where batch_mark = '@{batch_mark}' and record_operator = 'Update' and  discount_flow.rebate_id = discount_detail.id 
                        group by rebate_id ), 
            discount_detail.amt_freeze = discount_detail.amt_freeze + ( 
                select (sum(@{IfEmpty}(num_freeze, 0)) - sum(@{IfEmpty}(num_unfreeze, 0))) as num_frozen_change 
                from discount_flow 
                where batch_mark = '@{batch_mark}' and record_operator = 'Update'  and  discount_flow.rebate_id = discount_detail.id 
                group by rebate_id  ) 
                where EXISTS(
                select 1 from  discount_flow 
                where discount_flow.REBATE_ID = discount_detail.id 
                and batch_mark = '@{batch_mark}' and record_operator = 'Update' ) 
        ]]>
        </sql>            
                
        <sql name="resetDiscountDetailOtherFields">
        <![CDATA[
            update discount_detail book_detail
            set book_detail.amt_net = book_detail.amt_book - book_detail.amt_used,
                book_detail.amt_available = book_detail.amt_book - book_detail.amt_used - book_detail.amt_freeze
            where book_detail.batch_mark = '@{batch_mark}'    
        ]]>
        </sql>
 
        <sql name="getNegativeDiscountDetail">
            <![CDATA[
                   select count(1)
                   from discount_detail 
                   where batch_mark = '@{batch_mark}' 
                     and ((amt_available < 0) or (amt_net < 0)) 
               ]]>
        </sql>    
        
    </dataSpace>
    
</sqls>