|
|
@ -0,0 +1,215 @@ |
|
|
|
package org.jeecg.modules.sysMiniapp.product.controller; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
import org.apache.shiro.authz.annotation.RequiresPermissions; |
|
|
|
import org.jeecg.common.api.vo.Result; |
|
|
|
import org.jeecg.common.aspect.annotation.AutoLog; |
|
|
|
import org.jeecg.common.constant.SymbolConstant; |
|
|
|
import org.jeecg.common.system.base.controller.JeecgController; |
|
|
|
import org.jeecg.common.system.query.QueryGenerator; |
|
|
|
import org.jeecg.common.util.oConvertUtils; |
|
|
|
import org.jeecg.modules.sysMiniapp.product.entity.AppProduct; |
|
|
|
import org.jeecg.modules.sysMiniapp.product.service.IAppProductService; |
|
|
|
import org.jeecg.modules.sysMiniapp.productCategoryJoin.entity.AppProductCategoryJoin; |
|
|
|
import org.jeecg.modules.sysMiniapp.productCategoryJoin.mapper.AppProductCategoryJoinMapper; |
|
|
|
import org.jeecg.modules.sysMiniapp.productCategoryJoin.service.IAppProductCategoryJoinService; |
|
|
|
import org.jeecg.modules.sysMiniapp.product.entity.Product; |
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description: 产品管理 |
|
|
|
* @Author: jeecg-boot |
|
|
|
* @Date: 2025-02-16 |
|
|
|
* @Version: V1.0 |
|
|
|
*/ |
|
|
|
@Api(tags="产品管理") |
|
|
|
@RestController |
|
|
|
@RequestMapping("/product/appProduct") |
|
|
|
@Slf4j |
|
|
|
public class ProductController extends JeecgController<AppProduct, IAppProductService> { |
|
|
|
@Autowired |
|
|
|
private IAppProductService appProductService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IAppProductCategoryJoinService appProductCategoryJoinService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private AppProductCategoryJoinMapper appProductCategoryJoinMapper; |
|
|
|
|
|
|
|
/** |
|
|
|
* 分页列表查询 |
|
|
|
* |
|
|
|
* @param appProduct |
|
|
|
* @param pageNo |
|
|
|
* @param pageSize |
|
|
|
* @param req |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
//@AutoLog(value = "产品管理-分页列表查询") |
|
|
|
@ApiOperation(value="产品管理-分页列表查询", notes="产品管理-分页列表查询") |
|
|
|
@GetMapping(value = "/list") |
|
|
|
public Result<IPage<AppProduct>> queryPageList(AppProduct appProduct, |
|
|
|
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo, |
|
|
|
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize, |
|
|
|
HttpServletRequest req) { |
|
|
|
QueryWrapper<AppProduct> queryWrapper = QueryGenerator.initQueryWrapper(appProduct, req.getParameterMap()); |
|
|
|
Page<AppProduct> page = new Page<AppProduct>(pageNo, pageSize); |
|
|
|
IPage<AppProduct> pageList = appProductService.page(page, queryWrapper); |
|
|
|
pageList.getRecords().forEach(item -> { |
|
|
|
List<Integer> categoryList = appProductCategoryJoinMapper.getProductCategorysByProductId(item.getId()); |
|
|
|
if (oConvertUtils.isNotEmpty(categoryList)) { |
|
|
|
item.setCategoryId(StringUtils.join(categoryList.toArray(), SymbolConstant.COMMA)); |
|
|
|
} else { |
|
|
|
item.setCategoryId(""); |
|
|
|
} |
|
|
|
}); |
|
|
|
return Result.OK(pageList); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 添加 |
|
|
|
* |
|
|
|
* @param product |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@AutoLog(value = "产品管理-添加") |
|
|
|
@ApiOperation(value="产品管理-添加", notes="产品管理-添加") |
|
|
|
@RequiresPermissions("product:app_product:add") |
|
|
|
@PostMapping(value = "/add") |
|
|
|
@Transactional |
|
|
|
public Result<String> add(@RequestBody Product product) { |
|
|
|
List<String> categoryList = Arrays.asList(product.getCategoryId().split(",")); |
|
|
|
AppProduct appProduct = new AppProduct(); |
|
|
|
BeanUtils.copyProperties(product, appProduct); |
|
|
|
appProductService.save(appProduct); |
|
|
|
// 新增关联表 |
|
|
|
List<AppProductCategoryJoin> joinList = new ArrayList<AppProductCategoryJoin>(); |
|
|
|
for (String category : categoryList){ |
|
|
|
AppProductCategoryJoin appProductCategoryJoin = new AppProductCategoryJoin(); |
|
|
|
appProductCategoryJoin.setProductId(appProduct.getId()); |
|
|
|
appProductCategoryJoin.setCategoryId(Integer.valueOf(category)); |
|
|
|
joinList.add(appProductCategoryJoin); |
|
|
|
} |
|
|
|
appProductCategoryJoinService.saveOrUpdateBatch(joinList); |
|
|
|
|
|
|
|
return Result.OK("添加成功!"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 编辑 |
|
|
|
* |
|
|
|
* @param product |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@AutoLog(value = "产品管理-编辑") |
|
|
|
@ApiOperation(value="产品管理-编辑", notes="产品管理-编辑") |
|
|
|
@RequiresPermissions("product:app_product:edit") |
|
|
|
@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST}) |
|
|
|
@Transactional |
|
|
|
public Result<String> edit(@RequestBody Product product) { |
|
|
|
AppProduct appProduct = new AppProduct(); |
|
|
|
BeanUtils.copyProperties(product, appProduct); |
|
|
|
appProductService.updateById(appProduct); |
|
|
|
// 查询已有的分类关联 |
|
|
|
LambdaQueryWrapper<AppProductCategoryJoin> query = new LambdaQueryWrapper<>(); |
|
|
|
query.eq(AppProductCategoryJoin::getProductId,appProduct.getId()); |
|
|
|
List<AppProductCategoryJoin> existingJoins = appProductCategoryJoinService.list(query); |
|
|
|
Set<Integer> existingCategoryIds = existingJoins.stream() |
|
|
|
.map(AppProductCategoryJoin::getCategoryId) |
|
|
|
.collect(Collectors.toSet()); |
|
|
|
|
|
|
|
// 用户提交的分类ID列表 |
|
|
|
List<String> categoryList = Arrays.asList(product.getCategoryId().split(",")); |
|
|
|
Set<Integer> newCategoryIds = categoryList.stream() |
|
|
|
.map(Integer::valueOf) |
|
|
|
.collect(Collectors.toSet()); |
|
|
|
|
|
|
|
// 需要删除的关联 |
|
|
|
List<AppProductCategoryJoin> toDelete = existingJoins.stream() |
|
|
|
.filter(join -> !newCategoryIds.contains(join.getCategoryId())) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
// 需要新增的关联 |
|
|
|
List<AppProductCategoryJoin> toAdd = newCategoryIds.stream() |
|
|
|
.filter(categoryId -> !existingCategoryIds.contains(categoryId)) |
|
|
|
.map(categoryId -> { |
|
|
|
AppProductCategoryJoin join = new AppProductCategoryJoin(); |
|
|
|
join.setProductId(appProduct.getId()); |
|
|
|
join.setCategoryId(categoryId); |
|
|
|
return join; |
|
|
|
}) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
// 执行删除和新增 |
|
|
|
if (!toDelete.isEmpty()) { |
|
|
|
appProductCategoryJoinService.removeBatchByIds(toDelete); |
|
|
|
} |
|
|
|
if (!toAdd.isEmpty()) { |
|
|
|
appProductCategoryJoinService.saveBatch(toAdd); |
|
|
|
} |
|
|
|
return Result.OK("编辑成功!"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 通过id删除 |
|
|
|
* |
|
|
|
* @param id |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@AutoLog(value = "产品管理-通过id删除") |
|
|
|
@ApiOperation(value="产品管理-通过id删除", notes="产品管理-通过id删除") |
|
|
|
@RequiresPermissions("product:app_product:delete") |
|
|
|
@DeleteMapping(value = "/delete") |
|
|
|
@Transactional |
|
|
|
public Result<String> delete(@RequestParam(name="id",required=true) String id) { |
|
|
|
appProductService.removeById(id); |
|
|
|
// 删除关联表中的数据 |
|
|
|
LambdaQueryWrapper<AppProductCategoryJoin> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
queryWrapper.eq(AppProductCategoryJoin::getProductId, id); |
|
|
|
appProductCategoryJoinService.remove(queryWrapper); |
|
|
|
return Result.OK("删除成功!"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 批量删除 |
|
|
|
* |
|
|
|
* @param ids |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@AutoLog(value = "产品管理-批量删除") |
|
|
|
@ApiOperation(value="产品管理-批量删除", notes="产品管理-批量删除") |
|
|
|
@RequiresPermissions("product:app_product:deleteBatch") |
|
|
|
@DeleteMapping(value = "/deleteBatch") |
|
|
|
@Transactional |
|
|
|
public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) { |
|
|
|
// 将逗号分隔的ID转换为列表 |
|
|
|
List<String> idList = Arrays.asList(ids.split(",")); |
|
|
|
|
|
|
|
// 批量删除产品 |
|
|
|
appProductService.removeByIds(idList); |
|
|
|
|
|
|
|
// 批量删除关联表中的数据 |
|
|
|
LambdaQueryWrapper<AppProductCategoryJoin> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
queryWrapper.in(AppProductCategoryJoin::getProductId, idList); |
|
|
|
appProductCategoryJoinService.remove(queryWrapper); |
|
|
|
return Result.OK("批量删除成功!"); |
|
|
|
} |
|
|
|
} |