TbCarService.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.pj.project.tb_car;
  2. import java.io.InputStream;
  3. import java.util.List;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.log.StaticLog;
  6. import com.alibaba.excel.EasyExcel;
  7. import com.alibaba.excel.ExcelReader;
  8. import com.alibaba.excel.read.metadata.ReadSheet;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import com.baomidou.mybatisplus.extension.service.IService;
  11. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  12. import com.pj.project4sp.global.BusinessException;
  13. import com.pj.utils.so.SoMap;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.annotation.Resource;
  19. /**
  20. * Service: tb_car --
  21. *
  22. * @author qzy
  23. */
  24. @Service
  25. @Transactional
  26. public class TbCarService extends ServiceImpl<TbCarMapper, TbCar> implements IService<TbCar> {
  27. /**
  28. * 底层 Mapper 对象
  29. */
  30. @Autowired
  31. TbCarMapper tbCarMapper;
  32. @Resource
  33. private ImportCarListener importCarListener;
  34. /**
  35. * 增
  36. */
  37. void add(TbCar t) {
  38. String cardNo=t.getCardNo().trim();
  39. TbCar db = this.findByCardNo(cardNo);
  40. if (db!=null){
  41. throw new BusinessException("车辆已存在");
  42. }
  43. if (StrUtil.isEmpty(t.getCustomerId())) {
  44. throw new BusinessException("请选择车辆所属");
  45. }
  46. t.setCardNo(cardNo);
  47. super.save(t);
  48. }
  49. /**
  50. * 删
  51. */
  52. void delete(Long id) {
  53. tbCarMapper.deleteById(id);
  54. }
  55. /**
  56. * 改
  57. */
  58. void update(TbCar t) {
  59. String cardNo=t.getCardNo().trim();
  60. TbCar db = this.findByCardNo(cardNo);
  61. if (db!=null&&!StrUtil.equals(t.getId(),db.getId())) {
  62. throw new BusinessException("车辆已存在");
  63. }
  64. t.setCardNo(cardNo);
  65. super.updateById(t);
  66. }
  67. /**
  68. * 查
  69. */
  70. TbCar getById(Long id) {
  71. return super.getById(id);
  72. }
  73. /**
  74. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  75. */
  76. List<TbCar> getList(SoMap so) {
  77. return tbCarMapper.getList(so);
  78. }
  79. public TbCar findByCardNo(String cardNo) {
  80. QueryWrapper<TbCar> ew = new QueryWrapper<>();
  81. ew.eq("card_no", cardNo);
  82. return getOne(ew);
  83. }
  84. public void importExcel(MultipartFile file) throws Exception {
  85. StaticLog.info("file size:{}", file.getSize());
  86. InputStream is = file.getInputStream();
  87. importCarListener.list.clear();
  88. ExcelReader reader = EasyExcel.read(is, ImportCarBO.class, importCarListener).build();
  89. ReadSheet readSheet = EasyExcel.readSheet(0).build();
  90. reader.read(readSheet);
  91. reader.finish();
  92. is.close();
  93. }
  94. }