package com.yeshi.fanli.service.impl.help; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.yeshi.utils.tencentcloud.COSManager; import com.yeshi.fanli.dao.mybatis.help.HelpClassMapper; import com.yeshi.fanli.entity.bus.help.HelpClass; import com.yeshi.fanli.exception.HelpClassException; import com.yeshi.fanli.service.inter.help.HelpClassService; import com.yeshi.fanli.util.StringUtil; @Service public class HelpClassServiceImpl implements HelpClassService { @Resource private HelpClassMapper helpClassMapper; @Override public int deleteByPrimaryKeyBatch(List list) { return helpClassMapper.deleteByPrimaryKeyBatch(list); } @Override public HelpClass selectByPrimaryKey(Long id) { return helpClassMapper.selectByPrimaryKey(id); } @Override public void save(HelpClass helpClass) throws HelpClassException { if (helpClass == null) { throw new HelpClassException(1, "参数不能为空"); } if (helpClass.getId() == null) { String name = helpClass.getName(); if (StringUtil.isNullOrEmpty(name)) { throw new HelpClassException(1, "名称不能为空"); } // 默认不显示 helpClass.setShowState(0); // 排序-末尾 int maxOrder = helpClassMapper.getMaxOrder(); helpClass.setSort(maxOrder + 1); helpClass.setCreateTime(new Date()); helpClass.setUpdateTime(new Date()); helpClassMapper.insertSelective(helpClass); } else { helpClass.setUpdateTime(new Date()); helpClassMapper.updateByPrimaryKeySelective(helpClass); } } @Override public int updateShowState(Long id) throws HelpClassException { if (id == null) { throw new HelpClassException(1, "传递参数不能为空"); } HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id); if (helpClass == null) { throw new HelpClassException(1, "更新数据已不存在"); } Integer showState = helpClass.getShowState(); if (showState == null || showState == 0) { showState = 1; } else { showState = 0; } HelpClass help = new HelpClass(); help.setId(id); help.setShowState(showState); help.setUpdateTime(new Date()); helpClassMapper.updateByPrimaryKeySelective(help); return showState; } @Override public void updateSort(Integer moveType, Long id) throws HelpClassException { if (id == null || moveType == null) { throw new HelpClassException(1, "传递参数不能为空"); } HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id); if (helpClass == null) { throw new HelpClassException(1, "更新数据已不存在"); } HelpClass changeObjct = helpClassMapper.getChangeOrder(moveType, helpClass.getSort()); if (changeObjct == null) { throw new HelpClassException(1, "已经是最边缘位置"); } else { Integer changeSort = changeObjct.getSort(); // 交换排序序号 changeObjct.setSort(helpClass.getSort()); helpClassMapper.updateByPrimaryKeySelective(changeObjct); helpClass.setSort(changeSort); helpClassMapper.updateByPrimaryKeySelective(helpClass); } } @Override public void uploadPic(MultipartFile file, Long id) throws HelpClassException, IOException { if (id == null || file == null) { throw new HelpClassException(1, "传递参数不能为空"); } HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id); if (helpClass == null) { throw new HelpClassException(1, "更新数据已不存在"); } InputStream inputStream = file.getInputStream(); String contentType = file.getContentType(); String type = contentType.substring(contentType.indexOf("/") + 1); String filePath ="/img/HelpClass/" + UUID.randomUUID().toString().replace("-", "") + "." + type; /* 修改图片时,先删除已存在图片 */ String picture = helpClass.getPicture(); if (!StringUtil.isNullOrEmpty(picture)) { COSManager.getInstance().deleteFile(picture); } String fileUrl = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl(); /* 更新数据库信息 */ if (!StringUtil.isNullOrEmpty(fileUrl)) { helpClass.setUpdateTime(new Date()); helpClass.setPicture(fileUrl); helpClassMapper.updateByPrimaryKeySelective(helpClass); } } @Override public List query(int start, int count, String key, Integer state) throws HelpClassException { return helpClassMapper.listQuery(start, count, key, state); } @Override public long countQuery(String key, Integer state) { return helpClassMapper.countQuery(key, state); } @Override public List getClassByState(Integer state) throws HelpClassException { return helpClassMapper.getClassByState(state); } @Override public List getProvidedClass() throws HelpClassException { return helpClassMapper.getProvidedClass(); } }