package com.sinata.modular.mall.controller;
|
|
import cn.hutool.core.util.StrUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.google.common.collect.Lists;
|
import com.sinata.core.base.controller.BaseController;
|
import com.sinata.core.base.tips.ErrorTip;
|
import com.sinata.core.common.annotion.BussinessLog;
|
import com.sinata.core.common.annotion.Permission;
|
import com.sinata.core.common.constant.factory.PageFactory;
|
import com.sinata.core.log.LogObjectHolder;
|
import com.sinata.modular.mall.model.*;
|
import com.sinata.modular.mall.service.*;
|
import org.apache.commons.collections.CollectionUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.ui.Model;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import java.util.*;
|
|
/**
|
* 规格属性控制器
|
*
|
* @author goku
|
*/
|
@Controller
|
@RequestMapping("/mallGoodsSpec")
|
public class MallGoodsSpecController extends BaseController {
|
|
private String PREFIX = "/mall/mallGoodsSpec/";
|
|
@Autowired
|
private IMallGoodsSpecService mallGoodsSpecService;
|
|
@Autowired
|
private IMallClassifyOneService iMallClassifyOneService;
|
|
@Autowired
|
private IMallClassifyTwoService iMallClassifyTwoService;
|
|
@Autowired
|
private IMallGoodsSpecValueService mallGoodsSpecValueService;
|
|
@Autowired
|
private IMallGoodsSkuService mallGoodsSkuService;
|
|
/**
|
* 跳转到规格属性首页
|
*/
|
@RequestMapping("")
|
public String index() {
|
return PREFIX + "mallGoodsSpec.html";
|
}
|
|
/**
|
* 跳转到添加规格属性
|
*/
|
@RequestMapping("/mallGoodsSpec_add")
|
public String mallGoodsSpecAdd(Model model) {
|
List<MallClassifyOne> mallClassifyOneList = this.iMallClassifyOneService.selectList(new EntityWrapper<MallClassifyOne>().eq("is_delete", 0));
|
List<String> oneIds = Lists.newArrayList();
|
for (MallClassifyOne classifyOne : mallClassifyOneList) {
|
oneIds.add("" + classifyOne.getId());
|
}
|
|
model.addAttribute("mallClassifyOneList", mallClassifyOneList);
|
model.addAttribute("oneIds", String.join(",", oneIds));
|
return PREFIX + "mallGoodsSpec_add.html";
|
}
|
|
/**
|
* 跳转到修改规格属性
|
*/
|
@RequestMapping("/mallGoodsSpec_update/{mallGoodsSpecId}")
|
public String mallGoodsSpecUpdate(@PathVariable Integer mallGoodsSpecId, Model model) {
|
MallGoodsSpec mallGoodsSpec = mallGoodsSpecService.selectById(mallGoodsSpecId);
|
model.addAttribute("item", mallGoodsSpec);
|
List<MallClassifyOne> mallClassifyOneList = this.iMallClassifyOneService.selectList(new EntityWrapper<MallClassifyOne>().eq("is_delete", 0));
|
model.addAttribute("mallClassifyOneList", mallClassifyOneList);
|
model.addAttribute("mallSpecValues", this.mallGoodsSpecValueService
|
.selectList(new EntityWrapper<MallGoodsSpecValue>().eq("spec_id", mallGoodsSpecId)
|
.eq("is_delete", "0")));
|
LogObjectHolder.me().set(mallGoodsSpec);
|
return PREFIX + "mallGoodsSpec_edit.html";
|
}
|
|
/**
|
* 获取规格属性列表
|
*
|
* @param specName 规格属性名称
|
*/
|
@ResponseBody
|
@RequestMapping(value = "/list")
|
public Object list(String beginTime, String endTime, String specName) {
|
Page<Map<String, Object>> page = new PageFactory().defaultPage();
|
Wrapper wrapper = new EntityWrapper<MallGoodsSpec>()
|
.ne("is_delete", 1).orderBy("id", false);
|
|
// 时间搜索
|
if (!StringUtils.isEmpty(beginTime)) {
|
wrapper.ge("create_time", beginTime + " 00:00:00");
|
}
|
if (!StringUtils.isEmpty(endTime)) {
|
wrapper.le("create_time", endTime + " 23:59:59");
|
}
|
if (!StringUtils.isEmpty(specName)) {
|
wrapper.like("spec_name", specName);
|
}
|
Map<String, String> classMap = getClassIdAndName();
|
// 查询数据列表
|
List<Map<String, Object>> list = mallGoodsSpecService.selectMapsPage(page, wrapper).getRecords();
|
if (CollectionUtils.isNotEmpty(list)) {
|
for (Map<String, Object> map : list) {
|
StringBuffer classNames = new StringBuffer("");
|
String oneIds = (String) map.get("classifyIdOne");
|
if (org.apache.commons.lang3.StringUtils.isNotEmpty(oneIds)) {
|
String[] ids = oneIds.split(",");
|
for (String id : ids) {
|
String className = classMap.get(id);
|
if (org.apache.commons.lang3.StringUtils.isNotEmpty(className)) {
|
classNames.append(className + ",");
|
}
|
}
|
}
|
|
map.put("classNames", classNames.toString());
|
}
|
}
|
|
page.setRecords(list);
|
return super.packForBT(page);
|
}
|
|
/**
|
* @Title: getClassIdAndName
|
* @Description: 获取类型对应类型和名字map
|
* @author guohongjin
|
* @date 2023/3/20
|
*/
|
@RequestMapping("/getClassIdAndName")
|
@ResponseBody
|
public Map<String, String> getClassIdAndName() {
|
Map<String, String> classMap = new HashMap<String, String>();
|
List<MallClassifyOne> mallClassifyOneList = this.iMallClassifyOneService.selectList(new EntityWrapper<MallClassifyOne>().eq("is_delete", 0));
|
if (CollectionUtils.isNotEmpty(mallClassifyOneList)) {
|
for (MallClassifyOne mallClassifyOne : mallClassifyOneList) {
|
classMap.put(mallClassifyOne.getId() + "", mallClassifyOne.getClassifyName());
|
}
|
}
|
return classMap;
|
}
|
|
/**
|
* 新增规格属性
|
*/
|
@Permission
|
@ResponseBody
|
@BussinessLog(value = "新增规格属性")
|
@RequestMapping(value = "/add")
|
public Object add(MallGoodsSpec mallGoodsSpec) {
|
mallGoodsSpecService.insert(mallGoodsSpec);
|
// if (org.apache.commons.lang3.StringUtils.isNotEmpty(mallGoodsSpec.getSpecValuesJson())){
|
// List<MallGoodsSpecValue> mallGoodsSpecValueList = JSONObject.parseArray(mallGoodsSpec.getSpecValuesJson(),MallGoodsSpecValue.class);
|
// mallGoodsSpecValueList.forEach(mallGoodsSpecValue -> {
|
// mallGoodsSpecValue.setSpecId(mallGoodsSpec.getId());
|
// });
|
// this.mallGoodsSpecValueService.insertBatch(mallGoodsSpecValueList);
|
// }
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 删除/批量删除
|
*/
|
@Permission
|
@ResponseBody
|
@BussinessLog(value = "删除/批量删除规格属性")
|
@RequestMapping(value = "/delete")
|
public Object delete(@RequestParam String ids) {
|
String[] idd = ids.split(",");
|
if (idd.length == 1) {
|
int count1 = this.mallGoodsSkuService.selectCount(new EntityWrapper<MallGoodsSku>().where("FIND_IN_SET({0},spec_ids)", idd[0]));
|
if (count1 == 0) {
|
// 逻辑删除
|
mallGoodsSpecService.updateForSet("is_delete = 1", new EntityWrapper<MallGoodsSpec>().in("id", ids.split(",")));
|
return SUCCESS_TIP;
|
} else {
|
ErrorTip errorTip = new ErrorTip(500, "该规格已被使用,不能删除");
|
return errorTip;
|
}
|
} else {
|
boolean state = false;
|
for (String id : idd) {
|
int count1 = this.mallGoodsSkuService.selectCount(new EntityWrapper<MallGoodsSku>().where("FIND_IN_SET({0},spec_ids)", id));
|
if (count1 == 0) {
|
// 逻辑删除
|
mallGoodsSpecService.updateForSet("is_delete = 1", new EntityWrapper<MallGoodsSpec>().in("id", ids.split(",")));
|
} else {
|
state = true;
|
}
|
}
|
if (state) {
|
ErrorTip errorTip = new ErrorTip(500, "已经被使用的分类删除失败。请解除关联关系后再删除");
|
return errorTip;
|
} else {
|
return SUCCESS_TIP;
|
}
|
}
|
}
|
|
/**
|
* 修改规格属性
|
*
|
* @param specValueIds 规格值ids 需要删除的没有删除的为null
|
*/
|
@Permission
|
@ResponseBody
|
@BussinessLog(value = "修改规格属性")
|
@RequestMapping(value = "/update")
|
public Object update(MallGoodsSpec mallGoodsSpec, String specValueIds, String deleteSpecValueIds) {
|
mallGoodsSpecService.updateById(mallGoodsSpec);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 修改规格属性状态
|
*/
|
@ResponseBody
|
@BussinessLog(value = "修改规格属性状态")
|
@RequestMapping(value = "/updateState")
|
public Object updateState(Integer mallGoodsSpecId, Integer state) {
|
mallGoodsSpecService.updateForSet("is_lock = " + state, new EntityWrapper<MallGoodsSpec>().eq("id", mallGoodsSpecId));
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 跳转规格属性详情
|
*/
|
@RequestMapping(value = "/detail/{mallGoodsSpecId}")
|
public Object detail(@PathVariable("mallGoodsSpecId") Integer mallGoodsSpecId, Model model) {
|
MallGoodsSpec mallGoodsSpec = mallGoodsSpecService.selectById(mallGoodsSpecId);
|
model.addAttribute("item", mallGoodsSpec);
|
return PREFIX + "mallGoodsSpec_detail.html";
|
}
|
}
|