fanli/src/main/java/com/yeshi/fanli/controller/admin/homemodule/SwiperBannerAdminController.java
@@ -1,18 +1,17 @@ package com.yeshi.fanli.controller.admin.homemodule; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.Resource; import net.sf.json.JSONObject; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.yeshi.utils.JsonUtil; import com.google.gson.Gson; @@ -31,6 +30,8 @@ import com.yeshi.fanli.tag.PageEntity; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; import net.sf.json.JSONObject; /** * 轮播图管理 @@ -281,8 +282,8 @@ * @param bannerId * @param out */ @RequestMapping(value = "queryPicture") public void queryPicture(String callback, Integer pageIndex, Integer pageSize, Long bannerId, PrintWriter out) { @RequestMapping(value = "queryPicInfo") public void queryPicInfo(String callback, Integer pageIndex, Integer pageSize, Long bannerId, PrintWriter out) { if (pageIndex == null || pageIndex < 1) { pageIndex = 1; @@ -309,14 +310,27 @@ // 跳转链接 for (SwiperPicture swiperPicture: list) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"); Date startTime = swiperPicture.getStartTime(); if (startTime == null) { swiperPicture.setStartTime_str(""); } else { swiperPicture.setStartTime_str(sdf.format(startTime)); } Date endTime = swiperPicture.getEndTime(); if (endTime == null) { swiperPicture.setEndTime_str(""); } else { swiperPicture.setEndTime_str(sdf.format(endTime)); } String params = swiperPicture.getParams(); if (!StringUtil.isNullOrEmpty(params)) { JSONObject jsonData = JSONObject.fromObject(params); if (jsonData.get("url") != null){ swiperPicture.setParams(jsonData.get("url").toString()); } else { swiperPicture.setParams(null); } if (params == null) { swiperPicture.setParams(""); } JumpDetailV2 jumpDetail = swiperPicture.getJumpDetail(); @@ -327,7 +341,6 @@ jumpDetailV2.setType("default"); swiperPicture.setJumpDetail(jumpDetailV2); } } @@ -338,7 +351,7 @@ GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.serializeNulls(); Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create(); Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm").create(); JSONObject data = new JSONObject(); data.put("pe", pe); @@ -353,134 +366,35 @@ } /** * 上传图片以及对应信息 * @param file 图片文件 * @param uid * @param record 图片信息 * @param request * @param response * @param out */ @RequestMapping(value = "uploadPicture") public void uploadPicture(@RequestParam("file") CommonsMultipartFile file, Long uid, SwiperPicture record, PrintWriter out) { if (file == null) { out.print(JsonUtil.loadFalseResult("上传文件为空")); return; } try { Long bannerId = record.getBannerId(); if (bannerId == null) { out.print(JsonUtil.loadFalseResult("轮播图管理ID不能为空")); return; } int maxOrder = swiperPictureService.getMaxOrderByBannerID(bannerId); record.setOrder(maxOrder + 1); // 默认停用 record.setState(1); record.setAutoControl(1); record.setCreatetime(new Date()); record.setUpdatetime(new Date()); String params = record.getParams(); if (!StringUtil.isNullOrEmpty(params)) { String jumpValue = systemConfigService.get("jump"); if (StringUtil.isNullOrEmpty(jumpValue)) { jumpValue = "{\"url\":\"#\"}"; } params = jumpValue.replace("#", params); record.setParams(params); } swiperPictureService.insertSelective(record); // 上传图片 swiperPictureService.uploadPicture(file, record); out.print(JsonUtil.loadTrueResult("上传成功")); } catch (Exception e) { out.print(JsonUtil.loadFalseResult("操作失败")); e.printStackTrace(); } } /** * 修改图片 * @param file 图片 * @param uid 用户id * @param record * @param request * @param response * @param out */ @RequestMapping(value = "changePicture") public void changePicture(@RequestParam("file") CommonsMultipartFile file, Long uid, Long id, PrintWriter out) { if (file == null) { out.print(JsonUtil.loadFalseResult("上传文件为空")); return; } try { if (id == null) { out.print(JsonUtil.loadFalseResult("ID不能为空")); return; } SwiperPicture resultObj = swiperPictureService.selectByPrimaryKey(id); if (resultObj == null) { out.print(JsonUtil.loadFalseResult("操作数据已不存在")); return; } swiperPictureService.uploadPicture(file, resultObj); out.print(JsonUtil.loadTrueResult("上传成功")); } catch (Exception e) { out.print(JsonUtil.loadFalseResult("操作失败")); e.printStackTrace(); } } /** * 修改图片信息 * 保存信息 * * @param callback * @param record * @param special * @param out */ @RequestMapping(value = "changePicInfo") public void changePicInfo(String callback, SwiperPicture record, String jumpType, PrintWriter out) { @RequestMapping(value = "savePicInfo") public void savePicInfo(String callback, SwiperPicture record, String jumpType, HttpServletRequest request,PrintWriter out) { try { Long id = record.getId(); if (id == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("ID不能为空")); return; } swiperPictureService.save(record); // 1. 先判断httpRequest 是否含有文件类型 if (request instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request; swiperPictureService.saveObject(fileRequest.getFile("file"), record, jumpType); }else{ swiperPictureService.saveObject(null, record, jumpType); } JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("保存成功")); } catch (SwiperPictureException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg())); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作失败")); JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("保存失败")); e.printStackTrace(); } } /** * 删除图片以及信息 @@ -488,8 +402,8 @@ * @param idArray * @param out */ @RequestMapping(value = "deletePicture") public void deletePicture(String callback, String idArray, PrintWriter out) { @RequestMapping(value = "deletePicInfo") public void deletePicInfo(String callback, String idArray, PrintWriter out) { try { if (StringUtil.isNullOrEmpty(idArray)) { @@ -509,95 +423,12 @@ JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("成功删除["+ count +"]条数据")); } catch (SwiperPictureException e) { } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("删除失败")); e.printStackTrace(); } } /** * 修该状态 * @param callback * @param id * @param out */ @RequestMapping(value = "updateStatePic") public void updateStatePic(String callback, Long id, PrintWriter out) { try { if (id == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("ID不能为空")); return; } SwiperPicture resultObj = swiperPictureService.selectByPrimaryKey(id); if (resultObj == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作数据已不存在")); return; } Integer state = resultObj.getState(); if (state == null || state.equals(1) || state == 1) { resultObj.setState(0); } else { resultObj.setState(1); } swiperPictureService.updateByPrimaryKeySelective(resultObj); JSONObject data = new JSONObject(); data.put("state", resultObj.getState()); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (SwiperPictureException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常")); e.printStackTrace(); } } /** * 更新自动控制状态 * @param callback * @param id * @param out */ @RequestMapping(value = "updateAutoControl") public void updateAutoControl(String callback, Long id, PrintWriter out) { try { if (id == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("ID不能为空")); return; } SwiperPicture resultObj = swiperPictureService.selectByPrimaryKey(id); if (resultObj == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作数据已不存在")); return; } Integer state = resultObj.getAutoControl(); if (state == null || state.equals(1) || state == 1) { resultObj.setAutoControl(0); } else { resultObj.setAutoControl(1); } swiperPictureService.updateByPrimaryKeySelective(resultObj); JSONObject data = new JSONObject(); data.put("state", resultObj.getState()); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (SwiperPictureException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常")); e.printStackTrace(); } } /** * 修改排序 fanli/src/main/java/com/yeshi/fanli/entity/bus/homemodule/SwiperBanner.java
@@ -3,14 +3,8 @@ import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Table; import javax.persistence.Transient; import org.yeshi.utils.mybatis.Column; import org.yeshi.utils.mybatis.Table; /** * 轮播图管理 @@ -19,42 +13,33 @@ * * @date 2018年10月15日 */ @Entity @Table(name = "yeshi_ec_swiper_banner") @org.yeshi.utils.mybatis.Table("yeshi_ec_swiper_banner") @Table("yeshi_ec_swiper_banner") public class SwiperBanner implements Serializable{ private static final long serialVersionUID = 1L; @Id @Column(name = "sp_id") @GeneratedValue(strategy = GenerationType.AUTO) @org.yeshi.utils.mybatis.Column(name = "sp_id") private Long id; @JoinColumn(name = "sp_title") @org.yeshi.utils.mybatis.Column(name = "sp_title") @Column(name = "sp_title") private String title;// 名称 @JoinColumn(name = "sp_card") @org.yeshi.utils.mybatis.Column(name = "sp_card") @Column(name = "sp_card") private String card; // 匹配标识 @JoinColumn(name = "sp_state") @org.yeshi.utils.mybatis.Column(name = "sp_state") @Column(name = "sp_state") private Integer state; // 状态: 1 停用 0启用 @JoinColumn(name = "sp_createtime") @org.yeshi.utils.mybatis.Column(name = "sp_createtime") @Column(name = "sp_createtime") private Date createtime; // 创建时间 @JoinColumn(name = "sp_updatetime") @org.yeshi.utils.mybatis.Column(name = "sp_updatetime") @Column(name = "sp_updatetime") private Date updatetime; // 更新时间(修改时间) @Transient private Long totalPic; // 包含图片数量 // 包含图片数量 private Long totalPic; public Long getId() { return id; fanli/src/main/java/com/yeshi/fanli/entity/bus/homemodule/SwiperPicture.java
@@ -3,16 +3,9 @@ import java.io.Serializable; import java.util.Date; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.springframework.format.annotation.DateTimeFormat; import org.yeshi.utils.mybatis.Column; import org.yeshi.utils.mybatis.Table; import com.google.gson.annotations.Expose; import com.yeshi.fanli.entity.common.JumpDetailV2; @@ -22,88 +15,75 @@ * @author Administrator * */ @Entity @Table(name = "yeshi_ec_swiper_banner_picture") @org.yeshi.utils.mybatis.Table("yeshi_ec_swiper_banner_picture") @Table("yeshi_ec_swiper_banner_picture") public class SwiperPicture implements Serializable { private static final long serialVersionUID = 1L; @Id @javax.persistence.Column(name = "pic_id") @GeneratedValue(strategy = GenerationType.AUTO) @org.yeshi.utils.mybatis.Column(name = "pic_id") @Column(name = "pic_id") private Long id; // 轮播图标识ID @JoinColumn(name = "pic_banner_id") @org.yeshi.utils.mybatis.Column(name = "pic_banner_id") @Column(name = "pic_banner_id") private Long bannerId; // 图片链接 @Expose @JoinColumn(name = "pic_src") @org.yeshi.utils.mybatis.Column(name = "pic_src") @Column(name = "pic_src") private String src; // 跳转方式 @Expose @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "pic_jumpid") @org.yeshi.utils.mybatis.Column(name = "pic_jumpid") @Column(name = "pic_jumpid") private JumpDetailV2 jumpDetail; // 跳转参数 @Expose @JoinColumn(name = "pic_params") @org.yeshi.utils.mybatis.Column(name = "pic_params") @Column(name = "pic_params") private String params; // 跳转是否需要登陆 @JoinColumn(name = "pic_jump_need_login") @org.yeshi.utils.mybatis.Column(name = "pic_jump_need_login") @Column(name = "pic_jump_need_login") private boolean jumpNeedLogin; // 排序 @JoinColumn(name = "pic_order") @org.yeshi.utils.mybatis.Column(name = "pic_order") @Column(name = "pic_order") private Integer order; @JoinColumn(name = "pic_remark") @org.yeshi.utils.mybatis.Column(name = "pic_remark") @Column(name = "pic_remark") private String remark; // 启用状态 @JoinColumn(name = "pic_state") @org.yeshi.utils.mybatis.Column(name = "pic_state") // 启用状态 0 启用1 停用 @Column(name = "pic_state") private Integer state; // 自动控制 @JoinColumn(name = "pic_auto_control") @org.yeshi.utils.mybatis.Column(name = "pic_auto_control") @Column(name = "pic_auto_control") private Integer autoControl; // 起始时间 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JoinColumn(name = "pic_start_time") @org.yeshi.utils.mybatis.Column(name = "pic_start_time") @Column(name = "pic_start_time") private Date startTime; // 结束时间 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JoinColumn(name = "pic_end_time") @org.yeshi.utils.mybatis.Column(name = "pic_end_time") @Column(name = "pic_end_time") private Date endTime; @JoinColumn(name = "pic_createtime") @org.yeshi.utils.mybatis.Column(name = "pic_createtime") @Column(name = "pic_createtime") private Date createtime; @JoinColumn(name = "pic_updatetime") @org.yeshi.utils.mybatis.Column(name = "pic_updatetime") @Column(name = "pic_updatetime") private Date updatetime; // 起始时间 private String startTime_str; // 结束时间 private String endTime_str; public Long getId() { return this.id; } @@ -215,5 +195,20 @@ public void setJumpNeedLogin(boolean jumpNeedLogin) { this.jumpNeedLogin = jumpNeedLogin; } public String getStartTime_str() { return startTime_str; } public void setStartTime_str(String startTime_str) { this.startTime_str = startTime_str; } public String getEndTime_str() { return endTime_str; } public void setEndTime_str(String endTime_str) { this.endTime_str = endTime_str; } } fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/SwiperPictureServiceImpl.java
@@ -1,6 +1,8 @@ package com.yeshi.fanli.service.impl.homemodule; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.UUID; @@ -15,9 +17,9 @@ import com.yeshi.fanli.dao.mybatis.homemodule.SwiperPictureMapper; import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture; import com.yeshi.fanli.exception.NotExistObjectException; import com.yeshi.fanli.entity.common.JumpDetailV2; import com.yeshi.fanli.exception.banner.SwiperPictureException; import com.yeshi.fanli.service.inter.config.SystemConfigService; import com.yeshi.fanli.service.inter.common.JumpDetailV2Service; import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService; import com.yeshi.fanli.util.StringUtil; @@ -28,17 +30,9 @@ private SwiperPictureMapper swiperPictureMapper; @Resource private SystemConfigService systemConfigService; private JumpDetailV2Service jumpDetailV2Service; @Override public int deleteByPrimaryKey(Long id) throws SwiperPictureException{ return swiperPictureMapper.deleteByPrimaryKey(id); } @Override public int insert(SwiperPicture record) throws SwiperPictureException{ return swiperPictureMapper.insert(record); } @Override public int insertSelective(SwiperPicture record) throws SwiperPictureException{ @@ -71,9 +65,8 @@ } @Override public void save(SwiperPicture record) throws SwiperPictureException{ public void saveObject(MultipartFile file, SwiperPicture record, String jumpType) throws SwiperPictureException, Exception{ if (record == null) { throw new SwiperPictureException(1, "参数不能为空"); @@ -85,34 +78,61 @@ } String params = record.getParams(); if (!StringUtil.isNullOrEmpty(params)) { try { String jumpValue = systemConfigService.get("jump"); if (StringUtil.isNullOrEmpty(jumpValue)) { jumpValue = "{\"url\":\"#\"}"; } params = jumpValue.replace("#", params); } catch (NotExistObjectException e) { e.printStackTrace(); if ((params != null && params.trim().length() == 0) || "null".equalsIgnoreCase(params) ) { record.setParams(null); } if (!StringUtil.isNullOrEmpty(jumpType)) { List<JumpDetailV2> listByType = jumpDetailV2Service.listByType(jumpType); if (listByType !=null && listByType.size() > 0) { record.setJumpDetail(listByType.get(0)); } } try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String startTime_str = record.getStartTime_str(); if (startTime_str != null && startTime_str.trim().length() > 0) { startTime_str = startTime_str.replaceAll("T", " "); record.setStartTime(format.parse(startTime_str)); } String endTime_str = record.getEndTime_str(); if (endTime_str != null && endTime_str.trim().length() > 0) { endTime_str = endTime_str.replaceAll("T", " "); record.setEndTime(format.parse(endTime_str)); } } catch (ParseException e) { e.printStackTrace(); } String picture = null; if (file != null) { picture = uploadPicture(file); } Long id = record.getId(); if (id == null) { // 新增 int maxOrder = swiperPictureMapper.getMaxOrderByBannerID(bannerId); record.setOrder(maxOrder + 1); Integer state = record.getState(); // 默认停用 record.setState(1); // 默认非系统控制 record.setAutoControl(1); // 默认非登陆 record.setJumpNeedLogin(false); if (state == null) { record.setState(1); } Integer autoControl = record.getAutoControl(); if (autoControl == null) { // 默认非系统控制 record.setAutoControl(1); } record.setCreatetime(new Date()); record.setUpdatetime(new Date()); record.setSrc(picture); swiperPictureMapper.insert(record); } else { @@ -122,13 +142,57 @@ throw new SwiperPictureException(1, "参数不能为空"); } if (picture != null && picture.trim().length() > 0) { // 删除已存在图片 removePicture(resultObj); record.setSrc(picture); } else { record.setSrc(resultObj.getSrc()); } record.setOrder(resultObj.getOrder()); record.setCreatetime(resultObj.getCreatetime()); record.setUpdatetime(new Date()); swiperPictureMapper.updateByPrimaryKey(record); } } /** * 上传图片 * @param file * @return * @throws Exception */ public String uploadPicture(MultipartFile file) throws Exception { // 文件解析 InputStream inputStream = file.getInputStream(); String contentType = file.getContentType(); String type = contentType.substring(contentType.indexOf("/") + 1); // 文件路径 String filePath="/img/swiperPic/"+UUID.randomUUID().toString().replace("-", "") + "." + type; // 执行上传 String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl(); return fileLink; } /** * 删除图片-不更新数据库 * @param record * @throws Exception */ public void removePicture(SwiperPicture record) throws Exception { String picture = record.getSrc(); if (picture != null && picture.trim().length() > 0) { COSManager.getInstance().deleteFile(picture); } } @Override @Transactional @@ -155,30 +219,6 @@ return swiperPictureMapper.queryByListBannerID(list); } @Override public void uploadPicture(MultipartFile file, SwiperPicture record) throws Exception { InputStream inputStream = file.getInputStream(); String contentType = file.getContentType(); String type = contentType.substring(contentType.indexOf("/") + 1); // 上传文件相对位置 String fileUrl="swiperPic/"+UUID.randomUUID().toString().replace("-", "") + "." + type; /* 修改图片时,先删除已存在图片 */ String src = record.getSrc(); if (!StringUtil.isNullOrEmpty(src)) { COSManager.getInstance().deleteFile(src); } String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl(); /* 更新数据库信息 */ if (!StringUtil.isNullOrEmpty(uploadFilePath)) { record.setSrc(uploadFilePath); record.setUpdatetime(new Date()); updateByPrimaryKeySelective(record); } } @Override public List<SwiperPicture> getOrderByBannerID(Long bannerId, Integer type, Integer order) throws SwiperPictureException { fanli/src/main/java/com/yeshi/fanli/service/inter/homemodule/SwiperPictureService.java
@@ -11,9 +11,6 @@ public interface SwiperPictureService { public int deleteByPrimaryKey(Long id) throws SwiperPictureException; public int insert(SwiperPicture record) throws SwiperPictureException; public int insertSelective(SwiperPicture record) throws SwiperPictureException; @@ -51,13 +48,6 @@ */ public int deleteBatchByBannerID(List<Long> list) throws SwiperPictureException; /** * 上传图片 * @param file * @param record * @throws Exception */ public void uploadPicture(MultipartFile file, SwiperPicture record) throws Exception; /** * 根据管理id集合 查询 @@ -96,5 +86,5 @@ * @param record * @throws SwiperPictureException */ public void save(SwiperPicture record) throws SwiperPictureException; public void saveObject(MultipartFile file, SwiperPicture record, String jumpType) throws SwiperPictureException, Exception; }