|
@@ -1,27 +1,17 @@
|
|
|
package com.pj.tb_goods_type;
|
|
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.pj.tb_goods.TbGoods;
|
|
|
+import com.pj.tb_goods.TbGoodsMapper;
|
|
|
import com.pj.utils.so.SoMap;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import com.pj.utils.sg.*;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import javax.swing.filechooser.FileSystemView;
|
|
|
|
|
|
/**
|
|
|
* Service: tb_goods_type -- 商品分类
|
|
@@ -34,9 +24,8 @@ public class TbGoodsTypeService extends ServiceImpl<TbGoodsTypeMapper, TbGoodsTy
|
|
|
/** 底层 Mapper 对象 */
|
|
|
@Autowired
|
|
|
TbGoodsTypeMapper tbGoodsTypeMapper;
|
|
|
-
|
|
|
@Autowired
|
|
|
- private MethodGoodsTypeService methodGoodsTypeService;
|
|
|
+ TbGoodsMapper tbGoodsMapper;
|
|
|
|
|
|
/** 增 */
|
|
|
void add(TbGoodsType t){
|
|
@@ -65,97 +54,62 @@ public class TbGoodsTypeService extends ServiceImpl<TbGoodsTypeMapper, TbGoodsTy
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 导入
|
|
|
- * @param file excel文件
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public String importData(MultipartFile file) throws IOException {
|
|
|
- System.out.println("\n开始执行文件上传....\n");
|
|
|
+ *
|
|
|
+ *向商品分类添加商品
|
|
|
+ * @author loovi
|
|
|
+ * @date
|
|
|
+ */
|
|
|
+ public int AddGoodsToGoodsType(Long goodsTypeId,Long [] goodsIds){
|
|
|
+ // 获取当前登录用户id
|
|
|
+// Long loginId = StpUserUtil.getLoginIdAsLong();
|
|
|
+ int line = 0;
|
|
|
+ // 获取商品类型信息
|
|
|
+ com.pj.tb_goods_type.TbGoodsType tbGoodsType = tbGoodsTypeMapper.selectById(goodsTypeId);
|
|
|
+ // 如果商品类型不存在则抛异常
|
|
|
+ if (Objects.isNull(tbGoodsType)){
|
|
|
+ throw new RuntimeException("该商品类型不存在");
|
|
|
+ }
|
|
|
+ List<Long> list = Arrays.asList(goodsIds);
|
|
|
+ // 把商品与类型关联id写入商品类型商品关系表
|
|
|
+ for (Long goodsId : list) {
|
|
|
+ TbGoods tbGoods = tbGoodsMapper.selectById(goodsId);
|
|
|
+ if(Objects.isNull(tbGoods)){
|
|
|
+ throw new RuntimeException("该商品不存在");
|
|
|
+ }
|
|
|
+ int i =tbGoodsTypeMapper.addGoodsToGoodsType(goodsTypeId,goodsId);
|
|
|
+ line = i+1;
|
|
|
+ }
|
|
|
+ return line;
|
|
|
|
|
|
- //判空
|
|
|
- if(file.isEmpty()) return "文件为空,无法执行上传...";
|
|
|
- //获取文件上传数据
|
|
|
- HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
|
|
|
- //获取第一页sheet
|
|
|
- HSSFSheet sheet = wb.getSheetAt(0);
|
|
|
- //定义计数器
|
|
|
- int count = 0;
|
|
|
- //定义行对象
|
|
|
- HSSFRow row = null;
|
|
|
- //解析数据封装到集合
|
|
|
- count = methodGoodsTypeService.importMethod(row, sheet, count);
|
|
|
- wb.close();
|
|
|
- System.out.println("\n文件上传完成,共上传 " + count + "条 " + "数据...\n");
|
|
|
- return "上传完成,共上传" + count + "条" + "数据。";
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
- * 导出 excel文件
|
|
|
- * @param keyword
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String outportExcel(String keyword,String filepath) throws IOException {
|
|
|
- System.out.println("\n开始执行文件导出....\n");
|
|
|
- //导出的文件的路径
|
|
|
- if(filepath == null){
|
|
|
- // 获取当前用户的桌面路径
|
|
|
- FileSystemView fileSystemView = FileSystemView.getFileSystemView();
|
|
|
- filepath = fileSystemView.getHomeDirectory().getPath();
|
|
|
+ *删除商品分类中的商品
|
|
|
+ *
|
|
|
+ * @author loovi
|
|
|
+ * @date
|
|
|
+ */
|
|
|
+ public int delGoodsFromGoodsType(Long goodsTypeId,Long [] goodsIds){
|
|
|
+
|
|
|
+ // 获取当前登录用户id
|
|
|
+// Long loginId = StpUserUtil.getLoginIdAsLong();
|
|
|
+ int line = 0;
|
|
|
+ // 获取商品类型信息
|
|
|
+ com.pj.tb_goods_type.TbGoodsType tbGoodsType = tbGoodsTypeMapper.selectById(goodsTypeId);
|
|
|
+ // 如果商品类型不存在则抛异常
|
|
|
+ if (Objects.isNull(tbGoodsType)){
|
|
|
+ throw new RuntimeException("该商品类型不存在");
|
|
|
}
|
|
|
- filepath = filepath + "\\商品分类数据表_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ".xlsx";
|
|
|
- //根据需求查询数据
|
|
|
- List<TbGoodsType> selectedList = tbGoodsTypeMapper.selectList
|
|
|
- (new LambdaQueryWrapper<TbGoodsType>().eq(StringUtils.isNoneBlank(keyword), TbGoodsType::getName, keyword));
|
|
|
- if(selectedList.size() == 0)return "没有可导出的数据。";
|
|
|
- //建立excel对象封装数据
|
|
|
- HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
- //创建excel表格右下角的sheet页名称
|
|
|
- HSSFSheet sheet = workbook.createSheet("1");
|
|
|
- //创建表头
|
|
|
- HSSFRow row = sheet.createRow(0);
|
|
|
- row.createCell(0).setCellValue("序号");
|
|
|
- row.createCell(1).setCellValue("名称");
|
|
|
- row.createCell(2).setCellValue("排序");
|
|
|
- row.createCell(3).setCellValue("编号");
|
|
|
- row.createCell(12).setCellValue("创建时间");
|
|
|
- row.createCell(13).setCellValue("创建人编号");
|
|
|
- row.createCell(14).setCellValue("创建人名称");
|
|
|
- row.createCell(15).setCellValue("更新时间");
|
|
|
- row.createCell(16).setCellValue("更新人编号");
|
|
|
- row.createCell(17).setCellValue("更新人名称");
|
|
|
- row.createCell(18).setCellValue("删除状态");
|
|
|
-
|
|
|
- //定义计数器
|
|
|
- int count = 0;
|
|
|
- //遍历集合
|
|
|
- for (int i = 0; i < selectedList.size(); i++) {
|
|
|
- HSSFRow sheetRow = sheet.createRow(sheet.getLastRowNum() + 1);
|
|
|
- sheetRow.createCell(0).setCellValue(i + 1);
|
|
|
- sheetRow.createCell(1).setCellValue((selectedList.get(i).getName() + "").equals("null")? "": selectedList.get(i).getName() + "");
|
|
|
- sheetRow.createCell(2).setCellValue((selectedList.get(i).getSort() + "").equals("null")? "": selectedList.get(i).getSort() + "");
|
|
|
- sheetRow.createCell(3).setCellValue((selectedList.get(i).getNo() + "").equals("null")? "": selectedList.get(i).getNo() + "");
|
|
|
- sheetRow.createCell(22).setCellValue((selectedList.get(i).getCreateTime() + "").equals("null")? "": selectedList.get(i).getCreateTime() + "");
|
|
|
- sheetRow.createCell(23).setCellValue((selectedList.get(i).getCreateBy() + "").equals("null")? "": selectedList.get(i).getCreateBy() + "");
|
|
|
- sheetRow.createCell(24).setCellValue((selectedList.get(i).getCreateName() + "").equals("null")? "": selectedList.get(i).getCreateName() + "");
|
|
|
- sheetRow.createCell(25).setCellValue((selectedList.get(i).getUpdateTime() + "").equals("null")? "": selectedList.get(i).getUpdateTime() + "");
|
|
|
- sheetRow.createCell(26).setCellValue((selectedList.get(i).getUpdateBy() + "").equals("null")? "": selectedList.get(i).getUpdateBy() + "");
|
|
|
- sheetRow.createCell(27).setCellValue((selectedList.get(i).getUpdateName() + "").equals("null")? "": selectedList.get(i).getUpdateName() + "");
|
|
|
- sheetRow.createCell(28).setCellValue((selectedList.get(i).getDeleteStatus() + "").equals("null")? "": selectedList.get(i).getDeleteStatus() + "");
|
|
|
- count += 1;
|
|
|
+ List<Long> list = Arrays.asList(goodsIds);
|
|
|
+ // 从商品类型商品关系表把商品与类型关联删除
|
|
|
+ for (Long goodsId : list) {
|
|
|
+ TbGoods tbGoods = tbGoodsMapper.selectById(goodsId);
|
|
|
+ if(Objects.isNull(tbGoods)){
|
|
|
+ throw new RuntimeException("该商品不存在");
|
|
|
+ }
|
|
|
+ int i =tbGoodsTypeMapper.delGoodsFromGoodsType(goodsTypeId,goodsId);
|
|
|
+ line = i+1;
|
|
|
}
|
|
|
- //建立输出流,输出文件
|
|
|
- FileOutputStream fos = new FileOutputStream(filepath);
|
|
|
-
|
|
|
- workbook.write(fos);
|
|
|
- fos.flush();
|
|
|
- //关闭输出流
|
|
|
- fos.close();
|
|
|
- workbook.close();
|
|
|
- System.out.println("\n数据导出完成!共导出 " + count + " 条数据。");
|
|
|
- return "数据导出完成!共导出 " + count + " 条数据。";
|
|
|
+ return line;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
}
|