package com.huawei.iop.service.worldCUP.userCluster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import com.huawei.iop.bean.TaskInfo;
import com.huawei.iop.common.AbstractService;
import com.huawei.iop.common.IopConst;
import com.huawei.iop.common.IopException;
import com.huawei.iop.manager.worldCUP.userCluster.UserDefinedGroupManager;
import com.huawei.iop.manager.worldCUP.userCluster.WorldCUPManager;
import com.huawei.iop.util.ConfigPropertiesUtil;
import com.huawei.iop.util.IOHelperUtil;
public class ExpUserClusterFileService extends AbstractService
{
static char separator = ',';
private WorldCUPManager worldCUPManager;
@Autowired
private UserDefinedGroupManager userDefinedGroupManager;
public void setWorldCUPManager(WorldCUPManager worldCUPManager)
{
this.worldCUPManager = worldCUPManager;
}
public void setUserDefinedGroupManager(UserDefinedGroupManager userDefinedGroupManager)
{
this.userDefinedGroupManager = userDefinedGroupManager;
}
@Override
public String excute(Map<String, Object> context) throws IopException
{
logger.info("enter ExpUserClusterFile ...");
logger.info("params:" + context);
// 接收前台数据
String jsonObject = (String) context.get(IopConst.REQUEST_JSON);
String execute = IopConst.IOP_RUN_SUCCESS;
String savePath = request.getSession().getServletContext().getRealPath("tmp");
String urlPath = request.getServletContext().getContextPath() + "/tmp";
/*
* BufferedReader reader = null; BufferedOutputStream bo = null;
*/
// 封装Json
JSONObject object = null;
// String pathString = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
object = new JSONObject(jsonObject);
File saveFile = new File(savePath);
List<Boolean> flagList = new ArrayList<Boolean>(IopConst.DEFAULTNUM);
if (!saveFile.exists())
{
// 新建
// saveFile.mkdirs();
flagList.add(saveFile.mkdirs());
boolean b = saveFile.mkdirs();
if (b)
{
logger.info("saveFile.mkdirs():" + b);
}
}
String taskID = object.getString("taskID");
TaskInfo taskInfo = worldCUPManager.selectTaskById(taskID);
if (null == taskInfo)
{
return execute = IopConst.IOP_BEAN_FAILSE;
}
String fileName =taskInfo.getFileName();
String downloadFileName = "users_group_"+taskID+".csv";
//users_group_201806051010.csv
// 獲取當前時間的年月
/*
* Date nowDate = new Date(); SimpleDateFormat df = new
* SimpleDateFormat("yyyyMM"); String dateTime = df.format(nowDate);
*/
// String resultPathFromConf
// =ConfigPropertiesUtil.readValue("baseClusterUserPath");
// String resultPathFromConf =
// ConfigPropertiesUtil.readValue("baseClusterUserPath") +
// dateTime;// download/resultgroups
String resultPathFromConf = taskInfo.getDownloadPath();
File file = new File(resultPathFromConf, fileName);
logger.info("file:" + file);
File destFile = new File(savePath, downloadFileName);
logger.info("destFile:" + destFile);
/*
* if (StringUtils.isEmpty(resultPathFromConf)) { resultPathFromConf
* = "/home/iop/download/taskresults/" + dateTime; }
*/
try
{
fis = new FileInputStream(file);
fos = new FileOutputStream(destFile);
int nRead = 0;
byte[] buffer = new byte[1024];
// 用戶ID
String userId = ConfigPropertiesUtil.readValue("column1");
if (StringUtils.isEmpty(userId))
{
userId = "UERID";
}
// 手机号码
String telephone = ConfigPropertiesUtil.readValue("column2");
if (StringUtils.isEmpty(telephone))
{
telephone = "TELEPHONE";
}
// 流量值(M)
String flow = ConfigPropertiesUtil.readValue("column3");
if (StringUtils.isEmpty(flow))
{
flow = "FLOW(MB)";
}
fos.write((userId + "," + telephone + "," + flow + "\r\n").getBytes());
while ((nRead = fis.read(buffer, 0, 1024)) > 0)
{ // bis为网络输入流
fos.write(buffer, 0, nRead);
}
}
catch (Exception e)
{
logger.error("fis read failed ...");
logger.error("fos read failed ...");
}
finally
{
IOHelperUtil.close(fis, fos);
}
/*
* String separator = System.getProperty("line.separator"); reader =
* new BufferedReader(new FileReader(file));// 换成你的文件名 String line =
* null; String[] heads = { "用户ID", "手机号", "流量(M)" }; int i = 0;
* String[][] rows = new String[3][]; String[] split = null; while
* ((line = reader.readLine()) != null) { split =
* line.trim().split(","); rows[i++] = split; } byte[] bom = {
* (byte) 0xFF, (byte) 0xFE }; bo = new BufferedOutputStream(new
* FileOutputStream(destFile)); logger.info("write begin ...");
* bo.write(bom); bo.write(join(heads, "\t").getBytes("utf-16le"));
* bo.write(separator.getBytes("utf-16le")); for (String[] row :
* rows) { bo.write(join(row, "\t").getBytes("utf-16le"));
* bo.write(separator.getBytes("utf-16le")); // int value =
* Integer.parseInt(last);//如果是数值,可以转化为 } bo.flush();
*/
logger.info("write end ...");
/*
* if (file.isFile() || file.exists() || file.canRead()) {
*/
appendJSON("url", urlPath + "/" + downloadFileName);
// execute = IopConst.IOP_RUN_SUCCESS;
// }
/*
* else { logger.info("if-else : else"); }
*/
logger.info("out ExpActivityService ...");
}
/*
* execute = IopConst.IOP_RUN_FAILSE;
* logger.error("QueryHitContent execute:", e);
*/
catch (JSONException e)
{
execute = IopConst.IOP_RUN_FAILSE;
logger.error("JSONException: IopConst.IOP_RUN_FAILSE" + e);
}
/*
* catch (UnsupportedEncodingException e) { execute =
* IopConst.IOP_RUN_FAILSE;
* logger.error("UnsupportedEncodingException: IopConst.IOP_RUN_FAILSE"
* + e); } catch (IOException e) { execute = IopConst.IOP_RUN_FAILSE;
* logger.error("IOException: IopConst.IOP_RUN_FAILSE" + e); }
*/
catch (Exception e)
{
execute = IopConst.IOP_RUN_FAILSE;
logger.error("Exception: IopConst.IOP_RUN_FAILSE" + e);
}
finally
{
/*
* if (reader != null) { try { reader.close(); } catch (IOException
* e) { e.printStackTrace(); } } if (bo != null) { try { bo.close();
* } catch (IOException e) { e.printStackTrace(); } }
*/
logger.info("Executed once ExportDefinedGroupsService ");
// 错误日志
}
contextMap.put(IopConst.STATUES, execute);
return execute;
}
/*
* private String join(String[] strArr, String delim) { StringBuilder sb =
* new StringBuilder(); for (String s : strArr) { sb.append(s);
* sb.append(delim); } String ret; if (strArr.length > 1) { ret =
* sb.substring(0, sb.length() - 1); } else { ret = sb.toString(); } return
* ret; }
*/
}