李林
2024-01-02 bbd18f555ee3c0ed368b226cde7a3c64db4a4802
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
package com.yuanchu.mom.service.impl;
 
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.Wrappers;
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.DataReportingPageDto;
import com.yuanchu.mom.dto.ProductCountDto;
import com.yuanchu.mom.dto.RegistrantCountDto;
import com.yuanchu.mom.pojo.DataReporting;
import com.yuanchu.mom.service.DataReportingService;
import com.yuanchu.mom.mapper.DataReportingMapper;
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.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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
 
    @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.setCreateUser(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());
        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) {
        }
        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) registrantCountDto.setCreateUser(map1.get("userId"));
        map.put("body", dataReportingMapper.selectRegistrantCountDtoPageList(page, QueryWrappers.queryWrappers(registrantCountDto)));
        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) {
        int userId = Integer.parseInt(JSONUtil.parseObj(new Jwt().readJWT(ServletUtils.getRequest().getHeader("token")).get("data")).get("id") + "");
        Map<String, String> map = dataReportingMapper.selectUser(userId);
        for (DataReporting dataReporting : list) {
            if (dataReporting.getName() == null) dataReporting.setName(map.get("account"));
            DataReporting one = dataReportingMapper.selectOne(Wrappers.<DataReporting>lambdaQuery().eq(DataReporting::getProduct, dataReporting.getProduct()).eq(DataReporting::getName, dataReporting.getName()));
            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) {
            }
            if (ObjectUtil.isEmpty(one)) {
                dataReporting.setRegistrant(map.get("name"));
                dataReporting.setDepartment(map.get("department"));
                dataReportingMapper.insert(dataReporting);
            } else {
                dataReporting.setId(one.getId());
                dataReporting.setName(null);
                dataReportingMapper.updateById(dataReporting);
            }
        }
    }
 
    @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[]{});
        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;
    }
}