package cn.com.nike.controller;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import cn.com.nike.dao.EntityDao;
import cn.com.nike.entity.ErrorInfo;
import cn.com.nike.service.MasterLineImportService;
import cn.com.nike.service.PdhDataService;
import cn.com.nike.util.SFTPChannel;
import cn.com.nike.util.Util;
import cn.com.nike.util.UtilFile;
import cn.com.nike.util.UtilProperties;
import cn.com.nike.util.Constants.ConstantsData;
import cn.com.nike.util.Constants.ConstantsResult;
import cn.com.nike.util.common.ResultModel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("/masterline")
public class MasterLineImportController implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4251160483432383385L;
private static final Logger log = LoggerFactory.getLogger(MasterLineImportController.class);
@Resource
private MasterLineImportService masterLineImportService;
@Resource
private SFTPChannel sftpchannel;
@Resource
private EntityDao entityDao;
@Resource
private PdhDataService pdhDataService;
@RequestMapping(value = "/masterlineimport", method = RequestMethod.POST)
@ResponseBody
public ResultModel masterLineImport(@RequestParam MultipartFile[] files, @RequestParam String receiveYear,
@RequestParam String receiveSeason, HttpServletRequest request) throws Exception {
ResultModel resultModel = new ResultModel();
ErrorInfo errorInfo = new ErrorInfo();
Map<String, Object> allErrorData = new HashMap<>();
String originalFilename = "";// 上传的文件的文件名
String path = "";
// 校验上传文件上否为空
if (null == files || files.length <= 0) {
resultModel.setResultMsg(ConstantsResult.FILE_NULL_ERROR);
return resultModel;
}
for (MultipartFile file : files) {
if (!file.isEmpty()) {
// 获得文件后缀名
String suffix = UtilFile.getSuffix(file);
// 检查文件格式是否正确 .xlsx(2007)
if (!UtilFile.checkSuffix(suffix)) {
resultModel.setResultCode(ConstantsResult.SERVICE_ERROR_CODE);
resultModel.setResultMsg(ConstantsResult.CHECK_EXCEL_NOT_EXIST_MSG);
return resultModel;
}
// 获得文件源名
originalFilename = UtilFile.getOriginalFilename(file);
InputStream readStream = file.getInputStream();
InputStream SFTPStream = file.getInputStream();
try {
// 1.读取文件信息
long start = System.currentTimeMillis();
Map<String, List<JSONObject>> list = masterLineImportService.readExcel(readStream, receiveYear,
receiveSeason, errorInfo, allErrorData);
long end = System.currentTimeMillis();
log.info("解析完毕,用时:" + (end - start) / 1000 + "秒");
log.debug("-------------" + list.get("updateToPdh"));
if (errorInfo.getDifferSeason() != null) {
allErrorData.put("difSeasonInColumn", errorInfo.getDifferSeason());
}
if (!allErrorData.isEmpty()) {
resultModel.setResultCode(ConstantsResult.DATA_ERROR_CODE);
resultModel.setResultData(allErrorData);
return resultModel;
} else {
QueryBuilder query = QueryBuilders.wildcardQuery("Style-color number", "*");
List pdhlist = new ArrayList<>();
if (entityDao.exindex(receiveYear + receiveSeason.toLowerCase() + "_pdh", "pdh")) {
pdhlist = entityDao.serarchNoPage(receiveYear + receiveSeason.toLowerCase() + "_pdh", "pdh",
query);
}
log.debug("pdh数据========" + pdhlist.size());
String season = receiveSeason;
String totype = "pdh";
String indexML = receiveYear + ConstantsData.MASTERLINE_DB_NAME;
String typeMLdrop = season + ConstantsData.MASTERLINE_DROP_TABLE_NAME;
String typeMLall = season + ConstantsData.MASTERLINE_ALL_TABLE_NAME;
String toindex = receiveYear + season.toLowerCase() + "_" + "pdh";
String fromname = "Launch Date";
String toname = "Product First Offer date";
String pdhseasonyear = season + receiveYear;
// drop表的逻辑判断
log.debug(season + "====" + toindex + "===" + totype);
Map<String, List<JSONObject>> compareResult = masterLineImportService
.pdhCompare(list.get("drop"), pdhlist, season, toindex, totype);
log.debug("-------" + compareResult.get("judgeChangelog"));
if (compareResult.get("judgeChangelog") == null
|| compareResult.get("judgeChangelog").isEmpty()) {
// 2.保存到数据库masterline all
if (!list.get("ALL").isEmpty()) {
Map<String, Object> mapN = masterLineImportService.updatedatebyname(list.get("ALL"),
pdhlist, fromname, toindex, totype, toname, season);
masterLineImportService.createJson(indexML, typeMLall, list.get("ALL"));
}
log.debug("-----" + list.get("updateToPdh"));
// all表的lifecycle状态更新
List<Map<String, Object>> resultpdhskuall = new ArrayList<>();
JSONArray arrayallLif = JSONArray.fromObject(list.get("updateToPdh"));
if (!arrayallLif.isEmpty()) {
for (int i = 0; i < arrayallLif.size(); i++) {
Map<String, Object> mapallRecieve = new HashMap<String, Object>();
JSONObject jsonallLif = arrayallLif.getJSONObject(i);
Iterator<?> it = jsonallLif.keys();
while (it.hasNext()) {
String key = String.valueOf(it.next());
Object value = jsonallLif.get(key);
mapallRecieve.put(key, value);
}
resultpdhskuall.add(mapallRecieve);
}
log.debug("----all 表要更新到pdh的值----" + resultpdhskuall);
pdhDataService.updateProductOfferingLifeCycleBySeasonAndSku(pdhseasonyear,
resultpdhskuall);
}
if (!list.get("drop").isEmpty()) {
// 创建drop表到数据库中
masterLineImportService.createJson(indexML, typeMLdrop, compareResult.get("droplist"));
// drop表的lifecycle状态更新
List<Map<String, Object>> resultpdhskudrop = new ArrayList<>();
JSONArray arrayLif = JSONArray.fromObject(compareResult.get("lifecycle"));
if (!arrayLif.isEmpty()) {
for (int i = 0; i < arrayLif.size(); i++) {
Map<String, Object> mapRecieve = new HashMap<String, Object>();
JSONObject jsonLif = arrayLif.getJSONObject(i);
Iterator<?> it = jsonLif.keys();
while (it.hasNext()) {
String key = String.valueOf(it.next());
Object value = jsonLif.get(key);
mapRecieve.put(key, value);
}
resultpdhskudrop.add(mapRecieve);
}
log.debug("----drop 表要更新到pdh的值----" + resultpdhskudrop);
pdhDataService.updateProductOfferingLifeCycleBySeasonAndSku(pdhseasonyear,
resultpdhskudrop);
}
}
} else {
// drop表中sku旧值在pdh中没有
allErrorData.put("oldSkuNoMatch", compareResult.get("judgeChangelog"));
resultModel.setResultCode(ConstantsResult.DATA_ERROR_CODE);
resultModel.setResultData(allErrorData);
return resultModel;
}
resultModel.setResultCode(ConstantsResult.SERVICE_SUCCESS_CODE);
resultModel.setResultMsg(ConstantsResult.SERVICE_SUCCESS_MSG);
return resultModel;
}
} catch (Exception e) {
e.printStackTrace();
log.error("==mastlinecontroller error==", e);
resultModel.setResultCode(ConstantsResult.SERVICE_ERROR_CODE);
resultModel.setResultMsg(e.getMessage());
return resultModel;
}
}
}
return resultModel;
}
@RequestMapping(value = "/masterlinetimejudge", method = RequestMethod.POST)
@ResponseBody
public ResultModel masterLineTimeJudge(@RequestParam String receiveYear, @RequestParam String receiveSeason,
HttpServletRequest request) throws Exception {
ResultModel resultModel = new ResultModel();
log.debug("ddddddd===" + receiveSeason + receiveYear.substring(2, 4));
List seasonDeadlinereceive = UtilProperties.getPropertyAsList("aoc-config.properties",
receiveSeason + receiveYear.substring(2, 4));
if (!seasonDeadlinereceive.isEmpty()) {
Object seasonDeadline = seasonDeadlinereceive.get(0);
Date deadline = Util.sdf.parse((String) seasonDeadline);
if (new Date().getTime() > deadline.getTime()) {
resultModel.setResultCode(ConstantsResult.SERVICE_SUCCESS_CODE);
return resultModel;
}
}
resultModel.setResultCode(ConstantsResult.SERVICE_ERROR_CODE);
return resultModel;
}
}