yujian
2019-04-08 5c4ecf9e8b47efbbf5d21c9e109be0a60cb7e26d
fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/HomeNavbarServiceImpl.java
@@ -1,6 +1,7 @@
package com.yeshi.fanli.service.impl.homemodule;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -18,8 +19,10 @@
import com.yeshi.fanli.dao.mybatis.homemodule.SuperHomeNavbarMapper;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar;
import com.yeshi.fanli.entity.bus.homemodule.SuperHomeNavbar;
import com.yeshi.fanli.entity.system.System;
import com.yeshi.fanli.service.inter.config.SystemService;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.exception.ActivityException;
import com.yeshi.fanli.exception.homemodule.HomeNavbarException;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarService;
import com.yeshi.fanli.util.StringUtil;
@@ -28,43 +31,13 @@
public class HomeNavbarServiceImpl implements HomeNavbarService {
   
   @Resource
   private SystemService systemService;
   private BusinessSystemService businessSystemService;
   @Resource
   private HomeNavbarMapper homeNavbarMapper;
   @Resource
   private SuperHomeNavbarMapper superHomeNavbarMapper;
   
   @Override
   public int deleteByPrimaryKey(Long id) {
      return homeNavbarMapper.deleteByPrimaryKey(id);
   }
   @Override
   public int insert(HomeNavbar record) {
      return homeNavbarMapper.insert(record);
   }
   @Override
   public int insertSelective(HomeNavbar record) {
      return homeNavbarMapper.insertSelective(record);
   }
   @Override
   public HomeNavbar selectByPrimaryKey(Long id) {
      return homeNavbarMapper.selectByPrimaryKey(id);
   }
   @Override
   public int updateByPrimaryKeySelective(HomeNavbar record) {
      return homeNavbarMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   public int updateByPrimaryKey(HomeNavbar record) {
      return homeNavbarMapper.updateByPrimaryKey(record);
   }
   @Override
   @Transactional
   public int deleteBatchByPrimaryKey(List<Long> list) {
@@ -83,25 +56,167 @@
      return homeNavbarMapper.deleteBatchByPrimaryKey(list);
   }
   @Override
   public int getMaxOrder() {
      return homeNavbarMapper.getMaxOrder();
   }
   @Override
   public List<HomeNavbar> getChangeOrder(Integer type, Integer order) {
      return homeNavbarMapper.getChangeOrder(type, order);
   public void saveObject(MultipartFile file, HomeNavbar record) throws HomeNavbarException, Exception{
      String name = record.getName();
      if (name == null || name.trim().length() == 0) {
         throw new HomeNavbarException(1, "导航名称不能为空");
      }
      // 时间转换
      conversionTime(record);
      // 图片上传
      String picture = null;
      if (file != null) {
         picture = uploadPicture(file);
      }
      Long id = record.getId();
      if (id == null) {
         record.setPicture(picture);
         record.setCreatetime(new Date());
         record.setUpdatetime(new Date());
         Integer state = record.getState();
         if (state == null) {
            record.setState(0);
         }
         int maxOrder = homeNavbarMapper.getMaxOrder();
         record.setOrderby(maxOrder + 1);
         homeNavbarMapper.insert(record);
      } else {
         // 修改
         HomeNavbar resultObj = homeNavbarMapper.selectByPrimaryKey(id);
         if (resultObj == null) {
            throw new HomeNavbarException(1, "修改内容已不存在");
         }
         if (picture != null && picture.trim().length() > 0) {
            // 删除老图
            removePicture(resultObj.getPicture());
            // 存储新图
            record.setPicture(picture);
         } else {
            record.setPicture(resultObj.getPicture());
         }
         record.setOrderby(resultObj.getOrderby());
         record.setCreatetime(resultObj.getCreatetime());
         record.setUpdatetime(new Date());
         homeNavbarMapper.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/HomeNavbar/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      return fileLink;
   }
   /**
    * 删除图片-不更新数据库
    * @param record
    * @throws Exception
    */
   public void removePicture(String picture) throws Exception {
      if (picture != null && picture.trim().length() > 0) {
         COSManager.getInstance().deleteFile(picture);
      }
   }
   /**
    * web段时间转换
    * @param record
    */
   public void conversionTime(HomeNavbar record) throws ActivityException, Exception {
      // 是否时间控制
      if(!record.isTimeTask()) {
         record.setStartTime(null);
         record.setEndTime(null);
      } else {
         String startTime_str = record.getStartTime_str();
         String endTime_str = record.getEndTime_str();
         if ((startTime_str == null|| startTime_str.trim().length() == 0)
               && (endTime_str == null || endTime_str.trim().length() == 0)) {
            throw new ActivityException(1, "请输入控制时间");
         } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            if (startTime_str != null && startTime_str.trim().length() > 0) {
               startTime_str = startTime_str.replaceAll("T", " ");
               record.setStartTime(format.parse(startTime_str));
            }
            if (endTime_str != null && endTime_str.trim().length() > 0) {
               endTime_str = endTime_str.replaceAll("T", " ");
               record.setEndTime(format.parse(endTime_str));
            }
         }
      }
   }
   @Override
   public List<HomeNavbar> listQuery(long start, int count, String key) {
   public void updateOrder(Long id, Integer moveType) throws HomeNavbarException, Exception{
      if (moveType == null || (!moveType.equals(1) && !moveType.equals(-1))) {
         throw new HomeNavbarException(1, "传递的类型不正确");
      }
      if (id == null) {
         throw new HomeNavbarException(1, "ID不能为空");
      }
      HomeNavbar resultObj = homeNavbarMapper.selectByPrimaryKey(id);
      if (resultObj == null) {
         throw new HomeNavbarException(1, "操作数据已不存在");
      }
      Integer oldOrder = resultObj.getOrderby();
      HomeNavbar changeObj = homeNavbarMapper.getChangeOrder(moveType, oldOrder);
      if (changeObj == null ) {
         throw new HomeNavbarException(1, "已经在最边缘,无可交换的位置");
      }
      // 交换排序序号
      resultObj.setOrderby(changeObj.getOrderby());
      changeObj.setOrderby(oldOrder);
      homeNavbarMapper.updateByPrimaryKeySelective(changeObj);
      homeNavbarMapper.updateByPrimaryKeySelective(resultObj);
   }
   @Override
   public List<HomeNavbar> listQueryAndBusinessSystem(long start, int count, String key) {
      
      List<HomeNavbar> listObj = homeNavbarMapper.listQuery(start, count, key);
      if (listObj == null || listObj.size() == 0) {
         return null;
      }
      
      List<System> systemList = systemService.getSystems();
      List<BusinessSystem> systemList = businessSystemService.getBusinessSystems();
      
      List<Long> listId = new ArrayList<Long>();
      for (HomeNavbar homeNavbar: listObj) {
@@ -119,12 +234,12 @@
         for (HomeNavbar homeNavbar: listObj) {
            
            Long id = homeNavbar.getId();
            List<System> newList = new ArrayList<System>();
            List<BusinessSystem> newList = new ArrayList<BusinessSystem>();
            
            // 是否有关联系统选项
            for (System dsystem : systemList) {
            for (BusinessSystem dsystem : systemList) {
               
               System newsystem = new System();
               BusinessSystem newsystem = new BusinessSystem();
               try {
                  PropertyUtils.copyProperties(newsystem, dsystem);
               } catch (Exception e) {
@@ -136,7 +251,7 @@
                  
                  for (SuperHomeNavbar superHomeNavbar : listSuper) {
                     HomeNavbar navbar = superHomeNavbar.getHomeNavbar();
                     System system = superHomeNavbar.getSystem();
                     BusinessSystem system = superHomeNavbar.getSystem();
                     
                     // 当前专题 、当前系统
                     if (navbar != null && system != null && id == navbar.getId() 
@@ -162,35 +277,57 @@
      return listObj;
   }
   @Override
   public List<HomeNavbar> listQuery(long start, int count, String key) {
      List<HomeNavbar> listObj = homeNavbarMapper.listQuery(start, count, key);
      if (listObj == null || listObj.size() == 0) {
         return null;
      }
      for (HomeNavbar homeNavbar: listObj) {
         Date startTime = homeNavbar.getStartTime();
         Date endTime = homeNavbar.getEndTime();
         if (startTime == null && endTime == null) {
            homeNavbar.setTimeTask(false);
            homeNavbar.setStartTime_str("");
            homeNavbar.setEndTime_str("");
         } else {
            homeNavbar.setTimeTask(true);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            if (startTime == null) {
               homeNavbar.setStartTime_str("");
            } else {
               homeNavbar.setStartTime_str(sdf.format(startTime));
            }
            if (endTime == null) {
               homeNavbar.setEndTime_str("");
            } else {
               homeNavbar.setEndTime_str(sdf.format(endTime));
            }
         }
      }
      return listObj;
   }
   @Override
   public long countlistQuery(String key) {
      return homeNavbarMapper.countListQuery(key);
   }
   @Override
   public List<HomeNavbar> listQueryEffective() {
      return homeNavbarMapper.listQueryEffective();
   }
   @Override
   public void uploadPicture(MultipartFile file, HomeNavbar homeNavbar) throws Exception {
      InputStream inputStream = file.getInputStream();
      String contentType = file.getContentType();
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 上传文件相对位置
      String fileUrl="HomeNavbar/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
      /*  修改图片时,先删除已存在图片  */
      String picture = homeNavbar.getPicture();
      if (!StringUtil.isNullOrEmpty(picture)) {
         COSManager.getInstance().deleteFile(picture);
      }
      String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
      /*  更新数据库信息  */
      if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
         homeNavbar.setUpdatetime(new Date());
         homeNavbar.setPicture(uploadFilePath);
         updateByPrimaryKeySelective(homeNavbar);
      }
   public HomeNavbar getEffectiveByClassId(Long classId) {
      return homeNavbarMapper.getEffectiveByClassId(classId);
   }
}