package com.yeshi.fanli.controller.admin;
|
|
import java.io.PrintWriter;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import net.sf.json.JSONObject;
|
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.fanli.entity.bus.lable.Label;
|
import com.yeshi.fanli.entity.common.AdminUser;
|
import com.yeshi.fanli.exception.LabelException;
|
import com.yeshi.fanli.service.AdminUserService;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.lable.LabelGoodsService;
|
import com.yeshi.fanli.service.inter.lable.LabelService;
|
import com.yeshi.fanli.tag.PageEntity;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.TimeUtil;
|
import com.yeshi.fanli.util.annotation.RequestNoLogin;
|
import org.yeshi.utils.JsonUtil;
|
|
@Controller
|
@RequestMapping("admin/new/api/v1/lable")
|
public class LableAdminController {
|
|
@Resource
|
private LabelService labelService;
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private AdminUserService adminUserService;
|
|
@Resource
|
private LabelGoodsService labelGoodsService;
|
|
/**
|
* 添加标签并上传图片
|
* @param callback
|
* @param label
|
* @param request
|
* @param out
|
*/
|
@RequestNoLogin()
|
@RequestMapping(value = "saveAdd")
|
public void saveAdd(String callback,Long uid, Label label,
|
HttpServletRequest request, PrintWriter out, HttpServletResponse response) {
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
response.setHeader("Access-Control-Allow-Methods", "*");
|
|
// // 获取当前操作用户
|
AdminUser admin = adminUserService.selectByPrimaryKey(uid);
|
if (admin == null) {
|
out.print(JsonUtil.loadFalseResult("当前账户已失效,请重新登陆。"));
|
} else {
|
|
try {
|
String title = label.getTitle();
|
if (StringUtil.isNullOrEmpty(title)) {
|
out.print( JsonUtil.loadFalseResult("标签名称为空"));
|
} else {
|
|
List<Label> labels = labelService.selectByTitle(title.trim());
|
|
if (labels == null || labels.size() == 0) {
|
|
label.setTitle(label.getTitle().trim());
|
|
if(request instanceof MultipartHttpServletRequest){
|
|
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
|
|
if (files != null && files.size() > 0) {
|
labelService.insertSingle(label, admin, files.get(0));
|
} else {
|
labelService.insertSingle(label, admin, null);
|
}
|
|
} else {
|
labelService.insertSingle(label, admin, null);
|
}
|
|
out.print( JsonUtil.loadTrueResult("添加成功"));
|
|
} else {
|
out.print( JsonUtil.loadTrueResult("已存在此标签"));
|
}
|
}
|
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
out.print(JsonUtil.loadFalseResult("操作异常"));
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
|
|
/**
|
* 批量添加标签
|
* @param callback
|
* @param label
|
* @param request
|
* @param out
|
*/
|
@RequestMapping(value = "addBatch")
|
public void addBatch(String callback, String param,
|
HttpServletRequest request, PrintWriter out) {
|
// // 获取当前操作用户
|
AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
|
|
if (admin == null) {
|
out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadFalseResult("当前账户已失效,请重新登陆。")));
|
} else {
|
|
try {
|
Gson gson = new Gson();
|
List<Label> labs = gson.fromJson(param, new TypeToken<ArrayList<Label>>() {}.getType());
|
|
List<Label> newList = new ArrayList<Label>();
|
if (labs == null || labs.size() == 0) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未检索到数据")));
|
} else {
|
for (Label label: labs) {
|
String title = label.getTitle();
|
if (!StringUtil.isNullOrEmpty(title)) {
|
|
List<Label> labels = labelService.selectByTitle(title.trim());
|
if (labels == null || labels.size() == 0) {
|
newList.add(label);
|
}
|
}
|
}
|
}
|
|
|
labelService.insertList(newList, admin);
|
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("添加成功")));
|
} catch (LabelException e) {
|
// TODO Auto-generated catch block
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("添加异常")));
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
|
|
|
/**
|
* 保存修改标签
|
* @param callback
|
* @param label
|
* @param request
|
* @param out
|
*/
|
@RequestMapping(value = "saveModify")
|
public void saveModify(String callback, Label label, HttpServletRequest request,PrintWriter out) {
|
|
try {
|
Long id = label.getId();
|
Label resultObj = labelService.selectByPrimaryKey(id);
|
|
if (resultObj == null) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该标签不存在或已被删除")));
|
} else {
|
Date nowTime = new Date();
|
label.setUpdatetime(nowTime);
|
// 选择性更新数据
|
String title = label.getTitle();
|
if (StringUtil.isNullOrEmpty(title)) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("标签名称不能为空")));
|
} else {
|
List<Label> labels = labelService.selectByTitle(label.getTitle().trim());
|
|
if (labels != null && labels.size() > 0) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("标签名称重复,修改失败")));
|
} else {
|
|
int result = labelService.updateByPrimaryKeySelective(label);
|
|
if (result > 0) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
|
} else {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
|
}
|
}
|
|
}
|
|
}
|
|
} catch (LabelException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
|
}
|
|
}
|
|
|
/**
|
* 批量删除2--- 跨域
|
* @param callback
|
* @param ids
|
* @param out
|
*/
|
@RequestMapping(value = "deleteLable")
|
public void deleteLable(String callback, String ids, PrintWriter out) {
|
try {
|
|
Gson gson = new Gson();
|
List<Long> labs = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {}.getType());
|
|
if (labs == null || labs.size() == 0) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未检测到删除的数据")));
|
} else {
|
|
labelService.deleteBatchByPrimaryKey(labs);
|
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("删除成功")));
|
}
|
|
} catch (LabelException e) {
|
// TODO Auto-generated catch block
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("删除失败")));
|
e.printStackTrace();
|
}
|
|
}
|
|
|
/**
|
* 上传excel文件
|
* @param callback
|
* @param file
|
* @param request
|
* @param out
|
* @param response
|
*/
|
@RequestNoLogin()
|
@RequestMapping(value = "uploadFile")
|
public void uploadFile(@RequestParam("file") CommonsMultipartFile file, Long uid,
|
HttpServletRequest request, PrintWriter out,HttpServletResponse response) {
|
// 获取当前操作用户
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
response.setHeader("Access-Control-Allow-Methods", "*");
|
|
AdminUser admin = adminUserService.selectByPrimaryKey(uid);
|
|
if (admin == null) {
|
out.print( JsonUtil.loadFalseResult("当前账户失效,请重新登陆。"));
|
} else {
|
if (file == null) {
|
out.print(JsonUtil.loadFalseResult("文件不能为空!"));
|
} else {
|
|
try {
|
labelService.analysisExcel(file.getInputStream(), admin);
|
out.print((JsonUtil.loadTrueResult("上传成功")));
|
//response.getWriter().print(JsonUtil.loadTrueResult("上传成功"));
|
} catch (Exception e) {
|
e.printStackTrace();
|
out.print((JsonUtil.loadFalseResult("上传失败")));
|
}
|
|
}
|
|
}
|
|
}
|
|
|
/**
|
* 上传标签图片
|
* @param callback
|
* @param file
|
* @param request
|
* @param out
|
* @param response
|
*/
|
@RequestNoLogin()
|
@RequestMapping(value = "uploadImg")
|
public void uploadImg(Long id, @RequestParam("file") CommonsMultipartFile file,
|
HttpServletRequest request, PrintWriter out, HttpServletResponse response) {
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
response.setHeader("Access-Control-Allow-Methods", "*");
|
|
// 获取当前操作用户
|
|
try {
|
Label label = labelService.selectByPrimaryKey(id);
|
|
if (label == null) {
|
out.print(JsonUtil.loadFalseResult("该标签不存在或已被删除"));
|
} else {
|
|
if (file == null) {
|
out.print(JsonUtil.loadFalseResult("文件不能为空!"));
|
} else {
|
int result = labelService.uploadPicture(file, label);
|
|
if (result == 0) {
|
out.print(JsonUtil.loadFalseResult("图片上传成功,数据更新失败"));
|
} else if (result == 1) {
|
out.print(JsonUtil.loadTrueResult("图片上传成功"));
|
} else {
|
out.print(JsonUtil.loadFalseResult("图片上传失败"));
|
}
|
}
|
}
|
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
out.print(JsonUtil.loadFalseResult("操作失败"));
|
}
|
|
}
|
|
/**
|
* 删除图片
|
* @param callback
|
* @param file
|
* @param request
|
* @param out
|
* @param response
|
*/
|
@RequestMapping(value = "deleteImg")
|
public void deleteImg(String callback, Long id, HttpServletRequest request, PrintWriter out,
|
HttpServletResponse response) {
|
|
try {
|
Label label = labelService.selectByPrimaryKey(id);
|
|
if (label == null) {
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该标签不存在或已被删除")));
|
} else {
|
|
|
int result = labelService.deleteImg(label);
|
if (result == 0) {
|
out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadFalseResult("图片删除成功,数据更新失败")));
|
} else if (result == 1) {
|
out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadTrueResult("图片删除成功")));
|
} else {
|
out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadFalseResult("图片删除失败")));
|
}
|
|
}
|
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
|
}
|
|
}
|
|
|
/**
|
* 查找所有标签
|
* @param callback
|
* @param pageIndex
|
* @param key
|
* @param startTime
|
* @param endTime
|
* @param out
|
*/
|
@RequestMapping(value = "query")
|
public void query(String callback, Integer pageIndex, String key, String startTime,
|
String endTime, String orderMode, PrintWriter out) {
|
|
try {
|
|
// 起始时间 为空设置默认值:
|
if (StringUtil.isNullOrEmpty(startTime)) {
|
startTime = "1970-01-01";
|
}
|
|
// 结束时间 为空设置默认值当前日期
|
if (StringUtil.isNullOrEmpty(endTime)) {
|
Date curDate = new Date();
|
endTime = TimeUtil.getSimpleDate(curDate);
|
}
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
Date enddate = sdf.parse(endTime);
|
Calendar c = Calendar.getInstance();
|
c.setTime(enddate);
|
c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
|
|
endTime = sdf.format(c.getTime());
|
|
int pageSize = Constant.PAGE_SIZE;
|
|
List<Label> labelList = labelService.query((pageIndex - 1) * pageSize, pageSize, key,
|
startTime, endTime,orderMode);
|
|
if (labelList == null || labelList.size() == 0) {
|
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未查找到相关数据")));
|
|
} else {
|
|
/* 统计每个标签关联商品数量*/
|
for (Label label :labelList) {
|
Long relationNum = labelGoodsService.getRelationNum(label.getId());
|
if (relationNum == null)
|
relationNum = 0l;
|
|
label.setCountRelation(relationNum); // 关联标签商品数量
|
|
Long iosClick = label.getIosClick();
|
if (iosClick == null)
|
iosClick = 0l;
|
|
Long androidClick = label.getAndroidClick();
|
if (androidClick == null)
|
androidClick = 0l;
|
|
|
label.setCountClick(iosClick + androidClick);// 总点击数量
|
|
label.setIosClick(0l);
|
label.setAndroidClick(0l);
|
}
|
|
|
int count = labelService.getQueryCount(key, startTime, endTime);
|
|
int totalPage = count % pageSize == 0 ? count / pageSize : count
|
/ pageSize + 1;
|
PageEntity pe = new PageEntity(pageIndex, pageSize, count,
|
totalPage);
|
|
GsonBuilder gsonBuilder = new GsonBuilder();
|
gsonBuilder.serializeNulls(); //重点
|
Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
|
|
JSONObject data = new JSONObject();
|
data.put("pe", pe);
|
data.put("labelList", gson.toJson(labelList));
|
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("系统异常")));
|
}
|
}
|
|
/**
|
* 查找所有标签
|
* @param callback
|
* @param out
|
*/
|
@RequestMapping(value = "getCountToday")
|
public void getCountToday(String callback, PrintWriter out) {
|
|
try {
|
|
long totalToday = labelService.getCountToday();
|
|
Map<String, Object> count = labelService.getCountByEntryMode();
|
|
JSONObject data = new JSONObject();
|
data.put("count", count);
|
data.put("totalToday", totalToday);
|
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("系统异常")));
|
}
|
}
|
|
|
}
|