1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| package com.sky.service.impl;
import com.baomidou.mybatisplus.core.batch.MybatisBatch; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.sky.constant.MessageConstant; import com.sky.dto.SetmealDTO; import com.sky.dto.SetmealPageQueryDTO; import com.sky.entity.Setmeal; import com.sky.entity.SetmealDish; import com.sky.mapper.SetmealDishMapper; import com.sky.mapper.SetmealMapper; import com.sky.result.PageResult; import com.sky.service.SetmealService; import com.sky.vo.SetmealVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service @Slf4j public class SetmealServiceImpl implements SetmealService {
@Autowired private SetmealMapper setmealMapper;
@Autowired private SetmealDishMapper setmealDishMapper;
@Autowired private SqlSessionFactory sqlSessionFactory;
@Override @Transactional public void saveWithDishes(SetmealDTO setmealDTO) { Setmeal setmeal = new Setmeal(); BeanUtils.copyProperties(setmealDTO, setmeal); setmealMapper.insert(setmeal); setmealMapper.selectById(setmeal.getId()); Long setmealId = setmeal.getId(); List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes(); if (setmealDishes != null && !setmealDishes.isEmpty()) { setmealDishes.forEach(setmealDish -> { setmealDish.setSetmealId(setmealId); }); MybatisBatch<SetmealDish> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, setmealDishes); MybatisBatch.Method<SetmealDish> method = new MybatisBatch.Method<>(SetmealDishMapper.class); mybatisBatch.execute(method.insert()); } }
@Override @Transactional public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) { Page<SetmealVO> page = new Page<>(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize()); QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>(); queryWrapper.isNotNull("s.name"); if (StringUtils.isNotBlank(setmealPageQueryDTO.getName())) { queryWrapper.like("s.name", setmealPageQueryDTO.getName()); } if (setmealPageQueryDTO.getCategoryId() != null) { queryWrapper.eq("s.category_id", setmealPageQueryDTO.getCategoryId()); } if (setmealPageQueryDTO.getStatus() != null) { queryWrapper.eq("s.status", setmealPageQueryDTO.getStatus()); } queryWrapper.orderByDesc("s.create_time"); System.out.println(queryWrapper.getCustomSqlSegment());
Page<SetmealVO> setmealVOPage = setmealMapper.selectPage(page, queryWrapper); long total = setmealVOPage.getTotal(); List<SetmealVO> setmealVOList = setmealVOPage.getRecords(); return new PageResult(total, setmealVOList); }
@Override @Transactional public void deleteBatch(List<Long> ids) { List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids); for (Setmeal setmeal : setmeals) { if (setmeal.getStatus() == 1) { throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE); } } setmealMapper.deleteBatchIds(ids); LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids); setmealDishMapper.delete(lambdaQueryWrapper); }
@Override @Transactional public SetmealVO getByIdWithDish(Long id) { Setmeal setmeal = setmealMapper.selectById(id); LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id); List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper); SetmealVO setmealVO = new SetmealVO(); BeanUtils.copyProperties(setmeal, setmealVO); setmealVO.setSetmealDishes(setmealDishes); return setmealVO; }
@Override @Transactional public void updateWithDishes(SetmealDTO setmealDTO) { Setmeal setmeal = new Setmeal(); BeanUtils.copyProperties(setmealDTO, setmeal); setmealMapper.updateById(setmeal); LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId()); setmealDishMapper.delete(lambdaQueryWrapper); List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes(); if (setmealDishes != null && !setmealDishes.isEmpty()) { setmealDishes.forEach(setmealDish -> { setmealDish.setSetmealId(setmeal.getId()); }); MybatisBatch<SetmealDish> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, setmealDishes); MybatisBatch.Method<SetmealDish> method = new MybatisBatch.Method<>(SetmealDishMapper.class); mybatisBatch.execute(method.insert()); } }
@Override @Transactional public void updateStatus(Integer status, Long id) { if (status == 1) { LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id); List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper); if (setmealDishes.isEmpty()) { throw new RuntimeException(MessageConstant.SETMEAL_ENABLE_FAILED); } } Setmeal setmeal = setmealMapper.selectById(id); if (setmeal == null) { throw new RuntimeException(MessageConstant.SETMEAL_NOT_FOUND); } setmeal.setStatus(status); setmealMapper.updateById(setmeal);
} }
|