java110
2021-06-16 38b7741d060048ebfd7be48cbc5980eeaeba287b
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
package com.java110.front.smo.assetImport.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.component.BaseComponentSMO;
import com.java110.core.context.IPageData;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.meterWater.ImportExportMeterWaterDto;
import com.java110.entity.component.ComponentValidateResult;
import com.java110.front.smo.assetImport.IImportMeterWaterFeeSMO;
import com.java110.utils.constant.ServiceConstant;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ImportExcelUtils;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @ClassName AssetImportSmoImpl
 * @Description TODO
 * @Author wuxw
 * @Date 2019/9/23 23:14
 * @Version 1.0
 * add by wuxw 2019/9/23
 **/
@Service("importMeterWaterFeeSMOImpl")
public class ImportMeterWaterFeeSMOImpl extends BaseComponentSMO implements IImportMeterWaterFeeSMO {
    private final static Logger logger = LoggerFactory.getLogger(ImportMeterWaterFeeSMOImpl.class);
 
    private static final int DEFAULT_ADD_FEE_COUNT = 500;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Override
    public ResponseEntity<String> importExcelData(IPageData pd, MultipartFile uploadFile) throws Exception {
 
        try {
            ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
 
            //InputStream is = uploadFile.getInputStream();
 
            Workbook workbook = ImportExcelUtils.createWorkbook(uploadFile);  //工作簿
 
            List<ImportExportMeterWaterDto> rooms = new ArrayList<ImportExportMeterWaterDto>();
 
            //获取楼信息
            getRooms(workbook, rooms);
            // 保存数据
            return dealExcelData(pd, rooms, result);
        } catch (Exception e) {
            logger.error("导入失败 ", e);
            return new ResponseEntity<String>("非常抱歉,您填写的模板数据有误:" + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }
 
    @Override
    public ResponseEntity<String> importExcelData2(IPageData pd, MultipartFile uploadFile) {
        try {
            ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
 
            //InputStream is = uploadFile.getInputStream();
 
            Workbook workbook = ImportExcelUtils.createWorkbook(uploadFile);  //工作簿
 
            List<ImportExportMeterWaterDto> rooms = new ArrayList<ImportExportMeterWaterDto>();
 
            //获取楼信息
            getRooms2(workbook, rooms);
            // 保存数据
            return dealExcelData(pd, rooms, result);
        } catch (Exception e) {
            logger.error("导入失败 ", e);
            return new ResponseEntity<String>("非常抱歉,您填写的模板数据有误:" + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }
 
    /**
     * 处理ExcelData数据
     */
    private ResponseEntity<String> dealExcelData(IPageData pd,
                                                 List<ImportExportMeterWaterDto> rooms,
 
                                                 ComponentValidateResult result) {
        ResponseEntity<String> responseEntity = null;
        //保存单元信息 和 楼栋信息
        responseEntity = savedRoomFees(pd, rooms, result);
 
        if (responseEntity == null || responseEntity.getStatusCode() != HttpStatus.OK) {
            return responseEntity;
        }
 
        return responseEntity;
    }
 
    private ResponseEntity<String> savedRoomFees(IPageData pd, List<ImportExportMeterWaterDto> rooms, ComponentValidateResult result) {
 
        JSONObject reqJson = JSONObject.parseObject(pd.getReqData());
        if (rooms.size() < 1) {
            throw new IllegalArgumentException("没有数据需要处理");
        }
 
        JSONObject data = JSONObject.parseObject(pd.getReqData());
        data.put("importFeeId", GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_feeId));
        data.put("storeId", result.getStoreId());
        data.put("userId", result.getUserId());
        data.put("configId", reqJson.getString("configId"));
        data.put("feeTypeCd", reqJson.getString("feeTypeCd"));
        data.put("communityId", result.getCommunityId());
 
        List<ImportExportMeterWaterDto> tmpImportRoomFees = new ArrayList<>();
        for (int roomIndex = 0; roomIndex < rooms.size(); roomIndex++) {
 
            tmpImportRoomFees.add(rooms.get(roomIndex));
 
            if (roomIndex % DEFAULT_ADD_FEE_COUNT == 0 && roomIndex != 0) {
 
                createRoomFee(pd, tmpImportRoomFees, data);
 
                tmpImportRoomFees = new ArrayList<>();
            }
        }
        if (tmpImportRoomFees != null && tmpImportRoomFees.size() > 0) {
 
            createRoomFee(pd, tmpImportRoomFees, data);
        }
        return ResultVo.success();
    }
 
    /**
     * 创建费用
     *
     * @param pd
     * @param tmpImportRoomFees
     */
    private void createRoomFee(IPageData pd, List<ImportExportMeterWaterDto> tmpImportRoomFees, JSONObject data) {
 
        JSONArray importRoomFees = JSONArray.parseArray(JSONObject.toJSONString(tmpImportRoomFees));
        data.put("importMeteWaterFees", importRoomFees);
 
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/meterWater/importMeterWater";
 
        ResponseEntity<String> responseEntity = this.callCenterService(restTemplate, pd, data.toJSONString(), apiUrl, HttpMethod.POST);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException(responseEntity.getBody());
        }
 
        JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
        if (ResultVo.CODE_OK != paramOut.getInteger("code")) {
            throw new IllegalArgumentException(paramOut.getString("msg"));
        }
 
    }
 
 
    /**
     * 获取 房屋信息
     *
     * @param workbook
     * @param rooms
     */
    private void getRooms(Workbook workbook, List<ImportExportMeterWaterDto> rooms) {
        Sheet sheet = null;
        sheet = ImportExcelUtils.getSheet(workbook, "房屋费用信息");
        List<Object[]> oList = ImportExcelUtils.listFromSheet(sheet);
        ImportExportMeterWaterDto importRoomFee = null;
        for (int osIndex = 0; osIndex < oList.size(); osIndex++) {
            Object[] os = oList.get(osIndex);
            if (osIndex == 0 || osIndex == 1) { // 第一行是 头部信息 直接跳过
                continue;
            }
            if (StringUtil.isNullOrNone(os[0])) {
                continue;
            }
            Assert.hasValue(os[1], (osIndex + 1) + "单元编号不能为空");
            Assert.hasValue(os[2], (osIndex + 1) + "房屋编号不能为空");
            Assert.hasValue(os[3], (osIndex + 1) + "上期度数不能为空");
            Assert.hasValue(os[4], (osIndex + 1) + "上期度数时间不能为空");
            Assert.hasValue(os[5], (osIndex + 1) + "本期度数不能为空");
            Assert.hasValue(os[6], (osIndex + 1) + "本期度数时间不能为空");
 
//
 
            String startTime = excelDoubleToDate(os[4].toString());
            String endTime = excelDoubleToDate(os[6].toString());
            Assert.isDate(startTime, DateUtil.DATE_FORMATE_STRING_B, (osIndex + 1) + "行开始时间格式错误 请填写YYYY-MM-DD 文本格式");
            Assert.isDate(endTime, DateUtil.DATE_FORMATE_STRING_B, (osIndex + 1) + "行结束时间格式错误 请填写YYYY-MM-DD 文本格式");
 
 
            importRoomFee = new ImportExportMeterWaterDto();
            importRoomFee.setFloorNum(os[0].toString());
            importRoomFee.setUnitNum(os[1].toString());
            importRoomFee.setRoomNum(os[2].toString());
            importRoomFee.setPreDegrees(os[3].toString());
            importRoomFee.setPreReadingTime(startTime);
            importRoomFee.setCurDegrees(os[5].toString());
            importRoomFee.setCurReadingTime(endTime);
            importRoomFee.setPrice(-1);
            rooms.add(importRoomFee);
        }
    }
 
    /**
     * 获取 房屋信息
     *
     * @param workbook
     * @param rooms
     */
    private void getRooms2(Workbook workbook, List<ImportExportMeterWaterDto> rooms) {
        Sheet sheet = null;
        sheet = ImportExcelUtils.getSheet(workbook, "房屋费用信息");
        List<Object[]> oList = ImportExcelUtils.listFromSheet(sheet);
        ImportExportMeterWaterDto importRoomFee = null;
        for (int osIndex = 0; osIndex < oList.size(); osIndex++) {
            Object[] os = oList.get(osIndex);
            if (osIndex == 0 || osIndex == 1) { // 第一行是 头部信息 直接跳过
                continue;
            }
            if (StringUtil.isNullOrNone(os[0])) {
                continue;
            }
            Assert.hasValue(os[1], (osIndex + 1) + "单元编号不能为空");
            Assert.hasValue(os[2], (osIndex + 1) + "房屋编号不能为空");
            Assert.hasValue(os[4], (osIndex + 1) + "单价不能为空");
            Assert.hasValue(os[5], (osIndex + 1) + "上期度数不能为空");
            Assert.hasValue(os[6], (osIndex + 1) + "上期度数时间不能为空");
            Assert.hasValue(os[7], (osIndex + 1) + "本期度数不能为空");
            Assert.hasValue(os[8], (osIndex + 1) + "本期度数时间不能为空");
 
//
 
            String startTime = excelDoubleToDate(os[6].toString());
            String endTime = excelDoubleToDate(os[8].toString());
            Assert.isDate(startTime, DateUtil.DATE_FORMATE_STRING_B, (osIndex + 1) + "行开始时间格式错误 请填写YYYY-MM-DD 文本格式");
            Assert.isDate(endTime, DateUtil.DATE_FORMATE_STRING_B, (osIndex + 1) + "行结束时间格式错误 请填写YYYY-MM-DD 文本格式");
 
 
            importRoomFee = new ImportExportMeterWaterDto();
            importRoomFee.setFloorNum(os[0].toString());
            importRoomFee.setUnitNum(os[1].toString());
            importRoomFee.setRoomNum(os[2].toString());
            importRoomFee.setPrice(Double.parseDouble(os[4].toString()));
            importRoomFee.setPreDegrees(os[5].toString());
            importRoomFee.setPreReadingTime(startTime);
            importRoomFee.setCurDegrees(os[7].toString());
            importRoomFee.setCurReadingTime(endTime);
            rooms.add(importRoomFee);
        }
    }
 
 
    //解析Excel日期格式
    public static String excelDoubleToDate(String strDate) {
        if (strDate.length() == 5) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date tDate = DoubleToDate(Double.parseDouble(strDate));
                return sdf.format(tDate);
            } catch (Exception e) {
                e.printStackTrace();
                return strDate;
            }
        }
        return strDate;
    }
 
    //解析Excel日期格式
    public static Date DoubleToDate(Double dVal) {
        Date tDate = new Date();
        long localOffset = tDate.getTimezoneOffset() * 60000; //系统时区偏移 1900/1/1 到 1970/1/1 的 25569 天
        tDate.setTime((long) ((dVal - 25569) * 24 * 3600 * 1000 + localOffset));
 
        return tDate;
    }
 
    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
}