| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.service.business.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.doumee.core.constants.Constants; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.model.LoginUserInfo; |
| | | import com.doumee.core.model.PageData; |
| | | import com.doumee.core.model.PageWrap; |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.dao.business.AddrMapper; |
| | | import com.doumee.dao.business.model.Addr; |
| | | import com.doumee.service.business.AddrService; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * å°åç°¿Serviceå®ç° |
| | | * @author rk |
| | | * @date 2026/04/15 |
| | | */ |
| | | @Service |
| | | public class AddrServiceImpl implements AddrService { |
| | | |
| | | @Autowired |
| | | private AddrMapper addrMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class, BusinessException.class}) |
| | | public Integer create(Addr addr) { |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); |
| | | addr.setDeleted(Constants.ZERO); |
| | | addr.setCreateTime(new Date()); |
| | | addr.setCreateUser(loginUserInfo.getId()); |
| | | addr.setUpdateTime(new Date()); |
| | | addr.setUpdateUser(loginUserInfo.getId()); |
| | | addrMapper.insert(addr); |
| | | return addr.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteById(Integer id) { |
| | | addrMapper.update(new UpdateWrapper<Addr>().lambda() |
| | | .set(Addr::getDeleted, Constants.ONE) |
| | | .eq(Addr::getId, id)); |
| | | } |
| | | |
| | | @Override |
| | | public void delete(Addr addr) { |
| | | UpdateWrapper<Addr> deleteWrapper = new UpdateWrapper<>(addr); |
| | | addrMapper.delete(deleteWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteByIdInBatch(List<Integer> ids) { |
| | | if (CollectionUtils.isEmpty(ids)) { |
| | | return; |
| | | } |
| | | for (Integer id : ids) { |
| | | this.deleteById(id); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class, BusinessException.class}) |
| | | public void updateById(Addr addr) { |
| | | if (Objects.isNull(addr) || Objects.isNull(addr.getId())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); |
| | | addr.setUpdateTime(new Date()); |
| | | addr.setUpdateUser(loginUserInfo.getId()); |
| | | addrMapper.updateById(addr); |
| | | } |
| | | |
| | | @Override |
| | | public void updateStatus(Addr addr) { |
| | | if (Objects.isNull(addr) || Objects.isNull(addr.getId())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); |
| | | addr.setUpdateTime(new Date()); |
| | | addr.setUpdateUser(loginUserInfo.getId()); |
| | | addrMapper.updateById(addr); |
| | | } |
| | | |
| | | @Override |
| | | public void updateByIdInBatch(List<Addr> addrs) { |
| | | if (CollectionUtils.isEmpty(addrs)) { |
| | | return; |
| | | } |
| | | for (Addr addr : addrs) { |
| | | this.updateById(addr); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Addr findById(Integer id) { |
| | | Addr addr = addrMapper.selectById(id); |
| | | if (Objects.isNull(addr)) { |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | return addr; |
| | | } |
| | | |
| | | @Override |
| | | public Addr findOne(Addr addr) { |
| | | QueryWrapper<Addr> wrapper = new QueryWrapper<>(addr); |
| | | return addrMapper.selectOne(wrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<Addr> findList(Addr addr) { |
| | | QueryWrapper<Addr> wrapper = new QueryWrapper<>(addr); |
| | | return addrMapper.selectList(wrapper); |
| | | } |
| | | |
| | | @Override |
| | | public PageData<Addr> findPage(PageWrap<Addr> pageWrap) { |
| | | IPage<Addr> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); |
| | | QueryWrapper<Addr> queryWrapper = new QueryWrapper<>(); |
| | | Utils.MP.blankToNull(pageWrap.getModel()); |
| | | pageWrap.getModel().setDeleted(Constants.ZERO); |
| | | if (pageWrap.getModel().getDeleted() != null) { |
| | | queryWrapper.lambda().eq(Addr::getDeleted, pageWrap.getModel().getDeleted()); |
| | | } |
| | | if (pageWrap.getModel().getMemberId() != null) { |
| | | queryWrapper.lambda().eq(Addr::getMemberId, pageWrap.getModel().getMemberId()); |
| | | } |
| | | if (pageWrap.getModel().getIsDefault() != null) { |
| | | queryWrapper.lambda().eq(Addr::getIsDefault, pageWrap.getModel().getIsDefault()); |
| | | } |
| | | if (StringUtils.isNotBlank(pageWrap.getModel().getName())) { |
| | | queryWrapper.lambda().like(Addr::getName, pageWrap.getModel().getName()); |
| | | } |
| | | if (StringUtils.isNotBlank(pageWrap.getModel().getPhone())) { |
| | | queryWrapper.lambda().like(Addr::getPhone, pageWrap.getModel().getPhone()); |
| | | } |
| | | if (StringUtils.isNotBlank(pageWrap.getModel().getAddr())) { |
| | | queryWrapper.lambda().like(Addr::getAddr, pageWrap.getModel().getAddr()); |
| | | } |
| | | if (pageWrap.getModel().getAreaId() != null) { |
| | | queryWrapper.lambda().eq(Addr::getAreaId, pageWrap.getModel().getAreaId()); |
| | | } |
| | | for (PageWrap.SortData sortData : pageWrap.getSorts()) { |
| | | if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { |
| | | queryWrapper.orderByDesc(sortData.getProperty()); |
| | | } else { |
| | | queryWrapper.orderByAsc(sortData.getProperty()); |
| | | } |
| | | } |
| | | return PageData.from(addrMapper.selectPage(page, queryWrapper)); |
| | | } |
| | | |
| | | @Override |
| | | public long count(Addr addr) { |
| | | QueryWrapper<Addr> wrapper = new QueryWrapper<>(addr); |
| | | return addrMapper.selectCount(wrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<Addr> findListWithArea(Integer memberId) { |
| | | MPJLambdaWrapper<Addr> wrapper = new MPJLambdaWrapper<Addr>() |
| | | .selectAll(Addr.class) |
| | | .select("a3.name", Addr::getDistrictName) |
| | | .select("a3.code", Addr::getDistrictCode) |
| | | .select("a2.id", Addr::getCityId) |
| | | .select("a2.name", Addr::getCityName) |
| | | .select("a2.code", Addr::getCityCode) |
| | | .select("a1.id", Addr::getProvinceId) |
| | | .select("a1.name", Addr::getProvinceName) |
| | | .select("a1.code", Addr::getProvinceCode) |
| | | .leftJoin("areas a3 on a3.id = t.AREA_ID") |
| | | .leftJoin("areas a2 on a2.id = a3.PARENT_ID") |
| | | .leftJoin("areas a1 on a1.id = a2.PARENT_ID") |
| | | .eq(Addr::getDeleted, Constants.ZERO) |
| | | .eq(Addr::getMemberId, memberId) |
| | | .orderByDesc(Addr::getIsDefault) |
| | | .orderByDesc(Addr::getCreateTime); |
| | | return addrMapper.selectJoinList(Addr.class, wrapper); |
| | | } |
| | | |
| | | @Override |
| | | public Addr findByIdWithArea(Integer id) { |
| | | MPJLambdaWrapper<Addr> wrapper = new MPJLambdaWrapper<Addr>() |
| | | .selectAll(Addr.class) |
| | | .select("a3.name", Addr::getDistrictName) |
| | | .select("a3.code", Addr::getDistrictCode) |
| | | .select("a2.id", Addr::getCityId) |
| | | .select("a2.name", Addr::getCityName) |
| | | .select("a2.code", Addr::getCityCode) |
| | | .select("a1.id", Addr::getProvinceId) |
| | | .select("a1.name", Addr::getProvinceName) |
| | | .select("a1.code", Addr::getProvinceCode) |
| | | .leftJoin("areas a3 on a3.id = t.AREA_ID") |
| | | .leftJoin("areas a2 on a2.id = a3.PARENT_ID") |
| | | .leftJoin("areas a1 on a1.id = a2.PARENT_ID") |
| | | .eq(Addr::getId, id); |
| | | Addr addr = addrMapper.selectJoinOne(Addr.class, wrapper); |
| | | if (Objects.isNull(addr)) { |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | return addr; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class, BusinessException.class}) |
| | | public Integer createByMember(Addr addr, Integer memberId) { |
| | | validateAddr(addr); |
| | | // 设为é»è®¤æ¶ï¼å
æ¸
é¤è¯¥ç¨æ·åæçé»è®¤å°å |
| | | if (Constants.equalsInteger(addr.getIsDefault(), Constants.ONE)) { |
| | | clearDefault(memberId); |
| | | } |
| | | addr.setMemberId(memberId); |
| | | addr.setDeleted(Constants.ZERO); |
| | | addr.setCreateTime(new Date()); |
| | | addr.setCreateUser(memberId); |
| | | addr.setUpdateTime(new Date()); |
| | | addr.setUpdateUser(memberId); |
| | | addrMapper.insert(addr); |
| | | return addr.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class, BusinessException.class}) |
| | | public void updateByMember(Addr addr, Integer memberId) { |
| | | if (Objects.isNull(addr) || Objects.isNull(addr.getId())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | validateAddr(addr); |
| | | // 设为é»è®¤æ¶ï¼å
æ¸
é¤è¯¥ç¨æ·åæçé»è®¤å°å |
| | | if (Constants.equalsInteger(addr.getIsDefault(), Constants.ONE)) { |
| | | clearDefault(memberId); |
| | | } |
| | | addr.setMemberId(memberId); |
| | | addr.setUpdateTime(new Date()); |
| | | addr.setUpdateUser(memberId); |
| | | addrMapper.updateById(addr); |
| | | } |
| | | |
| | | private void validateAddr(Addr addr) { |
| | | if (Objects.isNull(addr.getAreaId())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "åºåä¸è½ä¸ºç©º"); |
| | | } |
| | | if (StringUtils.isBlank(addr.getAddr())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "详ç»å°åä¸è½ä¸ºç©º"); |
| | | } |
| | | if (Objects.isNull(addr.getIsDefault())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "æ¯å¦é»è®¤ä¸è½ä¸ºç©º"); |
| | | } |
| | | if (StringUtils.isBlank(addr.getName())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "å§åä¸è½ä¸ºç©º"); |
| | | } |
| | | if (StringUtils.isBlank(addr.getPhone())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "çµè¯ä¸è½ä¸ºç©º"); |
| | | } |
| | | if (Objects.isNull(addr.getLongitude())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "ç»åº¦ä¸è½ä¸ºç©º"); |
| | | } |
| | | if (Objects.isNull(addr.getLatitude())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "纬度ä¸è½ä¸ºç©º"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¸
é¤è¯¥ç¨æ·ææå°åçé»è®¤æ è®° |
| | | */ |
| | | private void clearDefault(Integer memberId) { |
| | | addrMapper.update(new UpdateWrapper<Addr>().lambda() |
| | | .set(Addr::getIsDefault, Constants.ZERO) |
| | | .eq(Addr::getMemberId, memberId) |
| | | .eq(Addr::getDeleted, Constants.ZERO) |
| | | .eq(Addr::getIsDefault, Constants.ONE)); |
| | | } |
| | | } |