puzhibing
2023-03-15 79962435853baf5a28e08461f46a831fffa1a4b0
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.UserActivityDiscount1;
import com.stylefeng.guns.modular.system.dao.UserActivityDiscount1Mapper;
import com.stylefeng.guns.modular.system.service.ITCompanyService;
import com.stylefeng.guns.modular.system.service.IUserActivityDiscount1Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 用户活动-折扣1 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-06-19
 */
@Service
public class UserActivityDiscount1ServiceImpl extends ServiceImpl<UserActivityDiscount1Mapper, UserActivityDiscount1> implements IUserActivityDiscount1Service {
 
    @Resource
    private UserActivityDiscount1Mapper userActivityDiscount1Mapper;
 
    @Autowired
    private ITCompanyService companyService;
 
 
    /**
     * 获取折扣统计
     * @param name
     * @param time
     * @param companyId
     * @param offset
     * @param limit
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryDiscount1(String name, String time, Integer companyId, Integer offset, Integer limit) throws Exception {
        String start = null;
        String end = null;
        if(ToolUtil.isNotEmpty(time)){
            start = time.split(" - ")[0];
            end = time.split(" - ")[1];
        }
        List<Map<String, Object>> list = userActivityDiscount1Mapper.queryDiscount1(name, start, end, companyId, offset, limit);
        int i = userActivityDiscount1Mapper.queryDiscount1Count(name, start, end, companyId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("total", i);
        return map;
    }
 
 
    /**
     * 下载折扣优惠
     * @param name
     * @param time
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public HSSFWorkbook downloadDiscount1(String name, String time, Integer companyId) throws Exception {
        String start = null;
        String end = null;
        if(ToolUtil.isNotEmpty(time)){
            start = time.split(" - ")[0];
            end = time.split(" - ")[1];
        }
        List<Map<String, Object>> maps = userActivityDiscount1Mapper.queryDiscount1(name, start, end, companyId, null, null);
 
        List<List<String>> lists = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("活动名称:" + name);
        list.add("起止时间:" + time);
        list.add("运营商:" + (null != companyId ? companyService.selectById(companyId).getName() : ""));
        lists.add(list);
        list = new ArrayList<>();
        list.add("时间");
        list.add("使用情况");
        list.add("");
        lists.add(list);
        list = new ArrayList<>();
        list.add("");
        list.add("折扣参与人数");
        list.add("折扣金额");
        lists.add(list);
 
        List<List<List<String>>> lists1 = new ArrayList<>();
        List<List<String>> lists2 = new ArrayList<>();
        for(Map<String, Object> map : maps){
            List<String> list2 = new ArrayList<>();
            list2.add(null != map.get("time") ? map.get("time").toString() : "");
            list2.add(null != map.get("userNum") ? map.get("userNum").toString() : "");
            list2.add(null != map.get("discountMoney") ? map.get("discountMoney").toString() : "");
            lists2.add(list2);
        }
        lists1.add(lists2);
 
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        hssfSheet.setColumnWidth(0, 6 * 256);
        hssfSheet.setDefaultRowHeightInPoints(20f);
        for(int i = 0; i < lists.size(); i++){
            HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题)
            HSSFCellStyle style = hssfWorkbook.createCellStyle();
            HSSFFont font = hssfWorkbook.createFont();
            font.setBold(true);
            style.setFont(font);
            style.setAlignment(HorizontalAlignment.CENTER);
            for (int l = 0; l < lists.get(i).size(); l++) {
                HSSFCell hssfCell = hssfRow.createCell(l);
                hssfCell.setCellType(CellType.STRING);//设置表格类型
                hssfCell.setCellValue(lists.get(i).get(l));
                hssfCell.setCellStyle(style);
                if(l > 0) {
                    hssfSheet.setColumnWidth(l , 20 * 256);
                }
 
            }
        }
        //这个就是合并单元格
        //参数说明:1:开始行 2:结束行  3:开始列 4:结束列
        //比如我要合并 第二行到第四行的    第六列到第八列     sheet.addMergedRegion(new CellRangeAddress(1,3,5,7));
        hssfSheet.addMergedRegion(new CellRangeAddress(1,2,0,0));
        hssfSheet.addMergedRegion(new CellRangeAddress(1,1,1,2));
 
 
        for(int i = 0; i < lists1.size(); i++){
            //将数据添加到表格中
            List<String> data = null;
            for (int l = 0; l < lists1.get(i).size(); l++) {
                HSSFRow row = hssfSheet.createRow(l + 3);
                data = lists1.get(i).get(l);
                for (int j = 0; j < data.size(); j++) {
                    HSSFCell hssfCell = row.createCell(j);
                    hssfCell.setCellType(CellType.STRING);//设置表格类型
                    hssfCell.setCellValue(data.get(j));
                }
            }
        }
        return hssfWorkbook;
    }
 
 
    /**
     * 获取折扣优惠详情
     * @param name
     * @param time
     * @param companyId
     * @param offset
     * @param limit
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryDiscountInfo(String name, String time, Integer companyId, Integer offset, Integer limit) throws Exception {
        String start = null;
        String end = null;
        if(ToolUtil.isNotEmpty(time)){
            start = time.split(" - ")[0];
            end = time.split(" - ")[1];
        }
        List<Map<String, Object>> list = userActivityDiscount1Mapper.queryDiscountInfo(name, start, end, companyId, offset, limit);
        int i = userActivityDiscount1Mapper.queryDiscountInfoCount(name, start, end, companyId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("total", i);
        return map;
    }
 
 
    /**
     * 下载折扣优惠详情
     * @param name
     * @param time
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public HSSFWorkbook downloadDiscountInfo(String name, String time, Integer companyId) throws Exception {
        String start = null;
        String end = null;
        if(ToolUtil.isNotEmpty(time)){
            start = time.split(" - ")[0];
            end = time.split(" - ")[1];
        }
        List<Map<String, Object>> maps = userActivityDiscount1Mapper.queryDiscountInfo(name, start, end, companyId, null, null);
 
        List<List<String>> lists = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("活动名称:" + name);
        list.add("起止时间:" + time);
        list.add("运营商:");
        list.add(null != companyId ? companyService.selectById(companyId).getName() : "");
        lists.add(list);
        list = new ArrayList<>();
        list.add("打折时间");
        list.add("打折使用人");
        list.add("打折使用人电话");
        list.add("打折金额");
        lists.add(list);
 
        List<List<List<String>>> lists1 = new ArrayList<>();
        List<List<String>> lists2 = new ArrayList<>();
        for(Map<String, Object> map : maps){
            List<String> list2 = new ArrayList<>();
            list2.add(null != map.get("time") ? map.get("time").toString() : "");
            list2.add(null != map.get("name") ? map.get("name").toString() : "");
            list2.add(null != map.get("phone") ? map.get("phone").toString() : "");
            list2.add(null != map.get("discountMoney") ? map.get("discountMoney").toString() : "");
            lists2.add(list2);
        }
        lists1.add(lists2);
 
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        hssfSheet.setColumnWidth(0, 6 * 256);
        hssfSheet.setDefaultRowHeightInPoints(20f);
        for(int i = 0; i < lists.size(); i++){
            HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题)
            HSSFCellStyle style = hssfWorkbook.createCellStyle();
            HSSFFont font = hssfWorkbook.createFont();
            font.setBold(true);
            style.setFont(font);
            style.setAlignment(HorizontalAlignment.CENTER);
            for (int l = 0; l < lists.get(i).size(); l++) {
                HSSFCell hssfCell = hssfRow.createCell(l);
                hssfCell.setCellType(CellType.STRING);//设置表格类型
                hssfCell.setCellValue(lists.get(i).get(l));
                hssfCell.setCellStyle(style);
                if(l > 0) {
                    hssfSheet.setColumnWidth(l , 20 * 256);
                }
 
            }
        }
 
        for(int i = 0; i < lists1.size(); i++){
            //将数据添加到表格中
            List<String> data = null;
            for (int l = 0; l < lists1.get(i).size(); l++) {
                HSSFRow row = hssfSheet.createRow(l + 2);
                data = lists1.get(i).get(l);
                for (int j = 0; j < data.size(); j++) {
                    HSSFCell hssfCell = row.createCell(j);
                    hssfCell.setCellType(CellType.STRING);//设置表格类型
                    hssfCell.setCellValue(data.get(j));
                }
            }
        }
        return hssfWorkbook;
    }
}