ShareMoneyMain.java
6.15 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.glsx.bzi.cost.income;
import com.glsx.conf.ConfigurationManager;
import com.glsx.connection.MysqlConn;
import com.glsx.util.DateUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* 设备收入分摊
*/
public class ShareMoneyMain {
public static void main(String[] args) {
Connection mysqlConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql_prd_login"));
// 直销配激活 代销(含广汇)配结算
//String channel = "('直销')";
//String channel = "('代销','广汇')";
//渠道 (原直销)
//String channelEx = "((settle_time >= '2022-09-01' and channel = '渠道') or (settle_time < '2022-09-01' and channel = '直销'))";
//String type2 = "active_time";
//直销 (原代销)
String channelEx = "((settle_time >= '2022-09-01' and channel in ('直销','广汇')) or" +
" (settle_time < '2022-09-01' and channel in ('代销','广汇')))";
String type2 = "settle_time";
int isReturn = 1; //是否退货
computeShareMoney(mysqlConn,channelEx, type2,isReturn);
MysqlConn.close(mysqlConn);
}
private static HashMap<String, String> classifyMerchant(Connection mysqlConn, String type) {
HashMap<String, String> classifyMap = new HashMap<>();
String sql = "SELECT settle_mer_name,channel FROM t_settle_merchant_channel_new where channel='" + type + "'";
MysqlConn.executeQuery(mysqlConn, sql, new MysqlConn.QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
while (rs.next()) {
classifyMap.put(rs.getString(1).trim(), rs.getString(2).trim());
}
}
});
// if (type.equals("直销"))
if (type.equals("广汇"))
classifyMap.put("广汇汽车", "广汇");
return classifyMap;
}
private static void computeShareMoney(Connection mysqlConn, String channel, String type2, int isReturn ) {
// 客户|(结算/激活)时间 -> 结算金额
// 结算客户分类
//HashMap<String, String> classifyMap = classifyMerchant(mysqlConn, channel);
TreeMap<String, Double> shareMoneyMap = new TreeMap<>();
String sql = "";
/*
if(isReturn == 0){
sql = "SELECT " + type2 + ",settle_merchant,pk_period,pk_price,hw_price,install_price from dj_2021_settle_details " +
"where device_type=8 and channel='" + channel +"' and " + type2 + " is not null ";// + " and " + type2 + " < '2021-01-01'";
}
else {
sql = "SELECT " + type2 + ",settle_merchant,pk_period,pk_price,hw_price,install_price from dj_2021_settle_details_return " +
"where channel='" + channel +"' and " + type2 + " is not null ";// + " and " + type2 + " < '2021-01-01'";
}*/
if(isReturn == 0){
sql = "SELECT " + type2 + ",settle_merchant,pk_period,pk_price,hw_price,install_price from dj_2021_settle_details " +
"where device_type=8 and " + channel +" and " + type2 + " is not null ";
}
else {
sql = "SELECT " + type2 + ",settle_merchant,pk_period,pk_price,hw_price,install_price from dj_2021_settle_details_return " +
"where device_type=8 and " + type2 + " is not null ";
}
MysqlConn.executeQuery(mysqlConn, sql, new MysqlConn.QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
while (rs.next()) {
String time = rs.getString(1);
String settle_merchant = rs.getString(2).trim();
int pk_period = rs.getInt(3);
double pkPrice = rs.getDouble(4);
double hwPrice = rs.getDouble(5);
double installPrice = rs.getDouble(6);
double price = pkPrice + hwPrice +installPrice;
//if (classifyMap.get(settle_merchant) == null) continue;
String monTime = "";
/*if (settle_merchant.equals("广汇汽车") && type2.equals("settle_time"))
monTime = DateUtils.getAfterMonth2(time);
else*/
monTime = DateUtils.convTime1(time);
double priceMonth = price/pk_period;
String key = "";
for (int i = 0; i < pk_period; i++) {
key = settle_merchant + "|" + monTime;
if (shareMoneyMap.get(key) == null)
shareMoneyMap.put(key, priceMonth);
else
shareMoneyMap.put(key, shareMoneyMap.get(key) + priceMonth);
monTime = DateUtils.getAfterMonth(monTime);
}
}
}
});
MysqlConn.executeUpdate(mysqlConn, "TRUNCATE TABLE dj_share_price");
String tmpsql = "INSERT INTO dj_share_price(settle_merchant,mon_time,price) values";
String values = "";
Integer total = 0;
for (Map.Entry<String, Double> entry : shareMoneyMap.entrySet()) {
String[] split = entry.getKey().split("\\|");
String settle_merchant = split[0];
String mon_time = split[1];
Double price = entry.getValue();
values += "('" + settle_merchant + "','" + mon_time + "'," + price + "),";
total += 1;
if (total % 1000 == 0){
tmpsql += values.substring(0, values.length() - 1);
MysqlConn.executeUpdate(mysqlConn, tmpsql);
tmpsql = "INSERT INTO dj_share_price(settle_merchant,mon_time,price) values";
values = "";
System.out.println("total = "+ total);
}
}
if (total % 1000 != 0){
tmpsql += values.substring(0, values.length() - 1);
MysqlConn.executeUpdate(mysqlConn, tmpsql);
}
System.out.println("done total = "+ total);
}
}