admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/lable/LabelServiceImpl.java
@@ -1,397 +1,398 @@
package com.yeshi.fanli.service.impl.lable;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
import com.yeshi.fanli.dao.mybatis.lable.LabelClassMapper;
import com.yeshi.fanli.dao.mybatis.lable.LabelGoodsMapper;
import com.yeshi.fanli.dao.mybatis.lable.LabelMapper;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.LabelGoods;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.exception.LabelException;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.util.StringUtil;
import jxl.Sheet;
import jxl.Workbook;
@Service
public class LabelServiceImpl implements LabelService {
   @Resource
   private LabelMapper labelMapper;
   @Resource
   private LabelGoodsMapper labelGoodsMapper;
   @Resource
   private LabelClassMapper labelClassMapper;
   @Override
   public int insertSelective(Label record) throws LabelException{
      return labelMapper.insertSelective(record);
   }
   @Override
   @Transactional
   public void insertList(List<Label>  records,AdminUser admin) throws LabelException{
      if (records != null && records.size() > 0) {
         for (Label label : records) {
            //   录入方式
            label.setEntrymode(Label.MODE_BACKSTAGE);
            Date nowTime = new Date();
            // 录入时间。人
            label.setCreatetime(nowTime);
            label.setUpdatetime(nowTime);
            label.setCreateUser(admin);
            label.setIosClick(0l);
            label.setAndroidClick(0l);
            labelMapper.insertSelective(label);
         }
      }
   }
   @Override
   public void insertSingle(Label label, AdminUser admin,MultipartFile file) throws Exception{
      Date nowTime = new Date();
      // 录入时间。人
      label.setCreatetime(nowTime);
      label.setUpdatetime(nowTime);
      label.setCreateUser(admin);
      label.setIosClick(0l);
      label.setAndroidClick(0l);
      //   录入方式
      label.setEntrymode(Label.MODE_BACKSTAGE);
      labelMapper.insertSelective(label);
      // 上传图片
      if (file != null) {
         uploadPicture(file, label);
      }
   }
   @Override
   @Transactional
   public void updateList(List<Label>  records) throws LabelException{
      if (records != null && records.size() > 0) {
         for (Label label : records) {
            Date nowTime = new Date();
            label.setUpdatetime(nowTime);
            labelMapper.updateByPrimaryKeySelective(label);
         }
      }
   }
   @Override
   public int updateByPrimaryKey(Label record) throws LabelException{
      // TODO Auto-generated method stub
      return labelMapper.updateByPrimaryKey(record);
   }
   @Override
   public int updateByPrimaryKeySelective(Label record) throws LabelException{
      // TODO Auto-generated method stub
      return labelMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   @Transactional
   public int deleteBatchById(long[] ids) throws LabelException{
      return labelMapper.deleteBatchByPrimaryKey(ids);
   }
   @Override
   @Transactional
   public void deleteBatchByPrimaryKey(List<Long> ids) throws LabelException {
      for (Long id : ids) {
         Label label = labelMapper.selectByPrimaryKey(id);
         if (label != null) {
            // 删除图片
            deleteImg(label);
            // 删除数据信息
            labelMapper.deleteByPrimaryKey(id);
            // 删除商品对应的标签
            labelGoodsMapper.deleteByLabId(id);
            // 删除分类对应的标签
            labelClassMapper.deleteByLabId(id);
         }
      }
   }
   @Override
   public Label selectByPrimaryKey(Long id) throws LabelException{
      return labelMapper.selectByPrimaryKey(id);
   }
   @Override
   public List<Label> selectByTitle(String title) throws LabelException{
      return labelMapper.selectByTitle(title);
   }
   @Override
   @Cacheable(value = "labelCache",key="'getByTitleCache-'+#labKey +'-'+#title")
   public Label getByTitleCache(String labKey, String title) throws LabelException{
      return labelMapper.getByTitle(title);
   }
   @Override
   @Cacheable(value = "labelCache",key="'listByTitlesCache-'+#list")
   public List<Label> listByTitlesCache(List<String> list){
      return labelMapper.listByTitles(list);
   }
   @Override
   public List<Label> query(int pageIndex, int pageSize, String key,
         String startTime, String endTime, String orderMode) throws LabelException {
      return labelMapper.query(pageIndex, pageSize, key, startTime, endTime,orderMode);
   }
   @Override
   public int getQueryCount( String key, String startTime, String endTime) throws LabelException {
      return labelMapper.getQueryCount(key, startTime, endTime);
   }
   /**
    * 从excel文件解析订单
    *
    * @param in
    * @return
    */
   @Override
   public void analysisExcel(InputStream in, AdminUser admin) throws Exception {
      jxl.Workbook rwb = Workbook.getWorkbook(in);
      Sheet sheet = rwb.getSheet(0);
      Date nowTime = new Date();
      Set<String> titles = new HashSet<String>();
      for (int r = 1; r < sheet.getRows(); r++) {
         for (int c = 0; c < sheet.getColumns(); c++) {
            String content = sheet.getCell(c, r).getContents();
            if (StringUtil.isNullOrEmpty(content))
               break;// 为空则结束当前行遍历
            // 去掉重复字段
            titles.add(content.trim());
         }
      }
      if (titles != null && titles.size() > 0) {
         for (String title : titles) {
            List<Label> labels = selectByTitle(title.trim());
            // 若已存在则不存入数据库
            if (labels == null || labels.size() == 0) {
               Label label = new Label();
               label.setTitle(title.trim()); // 名称
               label.setEntrymode(Label.MODE_EXCEL); // 批量录入
               label.setCreatetime(nowTime);
               label.setUpdatetime(nowTime);
               label.setCreateUser(admin);
               insertSelective(label);
            }
         }
      }
   }
   @Override
   public int uploadPicture(MultipartFile file, Label label) throws Exception {
      InputStream inputStream = file.getInputStream();
      String contentType = file.getContentType();
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 上传文件相对位置
      String fileUrl="LableImg/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
      boolean deleteFile =true;
      /*  修改图片时,先删除已存在图片  */
      String picture = label.getPicture();
      if (!StringUtil.isNullOrEmpty(picture))
         deleteFile = COSManager.getInstance().deleteFile(picture);
      int result = 2;
      /*  上传新图片  */
      if (deleteFile) {
         String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
         /*  更新数据库信息  */
         if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            Date nowTime = new Date();
            label.setPicture(uploadFilePath);
            label.setUpdatetime(nowTime);
            result = updateByPrimaryKeySelective(label);
         }
      }
      return result;
   }
   /**
    * 删除图片
    */
   @Override
   public int deleteImg(Label label) throws LabelException {
      String fileUrl = label.getPicture();
      int result = 2;
      boolean deleteFile = true;
      if (!StringUtil.isNullOrEmpty(fileUrl)) {
         deleteFile = COSManager.getInstance().deleteFile(fileUrl);
         if (deleteFile) {
            Date nowTime = new Date();
            label.setPicture(null);
            label.setUpdatetime(nowTime);
            // 更新数据库
            result = updateByPrimaryKey(label);
         }
      }
      return result;
   }
   @Override
   public Map<String, Object> getCountByEntryMode() throws LabelException{
      return labelMapper.getCountByEntryMode();
   }
   @Override
   public long getCountToday() throws LabelException{
      return labelMapper.getCountToday();
   }
   @Override
   public long getCount() {
      return labelMapper.getCount();
   }
   @Override
   public List<Label> queryGoodsCandidate(int start, int count, String key, Long classId) throws LabelException{
      return labelMapper.queryGoodsCandidate(start, count, key, classId);
   }
   @Override
   public int getCountQueryGoodsCandidate( String key, Long classId) throws LabelException{
      return labelMapper.getCountQueryGoodsCandidate(key, classId);
   }
   @Override
   public List<Label> queryClassCandidate(int start, int count, String key, Long classId) throws LabelException{
      return labelMapper.queryClassCandidate(start, count, key, classId);
   }
   @Override
   public int getCountQueryClassCandidate( String key, Long classId) throws LabelException{
      return labelMapper.getCountQueryClassCandidate(key, classId);
   }
   @Override
   public List<Label> querySubClassCandidate(int start, int count, String key, Long subClassId) throws LabelException{
      return labelMapper.querySubClassCandidate(start, count, key, subClassId);
   }
   @Override
   public int getCountQuerySubClassCandidate( String key, Long subClassId) throws LabelException{
      return labelMapper.getCountQuerySubClassCandidate(key, subClassId);
   }
   @Override
   public List<LabelGoods> getByGoodsId(Long goodsId) {
      return labelMapper.getByGoodsId(goodsId);
   }
   @Override
   public List<Label> addBatchByNames(String lableNames, AdminUser admin) throws LabelException{
      if (lableNames == null || lableNames.trim().length() == 0) {
         return null;
      }
      List<Label> listLabs = new ArrayList<Label>();
      // 空格隔开
      String[] arrtitles = lableNames.split("\\s+");
      for (String title : arrtitles) {
         /* 遍历标签名称:查询数据库中是否已存在 */
         List<Label> labels = selectByTitle(title.trim());
         if (labels != null && labels.size() > 0) {
            Label label = labels.get(0);
            listLabs.add(label);
         } else {
            // 新增
            Label label = new Label();
            label.setTitle(title);// 名称
            label.setEntrymode(Label.MODE_SYSTEM); // 批量录入
            label.setCreatetime(new Date());
            label.setUpdatetime(new Date());
            label.setCreateUser(admin);
            label.setIosClick(0l);
            label.setAndroidClick(0l);
            insertSelective(label);
            listLabs.add(label);
         }
      }
      return listLabs;
   }
}
package com.yeshi.fanli.service.impl.lable;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
import com.yeshi.fanli.dao.mybatis.lable.LabelClassMapper;
import com.yeshi.fanli.dao.mybatis.lable.LabelGoodsMapper;
import com.yeshi.fanli.dao.mybatis.lable.LabelMapper;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.LabelGoods;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.exception.goods.quality.LabelException;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import jxl.Sheet;
import jxl.Workbook;
@Service
public class LabelServiceImpl implements LabelService {
   @Resource
   private LabelMapper labelMapper;
   @Resource
   private LabelGoodsMapper labelGoodsMapper;
   @Resource
   private LabelClassMapper labelClassMapper;
   @Override
   public int insertSelective(Label record) throws LabelException{
      return labelMapper.insertSelective(record);
   }
   @Override
   @Transactional(rollbackFor=Exception.class)
   public void insertList(List<Label>  records,AdminUser admin) throws LabelException{
      if (records != null && records.size() > 0) {
         for (Label label : records) {
            //   录入方式
            label.setEntrymode(Label.MODE_BACKSTAGE);
            Date nowTime = new Date();
            // 录入时间。人
            label.setCreatetime(nowTime);
            label.setUpdatetime(nowTime);
            label.setCreateUser(admin);
            label.setIosClick(0l);
            label.setAndroidClick(0l);
            labelMapper.insertSelective(label);
         }
      }
   }
   @Override
   public void insertSingle(Label label, AdminUser admin,MultipartFile file) throws Exception{
      Date nowTime = new Date();
      // 录入时间。人
      label.setCreatetime(nowTime);
      label.setUpdatetime(nowTime);
      label.setCreateUser(admin);
      label.setIosClick(0l);
      label.setAndroidClick(0l);
      //   录入方式
      label.setEntrymode(Label.MODE_BACKSTAGE);
      labelMapper.insertSelective(label);
      // 上传图片
      if (file != null) {
         uploadPicture(file, label);
      }
   }
   @Override
   @Transactional(rollbackFor=Exception.class)
   public void updateList(List<Label>  records) throws LabelException{
      if (records != null && records.size() > 0) {
         for (Label label : records) {
            Date nowTime = new Date();
            label.setUpdatetime(nowTime);
            labelMapper.updateByPrimaryKeySelective(label);
         }
      }
   }
   @Override
   public int updateByPrimaryKey(Label record) throws LabelException{
      // TODO Auto-generated method stub
      return labelMapper.updateByPrimaryKey(record);
   }
   @Override
   public int updateByPrimaryKeySelective(Label record) throws LabelException{
      // TODO Auto-generated method stub
      return labelMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   @Transactional(rollbackFor=Exception.class)
   public int deleteBatchById(long[] ids) throws LabelException{
      return labelMapper.deleteBatchByPrimaryKey(ids);
   }
   @Override
   @Transactional(rollbackFor=Exception.class)
   public void deleteBatchByPrimaryKey(List<Long> ids) throws LabelException {
      for (Long id : ids) {
         Label label = labelMapper.selectByPrimaryKey(id);
         if (label != null) {
            // 删除图片
            deleteImg(label);
            // 删除数据信息
            labelMapper.deleteByPrimaryKey(id);
            // 删除商品对应的标签
            labelGoodsMapper.deleteByLabId(id);
            // 删除分类对应的标签
            labelClassMapper.deleteByLabId(id);
         }
      }
   }
   @Override
   public Label selectByPrimaryKey(Long id) throws LabelException{
      return labelMapper.selectByPrimaryKey(id);
   }
   @Override
   public List<Label> selectByTitle(String title) throws LabelException{
      return labelMapper.selectByTitle(title);
   }
   @Override
   @Cacheable(value = "labelCache",key="'getByTitleCache-'+#labKey +'-'+#title")
   public Label getByTitleCache(String labKey, String title) throws LabelException{
      return labelMapper.getByTitle(title);
   }
   @Override
   @Cacheable(value = "labelCache",key="'listByTitlesCache-'+#list")
   public List<Label> listByTitlesCache(List<String> list){
      return labelMapper.listByTitles(list);
   }
   @Override
   public List<Label> query(int pageIndex, int pageSize, String key,
         String startTime, String endTime, String orderMode) throws LabelException {
      return labelMapper.query(pageIndex, pageSize, key, startTime, endTime,orderMode);
   }
   @Override
   public int getQueryCount( String key, String startTime, String endTime) throws LabelException {
      return labelMapper.getQueryCount(key, startTime, endTime);
   }
   /**
    * 从excel文件解析订单
    *
    * @param in
    * @return
    */
   @Override
   public void analysisExcel(InputStream in, AdminUser admin) throws Exception {
      jxl.Workbook rwb = Workbook.getWorkbook(in);
      Sheet sheet = rwb.getSheet(0);
      Date nowTime = new Date();
      Set<String> titles = new HashSet<String>();
      for (int r = 1; r < sheet.getRows(); r++) {
         for (int c = 0; c < sheet.getColumns(); c++) {
            String content = sheet.getCell(c, r).getContents();
            if (StringUtil.isNullOrEmpty(content))
               break;// 为空则结束当前行遍历
            // 去掉重复字段
            titles.add(content.trim());
         }
      }
      if (titles != null && titles.size() > 0) {
         for (String title : titles) {
            List<Label> labels = selectByTitle(title.trim());
            // 若已存在则不存入数据库
            if (labels == null || labels.size() == 0) {
               Label label = new Label();
               label.setTitle(title.trim()); // 名称
               label.setEntrymode(Label.MODE_EXCEL); // 批量录入
               label.setCreatetime(nowTime);
               label.setUpdatetime(nowTime);
               label.setCreateUser(admin);
               insertSelective(label);
            }
         }
      }
   }
   @Override
   public int uploadPicture(MultipartFile file, Label label) throws Exception {
      InputStream inputStream = file.getInputStream();
      String contentType = file.getContentType();
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 上传文件相对位置
      String fileUrl=FilePathEnum.lable.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
      boolean deleteFile =true;
      /*  修改图片时,先删除已存在图片  */
      String picture = label.getPicture();
      if (!StringUtil.isNullOrEmpty(picture))
         deleteFile = COSManager.getInstance().deleteFile(picture);
      int result = 2;
      /*  上传新图片  */
      if (deleteFile) {
         String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
         /*  更新数据库信息  */
         if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            Date nowTime = new Date();
            label.setPicture(uploadFilePath);
            label.setUpdatetime(nowTime);
            result = updateByPrimaryKeySelective(label);
         }
      }
      return result;
   }
   /**
    * 删除图片
    */
   @Override
   public int deleteImg(Label label) throws LabelException {
      String fileUrl = label.getPicture();
      int result = 2;
      boolean deleteFile = true;
      if (!StringUtil.isNullOrEmpty(fileUrl)) {
         deleteFile = COSManager.getInstance().deleteFile(fileUrl);
         if (deleteFile) {
            Date nowTime = new Date();
            label.setPicture(null);
            label.setUpdatetime(nowTime);
            // 更新数据库
            result = updateByPrimaryKey(label);
         }
      }
      return result;
   }
   @Override
   public Map<String, Object> getCountByEntryMode() throws LabelException{
      return labelMapper.getCountByEntryMode();
   }
   @Override
   public long getCountToday() throws LabelException{
      return labelMapper.getCountToday();
   }
   @Override
   public long getCount() {
      return labelMapper.getCount();
   }
   @Override
   public List<Label> queryGoodsCandidate(int start, int count, String key, Long classId) throws LabelException{
      return labelMapper.queryGoodsCandidate(start, count, key, classId);
   }
   @Override
   public int getCountQueryGoodsCandidate( String key, Long classId) throws LabelException{
      return labelMapper.getCountQueryGoodsCandidate(key, classId);
   }
   @Override
   public List<Label> queryClassCandidate(int start, int count, String key, Long classId) throws LabelException{
      return labelMapper.queryClassCandidate(start, count, key, classId);
   }
   @Override
   public int getCountQueryClassCandidate( String key, Long classId) throws LabelException{
      return labelMapper.getCountQueryClassCandidate(key, classId);
   }
   @Override
   public List<Label> querySubClassCandidate(int start, int count, String key, Long subClassId) throws LabelException{
      return labelMapper.querySubClassCandidate(start, count, key, subClassId);
   }
   @Override
   public int getCountQuerySubClassCandidate( String key, Long subClassId) throws LabelException{
      return labelMapper.getCountQuerySubClassCandidate(key, subClassId);
   }
   @Override
   public List<LabelGoods> getByGoodsId(Long goodsId) {
      return labelMapper.getByGoodsId(goodsId);
   }
   @Override
   public List<Label> addBatchByNames(String lableNames, AdminUser admin) throws LabelException{
      if (lableNames == null || lableNames.trim().length() == 0) {
         return null;
      }
      List<Label> listLabs = new ArrayList<Label>();
      // 空格隔开
      String[] arrtitles = lableNames.split("\\s+");
      for (String title : arrtitles) {
         /* 遍历标签名称:查询数据库中是否已存在 */
         List<Label> labels = selectByTitle(title.trim());
         if (labels != null && labels.size() > 0) {
            Label label = labels.get(0);
            listLabs.add(label);
         } else {
            // 新增
            Label label = new Label();
            label.setTitle(title);// 名称
            label.setEntrymode(Label.MODE_SYSTEM); // 批量录入
            label.setCreatetime(new Date());
            label.setUpdatetime(new Date());
            label.setCreateUser(admin);
            label.setIosClick(0l);
            label.setAndroidClick(0l);
            insertSelective(label);
            listLabs.add(label);
         }
      }
      return listLabs;
   }
}