|
| 1 | +package com.example.demo.service.impl; |
| 2 | + |
| 3 | +import com.example.demo.dao.AreaDao; |
| 4 | +import com.example.demo.entity.Area; |
| 5 | +import com.example.demo.service.AreaService; |
| 6 | +import org.springframework.stereotype.Service; |
| 7 | +import org.springframework.transaction.annotation.Transactional; |
| 8 | + |
| 9 | +import java.util.Date; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +@Service |
| 13 | +public class AreaServiceImpl implements AreaService { |
| 14 | + |
| 15 | + private final AreaDao areaDao; |
| 16 | + |
| 17 | + public AreaServiceImpl(AreaDao areaDao) { |
| 18 | + this.areaDao = areaDao; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public List<Area> queryArea() { |
| 23 | + return areaDao.queryArea(); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public Area queryAreaById(int areaId) { |
| 28 | + return areaDao.queryAreaById(areaId); |
| 29 | + } |
| 30 | + |
| 31 | + @Transactional |
| 32 | + @Override |
| 33 | + public boolean insertArea(Area area) { |
| 34 | + if (area.getAreaName() != null && "".equals(area.getAreaName())){ |
| 35 | + area.setCreateTime(new Date()); |
| 36 | + area.setLastEditTime(new Date()); |
| 37 | + try { |
| 38 | + int effectedNumber = areaDao.insertArea(area); |
| 39 | + if (effectedNumber > 0) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + throw new RuntimeException("插入失败"); |
| 43 | + } catch (Exception e) { |
| 44 | + throw new RuntimeException("插入失败:" + e.getMessage()); |
| 45 | + } |
| 46 | + } |
| 47 | + throw new RuntimeException("插入失败:信息为空" ); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean updateArea(Area area) { |
| 52 | + if (area.getAreaId() != null && area.getAreaId() > 0){ |
| 53 | + area.setLastEditTime(new Date()); |
| 54 | + try { |
| 55 | + int effectedNumber = areaDao.updateArea(area); |
| 56 | + if (effectedNumber > 0) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + throw new RuntimeException("更新失败"); |
| 60 | + } catch (Exception e) { |
| 61 | + throw new RuntimeException("更新失败:" + e.getMessage()); |
| 62 | + } |
| 63 | + } |
| 64 | + throw new RuntimeException("更新失败:信息为空" ); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean deleteArea(int areaId) { |
| 69 | + if (areaId >0 ){ |
| 70 | + try { |
| 71 | + int effectedNumber = areaDao.deleteArea(areaId); |
| 72 | + if (effectedNumber > 0) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + throw new RuntimeException("删除失败"); |
| 76 | + } catch (Exception e) { |
| 77 | + throw new RuntimeException("删除失败:" + e.getMessage()); |
| 78 | + } |
| 79 | + } |
| 80 | + throw new RuntimeException("删除失败:信息为空" ); |
| 81 | + } |
| 82 | +} |
0 commit comments