value
2024-04-13 e569dcc6da8b7be3ce3983664eb4b0b535425531
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package com.yuanchu.mom.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.dto.DataComparisonDto;
import com.yuanchu.mom.dto.DataReportingPageDto;
import com.yuanchu.mom.dto.ProductCountDto;
import com.yuanchu.mom.dto.RegistrantCountDto;
import com.yuanchu.mom.mapper.AuthMapper;
import com.yuanchu.mom.mapper.FansSubmitMapper;
import com.yuanchu.mom.mapper.FinanceSubmitMapper;
import com.yuanchu.mom.pojo.DataReporting;
import com.yuanchu.mom.pojo.FansSubmit;
import com.yuanchu.mom.pojo.FinanceSubmit;
import com.yuanchu.mom.service.DataReportingService;
import com.yuanchu.mom.mapper.DataReportingMapper;
import com.yuanchu.mom.service.FansSubmitService;
import com.yuanchu.mom.service.FinanceSubmitService;
import com.yuanchu.mom.utils.Jwt;
import com.yuanchu.mom.utils.QueryWrappers;
import com.yuanchu.mom.utils.ServletUtils;
import lombok.AllArgsConstructor;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @author Administrator
 * @description 针对表【data_reporting(数据上报)】的数据库操作Service实现
 * @createDate 2023-12-23 10:39:43
 */
@Service
public class DataReportingServiceImpl extends ServiceImpl<DataReportingMapper, DataReporting>
        implements DataReportingService {
 
    @Resource
    private DataReportingMapper dataReportingMapper;
 
    @Resource
    private GetLook getLook;
 
    @Value("${outPath}")
    private String outPath;
 
    @Resource
    private FansSubmitMapper fansSubmitMapper;
 
    @Resource
    private FansSubmitService fansSubmitService;
 
    @Resource
    private FinanceSubmitService financeSubmitService;
 
    @Resource
    private FinanceSubmitMapper financeSubmitMapper;
 
    @Resource
    private AuthMapper authMapper;
 
    @Override
    public Map<String, Object> selectDataReportingList(IPage<DataReportingPageDto> page, DataReportingPageDto dataReportingPageDto) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectDataReportingList");
        if (map1.get("look") == 1) dataReportingPageDto.setUpdateUser(map1.get("userId"));
        Integer power = authMapper.isPower(map1.get("userId"), "dataReportingPower");
        if(power == 1){
           dataReportingPageDto.setDepartment(dataReportingMapper.getUserDepartmentById(map1.get("userId")));
        }
        map.put("head", PrintChina.printChina(DataReportingPageDto.class));
        map.put("body", dataReportingMapper.selectDataReportingDtoPageList(page, QueryWrappers.queryWrappers(dataReportingPageDto)));
        return map;
    }
 
    @Override
    public int updateDataReporting(DataReporting dataReporting) {
        DataReporting dataReporting2 = new DataReporting();
        dataReporting2.setId(dataReporting.getId());
        dataReporting2.setInternalCosts(dataReporting.getInternalCosts());
        dataReporting2.setCustomerCosts(dataReporting.getCustomerCosts());
        dataReporting2.setCustomerRebate(dataReporting.getCustomerRebate());
        dataReporting2.setRemark(dataReporting.getRemark());
        dataReporting2.setVxCard(dataReporting.getVxCard());
        try {
            dataReporting2.setRebateConsumption(dataReporting.getAccountConsumption().divide(dataReporting.getCustomerRebate(), 2, RoundingMode.HALF_UP));
        } catch (Exception e) {
        }
        try {
            dataReporting2.setConsumption(dataReporting.getCustomerCosts().multiply(BigDecimal.valueOf(dataReporting.getFansAdd())));
        } catch (Exception e) {
        }
        try {
            dataReporting2.setDiscountedConsumption(dataReporting.getConsumption().divide(dataReporting.getAgentRebate(), 2, RoundingMode.HALF_UP));
        } catch (Exception e) {
        }
        try {
            dataReporting2.setActualCost(dataReporting.getAccountConsumption().divide(new BigDecimal(dataReporting.getFansAdd()), 2, RoundingMode.HALF_UP));
        } catch (Exception e) {
        }
        try {
            dataReporting2.setProfit(dataReporting.getRebateConsumption().subtract(dataReporting.getConsumption()));
        } catch (Exception e) {
        }
        String format = dataReportingMapper.selectById(dataReporting.getId()).getCreateTime().plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        List<FansSubmit> fansSubmits = fansSubmitMapper.selectList(Wrappers.<FansSubmit>lambdaQuery().eq(FansSubmit::getProduct, dataReporting.getProduct()).eq(FansSubmit::getVx, dataReporting.getVxCard()).select(FansSubmit::getFansAdd).like(FansSubmit::getCreateTime, format));
        Integer sum = 0;
        for (FansSubmit fansSubmit : fansSubmits) {
            sum += fansSubmit.getFansAdd();
        }
        if (dataReporting.getFansAdd()!=null||dataReporting.getFansAdd()!=0){
            dataReporting2.setFansAdd(dataReporting.getFansAdd());
        }else{
            dataReporting2.setFansAdd(sum);
        }
        return dataReportingMapper.updateById(dataReporting2);
    }
 
    @Override
    public Map<String, Object> selectProductCountDtoPageList(IPage<ProductCountDto> page, ProductCountDto productCountDto) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(ProductCountDto.class));
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectProductCountDtoPageList");
        if (map1.get("look") == 1) productCountDto.setCreateUser(map1.get("userId"));
        map.put("body", dataReportingMapper.selectProductCountDtoPageList(page, QueryWrappers.queryWrappers(productCountDto)));
        return map;
    }
 
    @Override
    public Map<String, Object> selectRegistrantCountDtoPageList(IPage<RegistrantCountDto> page, RegistrantCountDto registrantCountDto) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(RegistrantCountDto.class));
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectRegistrantCountDtoPageList");
        if (map1.get("look") == 1) {
            String name = dataReportingMapper.getUserNameById(map1.get("userId"));
            registrantCountDto.setName2(name);
        };
        map.put("body", dataReportingMapper.selectRegistrantCountDtoPageList(page, QueryWrappers.queryWrappers(registrantCountDto), registrantCountDto.getStartTime(), registrantCountDto.getEndTime()));
        return map;
    }
 
    @Override
    public Map<String, Object> getDataList() {
        Map<String, Object> map = new HashMap<>();
        map.put("data1", dataReportingMapper.getDataFor1());
        map.put("data2", dataReportingMapper.getDataFor2());
        map.put("data3", dataReportingMapper.getDataFor3());
        map.put("data4", dataReportingMapper.getDataFor4());
        map.put("data5", dataReportingMapper.getDataFor5());
        return map;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDataReporting(List<DataReporting> list, String date, String param) {
        int userId = Integer.parseInt(JSONUtil.parseObj(new Jwt().readJWT(ServletUtils.getRequest().getHeader("token")).get("data")).get("id") + "");
        Map<String, String> map = dataReportingMapper.selectUser(userId);
        String date2 = date;
        if(date == null) {
            date2 = fansSubmitService.getYesterday();
            date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        } else date = LocalDateTime.parse(date+"T00:00:00").plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        for (DataReporting dataReporting : list) {
            FinanceSubmit financeSubmit = financeSubmitMapper.selectOne(Wrappers.<FinanceSubmit>lambdaQuery().eq(FinanceSubmit::getCompany, dataReporting.getAgent()).eq(FinanceSubmit::getName, dataReporting.getName()).select(FinanceSubmit::getEmployeeRebate).like(FinanceSubmit::getCreateTime, date).orderByDesc(FinanceSubmit::getId).last("limit 1"));
            if(ObjectUtil.isNotEmpty(financeSubmit)){
                dataReporting.setAgentRebate(financeSubmit.getEmployeeRebate());
            }
            if (dataReporting.getName() == null || dataReporting.getName().equals(""))
                dataReporting.setName(map.get("account"));
//            DataReporting one = dataReportingMapper.selectOne(Wrappers.<DataReporting>lambdaQuery().eq(DataReporting::getProduct, dataReporting.getProduct()).eq(DataReporting::getName, dataReporting.getName()).eq(DataReporting::getCreateUser, userId).like(DataReporting::getCreateTime, date));
            try {
                dataReporting.setRebateConsumption(dataReporting.getAccountConsumption().divide(dataReporting.getCustomerRebate(), 2, RoundingMode.HALF_UP));
            } catch (Exception e) {
            }
            try {
                dataReporting.setConsumption(dataReporting.getCustomerCosts().multiply(new BigDecimal(dataReporting.getFansAdd())));
            } catch (Exception e) {
            }
            try {
                dataReporting.setDiscountedConsumption(dataReporting.getConsumption().divide(dataReporting.getAgentRebate(), 2, RoundingMode.HALF_UP));
            } catch (Exception e) {
            }
            try {
                dataReporting.setActualCost(dataReporting.getAccountConsumption().divide(new BigDecimal(dataReporting.getFansAdd()), 2, RoundingMode.HALF_UP));
            } catch (Exception e) {
            }
            try {
                dataReporting.setProfit(dataReporting.getRebateConsumption().subtract(dataReporting.getConsumption()));
            } catch (Exception e) {
            }
            try {
                dataReporting.setRemark(dataReporting.getConsumption().subtract(dataReporting.getRebateConsumption()));
            } catch (Exception e) {
            }
            dataReporting.setRegistrant(map.get("name"));
            dataReporting.setDepartment(map.get("department"));
            dataReporting.setCreateTime(LocalDateTime.parse(date2 + " " + new SimpleDateFormat("HH:mm:ss").format(new Date()), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            DataReporting dataReporting1 = dataReportingMapper.selectOne(Wrappers.<DataReporting>lambdaQuery()
                    .eq(DataReporting::getName, dataReporting.getName())
                    .eq(DataReporting::getAccountConsumption, dataReporting.getAccountConsumption())
                    .eq(DataReporting::getShowNum, dataReporting.getShowNum())
                    .eq(DataReporting::getClick, dataReporting.getClick()));
            if(BeanUtil.isNotEmpty(dataReporting1)){
                dataReporting.setId(dataReporting1.getId());
                dataReportingMapper.updateById(dataReporting);
            }else{
                dataReportingMapper.insert(dataReporting);
            }
        }
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delDataReporting(Integer id) {
        return dataReportingMapper.deleteById(id);
    }
 
    @Override
    public String downDataReportingFile(IPage<DataReportingPageDto> page, DataReportingPageDto dataReportingPageDto) {
        Map<String, Object> map = selectDataReportingList(page, dataReportingPageDto);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String name = "数据上报" + time + ".csv";
        String path = outPath + name;
        CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_GBK);
        writer.write(new String[]{"导出时间:" + time});
        writer.write(new String[]{});
        writer.write(new String[]{"时间范围:" + dataReportingPageDto.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"))});
        writer.write(new String[]{});
        writer.write(new String[]{});
        List<Map<String, String>> head = JSON.parseObject(JSON.toJSONString(map.get("head")), List.class);
        String[] str = new String[head.size()];
        for (int i = 0; i < head.size(); i++) {
            str[i] = head.get(i).get("value");
        }
        writer.write(str);
        JSON.parseObject(JSON.toJSONString(map.get("body")), IPage.class).getRecords().forEach(a -> {
            Map<String, Object> map1 = JSON.parseObject(JSON.toJSONString(a), Map.class);
            for (int i = 0; i < head.size(); i++) {
                str[i] = map1.get(head.get(i).get("label")) == null ? null : map1.get(head.get(i).get("label")) + "";
            }
            writer.write(str);
        });
        writer.close();
        return name;
    }
 
    @Override
    public String downProductCountFile(IPage<ProductCountDto> page, ProductCountDto productCountDto) {
        Map<String, Object> map = selectProductCountDtoPageList(page, productCountDto);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String name = "项目数据统计" + time + ".csv";
        String path = outPath + name;
        CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_GBK);
        writer.write(new String[]{"导出时间:" + time});
        writer.write(new String[]{});
        writer.write(new String[]{});
        writer.write(new String[]{});
        writer.write(new String[]{});
        List<Map<String, String>> head = JSON.parseObject(JSON.toJSONString(map.get("head")), List.class);
        String[] str = new String[head.size()];
        for (int i = 0; i < head.size(); i++) {
            str[i] = head.get(i).get("value");
        }
        writer.write(str);
        JSON.parseObject(JSON.toJSONString(map.get("body")), IPage.class).getRecords().forEach(a -> {
            Map<String, Object> map1 = JSON.parseObject(JSON.toJSONString(a), Map.class);
            for (int i = 0; i < head.size(); i++) {
                str[i] = map1.get(head.get(i).get("label")) == null ? null : map1.get(head.get(i).get("label")) + "";
            }
            writer.write(str);
        });
        writer.close();
        return name;
    }
 
    @Override
    public String downRegistrantCountFile(IPage<RegistrantCountDto> page, RegistrantCountDto registrantCountDto) {
        Map<String, Object> map = selectRegistrantCountDtoPageList(page, registrantCountDto);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String name = "员工数据统计" + time + ".csv";
        String path = outPath + name;
        CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_GBK);
        writer.write(new String[]{"导出时间:" + time});
        writer.write(new String[]{});
        writer.write(new String[]{});
        writer.write(new String[]{});
        writer.write(new String[]{});
        List<Map<String, String>> head = JSON.parseObject(JSON.toJSONString(map.get("head")), List.class);
        String[] str = new String[head.size()];
        for (int i = 0; i < head.size(); i++) {
            str[i] = head.get(i).get("value");
        }
        writer.write(str);
        JSON.parseObject(JSON.toJSONString(map.get("body")), IPage.class).getRecords().forEach(a -> {
            Map<String, Object> map1 = JSON.parseObject(JSON.toJSONString(a), Map.class);
            for (int i = 0; i < head.size(); i++) {
                str[i] = map1.get(head.get(i).get("label")) == null ? null : map1.get(head.get(i).get("label")) + "";
            }
            writer.write(str);
        });
        writer.close();
        return name;
    }
 
    @Override
    public String downFansSubmitFile(IPage<FansSubmit> page, FansSubmit fansSubmit) {
        Map<String, Object> map = fansSubmitService.selectFansSubmitList(page, fansSubmit);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String name = "粉丝上报" + time + ".csv";
        String path = outPath + name;
        CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_GBK);
        writer.write(new String[]{"导出时间:" + time});
        writer.write(new String[]{});
        writer.write(new String[]{"时间范围:" + fansSubmit.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"))});
        writer.write(new String[]{});
        writer.write(new String[]{});
        List<Map<String, String>> head = JSON.parseObject(JSON.toJSONString(map.get("head")), List.class);
        String[] str = new String[head.size()];
        for (int i = 0; i < head.size(); i++) {
            str[i] = head.get(i).get("value");
        }
        writer.write(str);
        JSON.parseObject(JSON.toJSONString(map.get("body")), IPage.class).getRecords().forEach(a -> {
            Map<String, Object> map1 = JSON.parseObject(JSON.toJSONString(a), Map.class);
            for (int i = 0; i < head.size(); i++) {
                str[i] = map1.get(head.get(i).get("label")) == null ? null : map1.get(head.get(i).get("label")) + "";
            }
            writer.write(str);
        });
        writer.close();
        return name;
    }
 
    @Override
    public String downFinanceSubmitFile(IPage<FinanceSubmit> page, FinanceSubmit financeSubmit) {
        Map<String, Object> map = financeSubmitService.selectFinanceSubmitList(page, financeSubmit);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String name = "财务上报" + time + ".csv";
        String path = outPath + name;
        CsvWriter writer = CsvUtil.getWriter(path, CharsetUtil.CHARSET_GBK);
        writer.write(new String[]{"导出时间:" + time});
        writer.write(new String[]{});
        writer.write(new String[]{"时间范围:" + financeSubmit.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"))});
        writer.write(new String[]{});
        writer.write(new String[]{});
        List<Map<String, String>> head = JSON.parseObject(JSON.toJSONString(map.get("head")), List.class);
        String[] str = new String[head.size()];
        for (int i = 0; i < head.size(); i++) {
            str[i] = head.get(i).get("value");
        }
        writer.write(str);
        JSON.parseObject(JSON.toJSONString(map.get("body")), IPage.class).getRecords().forEach(a -> {
            Map<String, Object> map1 = JSON.parseObject(JSON.toJSONString(a), Map.class);
            for (int i = 0; i < head.size(); i++) {
                str[i] = map1.get(head.get(i).get("label")) == null ? null : map1.get(head.get(i).get("label")) + "";
            }
            writer.write(str);
        });
        writer.close();
        return name;
    }
 
    /**
     * 获取数据对比列表
     */
    @Override
    public Map<String, Object> selectDataComparisonDtoPageList(Page page, DataComparisonDto dataComparisonDto) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(DataComparisonDto.class));
 
        if (StringUtils.isEmpty(dataComparisonDto.getName())
                || StringUtils.isEmpty(dataComparisonDto.getComparisonName())) {
            map.put("body", new Page<DataComparisonDto>());
        } else {
            IPage<DataComparisonDto> dataReportingPageDtoIPage = dataReportingMapper.selectDataComparisonDtoPageList(page, dataComparisonDto);
            map.put("body", dataReportingPageDtoIPage);
        }
        return map;
    }
 
    @Override
    public void deleteAllData(String date) {
        List<DataReporting> list = dataReportingMapper.selectList(Wrappers.<DataReporting>lambdaQuery().eq(DataReporting::getCreateUser, getLook.selectPowerByMethodAndUserId("selectDataReportingList").get("userId")).like(DataReporting::getCreateTime, date));
        for (DataReporting reporting : list) {
            delDataReporting(reporting.getId());
        }
    }
 
    @Override
    public List<String> selectDataReportingForCreateUserNames(String time) {
        return dataReportingMapper.selectDataReportingForCreateUserNames(time.split(" ")[0]);
    }
 
    @Override
    public List<String> selectDataReportingForProduct(String time) {
        return dataReportingMapper.selectDataReportingForProduct(time.split(" ")[0]);
    }
 
    @Override
    public int updateVxCards(List<DataReporting> list) {
        for (DataReporting reporting : list) {
            updateDataReporting(reporting);
        }
        return 1;
    }
 
}