P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?xml version="1.0" encoding="UTF-8" ?>
    
<sqls>
    <dataSpace name="target">
 
        <sql name="summaryMonthTarget">
            INSERT INTO business_data_summary (
                id, idx, title_code, title_name, position_id, year, season, month, 
                month_amt_target, month_amt_achieve, month_qty_target, month_qty_achieve) 
            select UUID_SHORT()  id, null idx, '@{code}' title_code, '@{name}' title_name, target.position_id, 
                year(CURRENT_DATE) year, FLOOR((@{month}-1)/3) +1 season, @{month} month, 
                month_amt_target, ifnull(month_amt_achieve,0) month_amt_achieve,
                month_qty_target, ifnull(month_qty_achieve,0) month_qty_achieve
            from (
                select position_id,sum(ifnull(@{monthAmtTarget},0)) month_amt_target, sum(ifnull(@{monthQtyTarget},0)) month_qty_target
                from @{targetTable} target 
                left join @{positionTable} positionTable on @{joinSql}
                where target.state_code = 'Open' and year = year(CURRENT_DATE) and position_id is not null 
                group by position_id ) target
            left join (
                    select @{documentPosition} position_id, 
                        sum(ifnull(@{amtAchieve},0)) month_amt_achieve, 
                        sum(ifnull(@{qtyAchieve},0)) month_qty_achieve  
                    from @{document} document
                    left join @{documentDetail} document_detail on document_detail.parent_id = document.id 
                    where document.state_code in ('Open','Whole','Partial') and month(doc_date) = @{month} and year(CURRENT_DATE) = year(doc_date) 
                    group by position_id 
            ) document on document.position_id = target.position_id
        </sql>
        
        <sql name="summaryMonthAchieveWithoutTarget">
            INSERT INTO business_data_summary (
                id, idx, title_code, title_name, position_id, year, season, month, 
                month_amt_target, month_amt_achieve, month_qty_target, month_qty_achieve) 
            select UUID_SHORT() id, null idx, '@{code}' title_code, '@{name}' title_name, document.position_id, 
                year(CURRENT_DATE) year, FLOOR((@{month}-1)/3) +1 season, @{month} month, 
                ifnull(month_amt_target, 0) month_amt_target, ifnull(month_amt_achieve,0) month_amt_achieve,
                ifnull(month_qty_target, 0) month_qty_target, ifnull(month_qty_achieve,0) month_qty_achieve
            from (
                    select @{documentPosition} position_id, 
                        sum(ifnull(@{amtAchieve},0)) month_amt_achieve, 
                        sum(ifnull(@{qtyAchieve},0)) month_qty_achieve  
                    from @{document} document
                    left join @{documentDetail} document_detail on document_detail.parent_id = document.id 
                    where document.state_code in ('Open','Whole','Partial') and month(doc_date) = @{month} and year(CURRENT_DATE) = year(doc_date) 
                    group by position_id 
            ) document 
            left join (
                select position_id, sum(ifnull(@{monthAmtTarget},0)) month_amt_target, sum(ifnull(@{monthQtyTarget},0)) month_qty_target
                from @{targetTable} target 
                left join @{positionTable} positionTable on @{joinSql}
                where target.state_code = 'Open' and year = year(CURRENT_DATE) and position_id is not null 
                group by position_id ) target on document.position_id = target.position_id
            where target.position_id is null
        </sql>
            
        <sql name="summarySeasonTarget">
            update business_data_summary
            inner join (
                select year, season ,position_id,title_code,
                sum(month_amt_target) season_amt_target,sum(month_amt_achieve) season_amt_achieve,
                sum(month_qty_target) season_qty_target,sum(month_qty_achieve) season_qty_achieve
                from business_data_summary 
                where title_code =  '@{code}' AND year = Year(CURRENT_DATE)
                group by season, position_id, title_code
            )  season on season.season = business_data_summary.season 
                and season.year = business_data_summary.year
                and season.position_id = business_data_summary.position_id
                and season.title_code = business_data_summary.title_code
            set business_data_summary.season_amt_target = season.season_amt_target,
                business_data_summary.season_amt_achieve = season.season_amt_achieve,
                business_data_summary.season_qty_target = season.season_qty_target,
                business_data_summary.season_qty_achieve = season.season_qty_achieve
        </sql>        
            
        <sql name="summaryYearTarget">
            update business_data_summary
            inner join (
                select year, position_id,title_code,
                sum(month_amt_target) year_amt_target,sum(month_amt_achieve) year_amt_achieve,
                sum(month_qty_target) year_qty_target,sum(month_qty_achieve) year_qty_achieve
                from business_data_summary 
                where title_code = '@{code}' AND year = Year(CURRENT_DATE)
                group by year, position_id, title_code
            ) year on year.year = business_data_summary.year
                and year.position_id = business_data_summary.position_id
                and year.title_code = business_data_summary.title_code
            set business_data_summary.year_amt_target = year.year_amt_target,
                business_data_summary.year_amt_achieve = year.year_amt_achieve,
                business_data_summary.year_qty_target = year.year_qty_target,
                business_data_summary.year_qty_achieve = year.year_qty_achieve
        </sql>        
            
        <sql name="getAuthorizeDealer">
            select year, md_org_account_hospital.bu_id,md_org_account_hospital.company_id,md_org_account_hospital.org_id, account_id, org_name, org_code 
            from @{targetTable} temp_md_target_dealer_hospital 
            inner join md_org_account_hospital on md_org_account_hospital.hospital_id = temp_md_target_dealer_hospital.hospital_id 
                    and year(md_org_account_hospital.date_to) = temp_md_target_dealer_hospital.year 
                    and ifnull(md_org_account_hospital.department, '--')  = ifnull(temp_md_target_dealer_hospital.department, '--')
            where md_org_account_hospital.category_code = 'Dealer' 
                and md_org_account_hospital.is_active = 'T' 
                and md_org_account_hospital.state_code = 'Open'
                and @{filter}
            group by md_org_account_hospital.bu_id,md_org_account_hospital.company_id,md_org_account_hospital.org_id,md_org_account_hospital.account_id, temp_md_target_dealer_hospital.year
        </sql>    
            
        <sql name="collectDealerTarget">
            select 
                md5(UUID_SHORT()) id, md_target_dealer_hospital.year, md_target_dealer_hospital.company_id, md_target_dealer_hospital.bu_id, 
                md_org_account_hospital.org_id, md_org_account_hospital.account_id,  
                'Open'state_code, '生效' state_name, '@{typeCode}' type_code, '@{typeName}' type_name, 
                md_target_dealer_hospital.product_id, md_target_dealer_hospital.product_name, 
                sum(amt_year) amt_year, 
                sum(amt_q1) amt_q1, sum(amt_q2) amt_q2, sum(amt_q3) amt_q3, sum(amt_q4)amt_q4, 
                sum(amt_m1) amt_m1, sum(amt_m2) amt_m2, sum(amt_m3) amt_m3, sum(amt_m4) amt_m4, sum(amt_m5) amt_m5, sum(amt_m6) amt_m6, 
                sum(amt_m7) amt_m7, sum(amt_m8) amt_m8, sum(amt_m9) amt_m9, sum(amt_m10) amt_m10, sum(amt_m11) amt_m11, sum(amt_m12) amt_m12, 
                sum(qty_year) qty_year, 
                sum(qty_q1) qty_q1, sum(qty_q2) qty_q2, sum(qty_q3) qty_q3, sum(qty_q4) qty_q4, 
                sum(qty_m1) qty_m1, sum(qty_m2) qty_m2, sum(qty_m3) qty_m3, sum(qty_m4) qty_m4, sum(qty_m5) qty_m5, sum(qty_m6) qty_m6, 
                sum(qty_m7) qty_m7, sum(qty_m8) qty_m8, sum(qty_m9) qty_m9, sum(qty_m10) qty_m10, sum(qty_m11) qty_m11, sum(qty_m12) qty_m12, 
                md_target_dealer_hospital.creator_id, md_target_dealer_hospital.creator_name, md_target_dealer_hospital.create_date, md_target_dealer_hospital.update_date 
            from @{targetTable} md_target_dealer_hospital
            inner join (
                select distinct year(date_to) year,  bu_id,company_id,org_id, account_id, org_name, org_code,hospital_id,department
                from md_org_account_hospital
                where category_code = 'Dealer' and is_active = 'T' and state_code = 'Open'
                 and md_org_account_hospital.bu_id  = '@{buId}' 
                 and md_org_account_hospital.company_id  = '@{companyId}' 
                 and md_org_account_hospital.org_id  = '@{orgId}' 
            ) md_org_account_hospital on md_org_account_hospital.hospital_id = md_target_dealer_hospital.hospital_id 
                    and md_org_account_hospital.bu_id = md_target_dealer_hospital.bu_id 
                    and md_org_account_hospital.company_id = md_target_dealer_hospital.company_id 
                    and md_org_account_hospital.year = md_target_dealer_hospital.year
                    and ifnull(md_org_account_hospital.department, '--')  = ifnull(md_target_dealer_hospital.department, '--')
            where md_target_dealer_hospital.year = @{year}
            group by md_target_dealer_hospital.product_id,md_org_account_hospital.bu_id,md_org_account_hospital.company_id,md_org_account_hospital.org_id
        </sql>    
            
    </dataSpace>        
</sqls>