商品模块代码提交

This commit is contained in:
Tankaikai
2025-02-16 12:06:02 +08:00
parent 2244fee745
commit 9d86c376ec
9 changed files with 191 additions and 161 deletions

View File

@@ -1,6 +1,6 @@
package com.czg.service.product.mapper;
import com.czg.product.entity.ShopProductSpec;
import com.czg.product.entity.ShopProdSpec;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@@ -11,6 +11,6 @@ import org.apache.ibatis.annotations.Mapper;
* @since 1.0 2025-02-13
*/
@Mapper
public interface ShopProductSpecMapper extends BaseMapper<ShopProductSpec> {
public interface ShopProductSpecMapper extends BaseMapper<ShopProdSpec> {
}

View File

@@ -5,10 +5,9 @@ import cn.hutool.core.util.StrUtil;
import com.czg.constant.GlobalConstant;
import com.czg.enums.StatusEnum;
import com.czg.exception.CzgException;
import com.czg.product.dto.ShopProductSpecDTO;
import com.czg.product.entity.ShopProdCategory;
import com.czg.product.entity.ShopProductSpec;
import com.czg.product.service.ShopProductSpecService;
import com.czg.product.dto.ShopProdSpecDTO;
import com.czg.product.entity.ShopProdSpec;
import com.czg.product.service.ShopProdSpecService;
import com.czg.sa.StpKit;
import com.czg.service.product.mapper.ShopProductSpecMapper;
import com.czg.utils.PageUtil;
@@ -18,9 +17,12 @@ import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static com.czg.product.entity.table.ShopProdSpecTableDef.SHOP_PROD_SPEC;
/**
* 商品规格
@@ -29,117 +31,126 @@ import java.util.List;
* @since 1.0 2025-02-13
*/
@Service
public class ShopProductSpecServiceImpl extends ServiceImpl<ShopProductSpecMapper, ShopProductSpec> implements ShopProductSpecService {
public class ShopProductSpecServiceImpl extends ServiceImpl<ShopProductSpecMapper, ShopProdSpec> implements ShopProdSpecService {
private QueryWrapper buildQueryWrapper(ShopProductSpecDTO param) {
private QueryWrapper buildQueryWrapper(ShopProdSpecDTO param) {
QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
if (StrUtil.isNotEmpty(param.getName())) {
queryWrapper.like(ShopProductSpec::getName, param.getName());
queryWrapper.like(ShopProdSpec::getName, param.getName());
}
Long shopId = StpKit.USER.getLoginIdAsLong();
queryWrapper.eq(ShopProductSpec::getShopId, shopId);
queryWrapper.orderBy(ShopProductSpec::getId, false);
queryWrapper.eq(ShopProdSpec::getShopId, shopId);
queryWrapper.orderBy(ShopProdSpec::getSort, true);
queryWrapper.orderBy(ShopProdSpec::getId, false);
return queryWrapper;
}
@Override
public Page<ShopProductSpecDTO> page(ShopProductSpecDTO param) {
public Page<ShopProdSpecDTO> getShopProdSpecPage(ShopProdSpecDTO param) {
QueryWrapper queryWrapper = buildQueryWrapper(param);
return super.pageAs(PageUtil.buildPage(), queryWrapper, ShopProductSpecDTO.class);
return super.pageAs(PageUtil.buildPage(), queryWrapper, ShopProdSpecDTO.class);
}
@Override
public List<ShopProductSpecDTO> list(ShopProductSpecDTO param) {
public List<ShopProdSpecDTO> getShopProdSpecList(ShopProdSpecDTO param) {
QueryWrapper queryWrapper = buildQueryWrapper(param);
queryWrapper.eq(ShopProductSpec::getStatus, StatusEnum.ENABLED.value());
List<ShopProductSpecDTO> list = super.listAs(queryWrapper, ShopProductSpecDTO.class);
queryWrapper.eq(ShopProdSpec::getStatus, StatusEnum.ENABLED.value());
List<ShopProdSpecDTO> list = super.listAs(queryWrapper, ShopProdSpecDTO.class);
return TreeUtils.build(list, GlobalConstant.TREE_ROOT);
}
@Override
public ShopProductSpecDTO get(Long id) {
public ShopProdSpecDTO getShopProdSpecById(Long id) {
Long shopId = StpKit.USER.getLoginIdAsLong();
return super.getOneAs(query().eq(ShopProductSpec::getId, id).eq(ShopProductSpec::getShopId, shopId), ShopProductSpecDTO.class);
return super.getOneAs(query().eq(ShopProdSpec::getId, id).eq(ShopProdSpec::getShopId, shopId), ShopProdSpecDTO.class);
}
@Override
public boolean save(ShopProductSpecDTO dto) {
public boolean addShopProdSpec(ShopProdSpecDTO dto) {
Long shopId = StpKit.USER.getLoginIdAsLong();
boolean exists = super.exists(query()
.eq(ShopProductSpec::getName, dto.getName())
.eq(ShopProductSpec::getLevel, dto.getLevel())
.eq(ShopProductSpec::getShopId, shopId));
.eq(ShopProdSpec::getName, dto.getName())
.eq(ShopProdSpec::getPid, dto.getPid())
.eq(ShopProdSpec::getShopId, shopId));
if (exists) {
throw new CzgException("商品规格已存在");
}
ShopProductSpec entity = BeanUtil.copyProperties(dto, ShopProductSpec.class);
ShopProdSpec entity = BeanUtil.copyProperties(dto, ShopProdSpec.class);
if (GlobalConstant.TREE_ROOT.equals(entity.getPid())) {
entity.setFullName(entity.getName());
entity.setPids(GlobalConstant.TREE_ROOT.toString());
} else {
ShopProdSpec parent = super.getById(entity.getPid());
entity.setFullName(parent.getFullName() + " - " + entity.getName());
String pids = parent.getPids();
entity.setPids(pids + "," + entity.getPid());
}
entity.setStatus(StatusEnum.ENABLED.value());
entity.setShopId(shopId);
return super.save(entity);
}
@Override
public boolean update(ShopProductSpecDTO dto) {
@Transactional(rollbackFor = Exception.class)
public boolean updateShopProdSpec(ShopProdSpecDTO dto) {
//上级规格不能为自身
if (dto.getId().equals(dto.getPid())) {
throw new CzgException("上级规格不能为自身");
}
Long shopId = StpKit.USER.getLoginIdAsLong();
dto.setShopId(shopId);
boolean exists = super.exists(query()
.eq(ShopProductSpec::getName, dto.getName())
.eq(ShopProductSpec::getLevel, dto.getLevel())
.eq(ShopProductSpec::getShopId, shopId)
.ne(ShopProductSpec::getId, dto.getId()));
.eq(ShopProdSpec::getName, dto.getName())
.eq(ShopProdSpec::getPid, dto.getPid())
.eq(ShopProdSpec::getShopId, shopId)
.ne(ShopProdSpec::getId, dto.getId()));
if (exists) {
throw new CzgException("商品规格已存在");
}
ShopProductSpec entity = BeanUtil.copyProperties(dto, ShopProductSpec.class);
ShopProdSpec old = super.getById(dto.getId());
ShopProdSpec entity = BeanUtil.copyProperties(dto, ShopProdSpec.class);
if (GlobalConstant.TREE_ROOT.equals(entity.getPid())) {
entity.setFullName(entity.getName());
entity.setPids(GlobalConstant.TREE_ROOT.toString());
} else {
ShopProdSpec parent = super.getById(entity.getPid());
entity.setFullName(parent.getFullName() + " - " + entity.getName());
String pids = parent.getPids();
entity.setPids(pids + "," + entity.getPid());
}
String oldName = old.getName();
UpdateChain.of(ShopProdSpec.class)
.setRaw(ShopProdSpec::getFullName, StrUtil.format("replace(full_name, '{}', '{}')", oldName, entity.getName()))
.eq(ShopProdSpec::getShopId, entity.getShopId()).update();
return super.updateById(entity);
}
@Override
public boolean delete(Long id) {
public boolean deleteShopProdSpec(Long id) {
Long shopId = StpKit.USER.getLoginIdAsLong();
return super.remove(
query().eq(ShopProdCategory::getShopId, shopId)
.and(wrapper -> {
wrapper.eq(ShopProductSpec::getId, id)
.or(wrapper1 -> {
wrapper1.eq(ShopProductSpec::getPid, id);
});
})
return super.remove(query()
.where(SHOP_PROD_SPEC.SHOP_ID.eq(shopId)
.and(SHOP_PROD_SPEC.ID.eq(id).or(SHOP_PROD_SPEC.PIDS.like("," + id))))
);
}
@Override
public boolean disable(Long id) {
public boolean disableShopProdSpec(Long id) {
Long shopId = StpKit.USER.getLoginIdAsLong();
return UpdateChain.of(ShopProductSpec.class)
.set(ShopProductSpec::getStatus, StatusEnum.DISABLE.value())
.where(ShopProductSpec::getShopId).eq(shopId)
.and(wrapper -> {
wrapper.eq(ShopProductSpec::getId, id)
.or(wrapper1 -> {
wrapper1.eq(ShopProductSpec::getPid, id);
});
})
.update();
return UpdateChain.of(ShopProdSpec.class)
.set(SHOP_PROD_SPEC.STATUS, StatusEnum.DISABLE.value())
.where(SHOP_PROD_SPEC.SHOP_ID.eq(shopId)
.and(SHOP_PROD_SPEC.ID.eq(id).or(SHOP_PROD_SPEC.PIDS.like("," + id)))).update();
}
@Override
public boolean enable(Long id) {
public boolean enableShopProdSpec(Long id) {
Long shopId = StpKit.USER.getLoginIdAsLong();
return UpdateChain.of(ShopProductSpec.class)
.set(ShopProductSpec::getStatus, StatusEnum.ENABLED.value())
.where(ShopProductSpec::getShopId).eq(shopId)
.and(wrapper -> {
wrapper.eq(ShopProductSpec::getId, id)
.or(wrapper1 -> {
wrapper1.eq(ShopProductSpec::getPid, id);
});
})
.update();
return UpdateChain.of(ShopProdSpec.class)
.set(SHOP_PROD_SPEC.STATUS, StatusEnum.ENABLED.value())
.where(SHOP_PROD_SPEC.SHOP_ID.eq(shopId)
.and(SHOP_PROD_SPEC.ID.eq(id).or(SHOP_PROD_SPEC.PIDS.like("," + id)))).update();
}
}