商品模块代码提交

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,8 +1,8 @@
package com.czg.controller;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ShopProductSpecDTO;
import com.czg.product.service.ShopProductSpecService;
import com.czg.product.dto.ShopProdSpecDTO;
import com.czg.product.service.ShopProdSpecService;
import com.czg.resp.CzgResult;
import com.czg.utils.AssertUtil;
import com.czg.validator.ValidatorUtil;
@ -16,85 +16,85 @@ import java.util.List;
/**
* 商品规格
*
* @author Tankaikai tankaikai@aliyun.com
* @since 1.0 2025-02-13
*/
* 商品规格
*
* @author Tankaikai tankaikai@aliyun.com
* @since 1.0 2025-02-13
*/
@AllArgsConstructor
@RestController
@RequestMapping("/admin/prod/spec")
public class ShopProductSpecController {
private final ShopProductSpecService shopProductSpecService;
public class ShopProdSpecController {
private final ShopProdSpecService shopProdSpecService;
@GetMapping("list")
@OperationLog("商品规格-列表")
//@SaAdminCheckPermission("product:spec:list")
public CzgResult<List<ShopProductSpecDTO>> list(ShopProductSpecDTO param){
List<ShopProductSpecDTO> data = shopProductSpecService.list(param);
//@SaAdminCheckPermission("shopProductSpec:list")
public CzgResult<List<ShopProdSpecDTO>> list(ShopProdSpecDTO param) {
List<ShopProdSpecDTO> data = shopProdSpecService.getShopProdSpecList(param);
return CzgResult.success(data);
}
@GetMapping("{id}")
@OperationLog("商品规格-详情")
//@SaAdminCheckPermission("product:spec:info")
public CzgResult<ShopProductSpecDTO> get(@PathVariable("id") Long id){
//@SaAdminCheckPermission("shopProductSpec:info")
public CzgResult<ShopProdSpecDTO> get(@PathVariable("id") Long id) {
AssertUtil.isNull(id, "{}不能为空", "id");
ShopProductSpecDTO data = shopProductSpecService.get(id);
ShopProdSpecDTO data = shopProdSpecService.getShopProdSpecById(id);
return CzgResult.success(data);
}
@PostMapping
@OperationLog("商品规格-新增")
//@SaAdminCheckPermission("product:spec:save")
public CzgResult<Void> save(@RequestBody ShopProductSpecDTO dto){
//@SaAdminCheckPermission("shopProductSpec:save")
public CzgResult<Void> save(@RequestBody ShopProdSpecDTO dto) {
//效验数据
ValidatorUtil.validateEntity(dto, InsertGroup.class, DefaultGroup.class);
shopProductSpecService.save(dto);
shopProdSpecService.addShopProdSpec(dto);
return CzgResult.success();
}
@PutMapping
@OperationLog("商品规格-修改")
//@SaAdminCheckPermission("product:spec:update")
public CzgResult<Void> update(@RequestBody ShopProductSpecDTO dto){
//@SaAdminCheckPermission("shopProductSpec:update")
public CzgResult<Void> update(@RequestBody ShopProdSpecDTO dto) {
//效验数据
ValidatorUtil.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
shopProductSpecService.update(dto);
shopProdSpecService.updateShopProdSpec(dto);
return CzgResult.success();
}
@DeleteMapping("{id}")
@OperationLog("商品规格-删除")
//@SaAdminCheckPermission("product:spec:delete")
public CzgResult<Void> delete(@PathVariable("id") Long id){
//@SaAdminCheckPermission("shopProductSpec:delete")
public CzgResult<Void> delete(@PathVariable("id") Long id) {
//效验数据
AssertUtil.isNull(id, "{}不能为空", "id");
shopProductSpecService.delete(id);
shopProdSpecService.deleteShopProdSpec(id);
return CzgResult.success();
}
@PostMapping("disable/{id}")
@OperationLog("商品规格-禁用")
//@SaAdminCheckPermission("product:spec:able")
//@SaAdminCheckPermission("shopProductSpec:able")
public CzgResult<Void> disable(@PathVariable("id") Long id) {
//效验数据
AssertUtil.isNull(id, "{}不能为空", "id");
shopProductSpecService.disable(id);
shopProdSpecService.disableShopProdSpec(id);
return CzgResult.success();
}
@PostMapping("enable/{id}")
@OperationLog("商品规格-启用")
//@SaAdminCheckPermission("product:spec:able")
//@SaAdminCheckPermission("shopProductSpec:able")
public CzgResult<Void> enable(@PathVariable("id") Long id) {
//效验数据
AssertUtil.isNull(id, "{}不能为空", "id");
shopProductSpecService.enable(id);
shopProdSpecService.enableShopProdSpec(id);
return CzgResult.success();
}
}

View File

@ -21,7 +21,7 @@ import java.time.LocalDateTime;
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class ShopProductSpecDTO extends TreeNode<ShopProductSpecDTO> implements Serializable {
public class ShopProdSpecDTO extends TreeNode<ShopProdSpecDTO> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@ -37,6 +37,10 @@ public class ShopProductSpecDTO extends TreeNode<ShopProductSpecDTO> implements
*/
@NotBlank(message = "规格名称不能为空", groups = DefaultGroup.class)
private String name;
/**
* 完整规格名称
*/
private String fullName;
/**
* 规格级别
*/
@ -55,6 +59,10 @@ public class ShopProductSpecDTO extends TreeNode<ShopProductSpecDTO> implements
*/
@NotNull(message = "请选择上级规格", groups = DefaultGroup.class)
private Long pid;
/**
* 所有上级id用逗号分隔
*/
private String pids;
/**
* 店铺 id
*/

View File

@ -73,6 +73,6 @@ public class ShopProdCategory implements Serializable {
/**
* 更新时间
*/
@Column(onUpdateValue = "now()")
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@ -1,5 +1,6 @@
package com.czg.product.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
@ -17,8 +18,8 @@ import java.time.LocalDateTime;
* @since 1.0 2025-02-13
*/
@Data
@Table("tb_shop_product_spec")
public class ShopProductSpec implements Serializable {
@Table("tb_shop_prod_spec")
public class ShopProdSpec implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@ -32,6 +33,10 @@ public class ShopProductSpec implements Serializable {
* 规格名称
*/
private String name;
/**
* 完整规格名称
*/
private String fullName;
/**
* 规格级别
*/
@ -44,6 +49,10 @@ public class ShopProductSpec implements Serializable {
* 上级 id
*/
private Long pid;
/**
* 所有上级id用逗号分隔
*/
private String pids;
/**
* 店铺 id
*/
@ -55,9 +64,11 @@ public class ShopProductSpec implements Serializable {
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@ -22,41 +22,41 @@ import java.time.LocalDateTime;
public class ShopProdUnit implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
/**
* id
*/
@Id(keyType = KeyType.Generator, value = KeyGenerators.snowFlakeId)
private Long id;
/**
* 单位名称
*/
private String name;
/**
* 单位类型 number-计数 weight-记重
*/
private String unitType;
/**
* 单位来源 1-系统预设 0-商家创建
*/
private Integer isSystem;
/**
* 状态 0-禁用 1-启用
*/
private Integer status;
/**
* 店铺id
*/
private Long shopId;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新时间
*/
@Column(onUpdateValue = "now()")
private LocalDateTime updateTime;
/**
* id
*/
@Id(keyType = KeyType.Generator, value = KeyGenerators.snowFlakeId)
private Long id;
/**
* 单位名称
*/
private String name;
/**
* 单位类型 number-计数 weight-记重
*/
private String unitType;
/**
* 单位来源 1-系统预设 0-商家创建
*/
private Integer isSystem;
/**
* 状态 0-禁用 1-启用
*/
private Integer status;
/**
* 店铺id
*/
private Long shopId;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@ -0,0 +1,33 @@
package com.czg.product.service;
import com.czg.product.dto.ShopProdSpecDTO;
import com.czg.product.entity.ShopProdSpec;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.service.IService;
import java.util.List;
/**
* 商品规格
*
* @author Tankaikai tankaikai@aliyun.com
* @since 1.0 2025-02-13
*/
public interface ShopProdSpecService extends IService<ShopProdSpec> {
Page<ShopProdSpecDTO> getShopProdSpecPage(ShopProdSpecDTO param);
List<ShopProdSpecDTO> getShopProdSpecList(ShopProdSpecDTO param);
ShopProdSpecDTO getShopProdSpecById(Long id);
boolean addShopProdSpec(ShopProdSpecDTO dto);
boolean deleteShopProdSpec(Long id);
boolean updateShopProdSpec(ShopProdSpecDTO dto);
boolean disableShopProdSpec(Long id);
boolean enableShopProdSpec(Long id);
}

View File

@ -1,33 +0,0 @@
package com.czg.product.service;
import com.czg.product.dto.ShopProductSpecDTO;
import com.czg.product.entity.ShopProductSpec;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.service.IService;
import java.util.List;
/**
* 商品规格
*
* @author Tankaikai tankaikai@aliyun.com
* @since 1.0 2025-02-13
*/
public interface ShopProductSpecService extends IService<ShopProductSpec> {
Page<ShopProductSpecDTO> page(ShopProductSpecDTO param);
List<ShopProductSpecDTO> list(ShopProductSpecDTO param);
ShopProductSpecDTO get(Long id);
boolean save(ShopProductSpecDTO dto);
boolean delete(Long id);
boolean update(ShopProductSpecDTO dto);
boolean disable(Long id);
boolean enable(Long id);
}

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();
}
}