chengf
2026-03-25 1e0e62187e2b7ff3e1fef23b182a710dc8cd76f8
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
package com.java110.api.smo.assetImport.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.java110.api.importData.DefaultImportDataAdapt;
import com.java110.api.importData.IImportDataCleaningAdapt;
import com.java110.api.smo.DefaultAbstractComponentSMO;
import com.java110.api.smo.assetImport.IAssetImportSMO;
import com.java110.core.context.IPageData;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.PropertyWhiteListFlowDto;
import com.java110.dto.log.AssetImportLogDetailDto;
import com.java110.dto.payFee.PayFeeBatchDto;
import com.java110.dto.system.ComponentValidateResult;
import com.java110.dto.user.UserDto;
import com.java110.intf.IImportPropertyWhiteListfFlowServiceSMO;
import com.java110.intf.common.IAssetImportLogDetailInnerServiceSMO;
import com.java110.intf.common.IAssetImportLogInnerServiceSMO;
import com.java110.intf.fee.IPayFeeBatchV1InnerServiceSMO;
import com.java110.intf.job.IUserImportDataV1InnerServiceSMO;
import com.java110.intf.user.IMenuUserV1InnerServiceSMO;
import com.java110.intf.user.IUserInnerServiceSMO;
import com.java110.po.log.AssetImportLogDetailPo;
import com.java110.po.log.AssetImportLogPo;
import com.java110.po.payFee.PayFeeBatchPo;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.*;
import com.java110.vo.ResultVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import com.java110.core.log.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
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("assetImportSMOImpl")
public class AssetImportSMOImpl extends DefaultAbstractComponentSMO implements IAssetImportSMO {
    private final static Logger logger = LoggerFactory.getLogger(AssetImportSMOImpl.class);
 
    /**
     * 导入最大行数
     */
    @Value("${import.line.max}")
    public int MAX_LINE;
 
    /**
     * 导入最大行数
     */
    public static final int DEFAULT_ROWS = 200;
 
 
    @Autowired
    private RestTemplate restTemplate;
 
 
    private IImportDataCleaningAdapt importDataCleaningAdapt;
 
    @Autowired
    private IAssetImportLogInnerServiceSMO assetImportLogInnerServiceSMOImpl;
 
    @Autowired
    private IAssetImportLogDetailInnerServiceSMO assetImportLogDetailInnerServiceSMOImpl;
 
    @Autowired
    private IUserImportDataV1InnerServiceSMO userImportDataV1InnerServiceSMOImpl;
 
    @Autowired
    private IUserInnerServiceSMO iUserInnerServiceSMOImpl;
 
    @Autowired
    private IImportPropertyWhiteListfFlowServiceSMO importPropertyWhiteListfFlowServiceSMO;
 
 
    @Autowired
    private IPayFeeBatchV1InnerServiceSMO payFeeBatchV1InnerServiceSMOImpl;
 
    @Override
    public ResponseEntity<String> importExcelData(IPageData pd, MultipartFile uploadFile) throws Exception {
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        if (paramIn.getString("userId").isEmpty() || paramIn.getString("userId").equals("null")){
            throw new IllegalArgumentException("userId为空");
        }
        paramIn.put("userId", pd.getUserId());
        Assert.hasKeyAndValue(paramIn, "importAdapt", "未包含模板");
        Assert.hasKeyAndValue(paramIn, "userId", "未包含userId");
        String importAdapt = paramIn.getString("importAdapt") + "DataCleaning";
 
        importDataCleaningAdapt = ApplicationContextFactory.getBean(importAdapt, IImportDataCleaningAdapt.class);
 
        if (importDataCleaningAdapt == null) {
            throw new IllegalArgumentException("适配器没有实现" + importAdapt);
        }
        try {
            ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
            paramIn.put("storeId", result.getStoreId());
 
            Workbook workbook = null;  //工作簿
            //工作表
            String[] headers = null;   //表头信息
            if(uploadFile == null || uploadFile.getSize() == 0){
 
                PropertyWhiteListFlowDto propertyWhiteListFlowDto = new PropertyWhiteListFlowDto();
                propertyWhiteListFlowDto.setCommunityId((String) JSONObject.parseObject(pd.getReqData()).get("communityId"));
                // todo 适配器封装数据
                List<PropertyWhiteListFlowDto> datas = importPropertyWhiteListfFlowServiceSMO.queryPropertyWhiteListFlow(propertyWhiteListFlowDto);
                PayFeeBatchPo payFeeBatchPo = new PayFeeBatchPo();
                payFeeBatchPo.setBatchId(GenerateCodeFactory.getGeneratorId("12"));
                payFeeBatchPo.setCommunityId(paramIn.getString("communityId"));
                payFeeBatchPo.setCreateUserId(paramIn.getString("userId"));
                UserDto userDto = new UserDto();
                userDto.setUserId(paramIn.getString("userId"));
                List<UserDto> userDtos = iUserInnerServiceSMOImpl.getUsers(userDto);
 
                Assert.listOnlyOne(userDtos, "用户不存在");
                payFeeBatchPo.setCreateUserName(userDtos.get(0).getUserName());
                payFeeBatchPo.setState(PayFeeBatchDto.STATE_NORMAL);
                payFeeBatchPo.setMsg("正常");
                int flag = payFeeBatchV1InnerServiceSMOImpl.savePayFeeBatch(payFeeBatchPo);
 
                if (flag < 1) {
                    throw new IllegalArgumentException("生成批次失败");
                }
 
                paramIn.put("batchId", payFeeBatchPo.getBatchId());
 
                for (PropertyWhiteListFlowDto importRoomFee : datas) {
                    importRoomFee.setUserId(paramIn.getString("userId"));
                    importRoomFee.setStoreId(paramIn.getString("storeId"));
                    importRoomFee.setCommunityId(paramIn.getString("communityId"));
                    importRoomFee.setBatchId(paramIn.getString("batchId"));
                    importRoomFee.setObjType(paramIn.getString("objType"));
                }
 
                if (datas == null || datas.size() > MAX_LINE) {
                    throw new IllegalArgumentException("数据为空,或者数据行数大于" + MAX_LINE);
                }
 
                // 保存数据
                return saveLogAndImportData(pd, datas, result, paramIn.getString("importAdapt"));
            }
            else{
                workbook = ImportExcelUtils.createWorkbook(uploadFile);
                result.setImportFileName(uploadFile.getOriginalFilename());
                // todo 适配器封装数据
                List datas = importDataCleaningAdapt.analysisExcel(workbook, paramIn, result);
 
 
                if (datas == null || datas.size() > MAX_LINE) {
                    throw new IllegalArgumentException("数据为空,或者数据行数大于" + MAX_LINE);
                }
 
                // 保存数据
                return saveLogAndImportData(pd, datas, result, paramIn.getString("importAdapt"));
            }
        } catch (Exception e) {
            logger.error("导入失败 ", e);
            return new ResponseEntity<String>("非常抱歉,您填写的模板数据有误:" + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }
 
    /**
     * 处理ExcelData数据
     *
     * @param datas 数据
     */
    private ResponseEntity<String> saveLogAndImportData(IPageData pd,
                                                        List datas,
                                                        ComponentValidateResult result, String logType) {
        ResponseEntity<String> responseEntity = null;
 
        String logId = GenerateCodeFactory.getGeneratorId("10");
 
        AssetImportLogPo assetImportLogPo = new AssetImportLogPo();
        assetImportLogPo.setCommunityId(result.getCommunityId());
        assetImportLogPo.setLogId(logId);
        assetImportLogPo.setLogType(logType);
        assetImportLogPo.setErrorCount("0");
        assetImportLogPo.setSuccessCount("0");
        String userId = null;
        userId = pd.getUserId();
        if(userId != null) {
            UserDto userDto = new UserDto();
            userDto.setUserId(userId);
            List<UserDto> users = iUserInnerServiceSMOImpl.getUsers(userDto);
            if(users != null && users.size() > 0) {
                assetImportLogPo.setUserName(users.get(0).getUserName());
            }
        }
        assetImportLogPo.setImportFileName(result.getImportFileName());
        assetImportLogInnerServiceSMOImpl.saveAssetImportLog(assetImportLogPo);
 
        List<AssetImportLogDetailPo> assetImportLogDetailPos = new ArrayList<>();
        AssetImportLogDetailPo assetImportLogDetailPo = null;
        int flag = 0;
        Calendar createTimeCal = Calendar.getInstance();
        for (Object data : datas) {
            createTimeCal.add(Calendar.SECOND,1);
            assetImportLogDetailPo = new AssetImportLogDetailPo();
            assetImportLogDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId("11"));
            assetImportLogDetailPo.setLogId(logId);
            assetImportLogDetailPo.setState(AssetImportLogDetailDto.STATE_WAIT_IMPORT);
            assetImportLogDetailPo.setMessage("待导入");
            assetImportLogDetailPo.setCommunityId(result.getCommunityId());
            assetImportLogDetailPo.setContent(JSONObject.toJSONString(data));
            assetImportLogDetailPo.setCreateTime(DateUtil.getFormatTimeStringA(createTimeCal.getTime()));
            assetImportLogDetailPos.add(assetImportLogDetailPo);
            if (assetImportLogDetailPos.size() > DEFAULT_ROWS) {
                flag = assetImportLogDetailInnerServiceSMOImpl.saveAssetImportLogDetails(assetImportLogDetailPos);
                if (flag < 1) {
                    throw new IllegalArgumentException("保存失败");
                }
                assetImportLogDetailPos = new ArrayList<>();
            }
        }
 
        if (assetImportLogDetailPos.size() > 0) {
            flag = assetImportLogDetailInnerServiceSMOImpl.saveAssetImportLogDetails(assetImportLogDetailPos);
            if (flag < 1) {
                throw new IllegalArgumentException("保存失败");
            }
        }
 
        // todo 调用 导入队列开始导入
        flag = userImportDataV1InnerServiceSMOImpl.importExcelData(assetImportLogPo);
        if (flag > 0) {
            return ResultVo.createResponseEntity(assetImportLogPo);
        }
        return ResultVo.error("导入失败");
    }
 
 
}