三十九.销售属性维护功能

因为销售属性和基本属性共用的是一张表pms_attr,通过attr_type字段来区分,但是销售属性是没有属性分组这个字段需要维护的,所以如果为销售属性,那么不需要维护属性-属性分组关联表。

所以接下来要做的是就是在相同的接口和逻辑中区分出销售属性和基本属性。

webshop-common包中添加constant文件夹以及商品的销售属性以及基本属性的枚举,如下:

在这里插入图片描述
ProductConstant类信息如下:

package com.jiejie.common.constant;

/**
 * 这个类中维护商品相关的枚举
 */
public class ProductConstant {
    public enum AttrEnum {
        ATTR_TYPE_BASE(1, "基本属性"), ATTR_TYPE_SALE(0, "销售属性");
        private int code;
        private String msg;

        AttrEnum(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }
}

修改AttrController类中的baseAttrList接口,添加上分类类型,如下:

@GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String attrType) {

        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

同步修改AttrService类的方法参数,如下:
在这里插入图片描述
修改AttrServiceImpl类中需要加上类型区分逻辑的方法,如下:

package com.jiejie.webshop.prodect.service.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.service.impl.ServiceImpl;
import com.jiejie.common.constant.ProductConstant;
import com.jiejie.common.utils.PageUtils;
import com.jiejie.common.utils.Query;
import com.jiejie.webshop.prodect.dao.AttrAttrgroupRelationDao;
import com.jiejie.webshop.prodect.dao.AttrDao;
import com.jiejie.webshop.prodect.dao.AttrGroupDao;
import com.jiejie.webshop.prodect.dao.CategoryDao;
import com.jiejie.webshop.prodect.entity.AttrAttrgroupRelationEntity;
import com.jiejie.webshop.prodect.entity.AttrEntity;
import com.jiejie.webshop.prodect.entity.AttrGroupEntity;
import com.jiejie.webshop.prodect.entity.CategoryEntity;
import com.jiejie.webshop.prodect.qo.AttrQo;
import com.jiejie.webshop.prodect.service.AttrService;
import com.jiejie.webshop.prodect.service.CategoryService;
import com.jiejie.webshop.prodect.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public void saveAttr(AttrQo attrQo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrQo, attrEntity);
        //插入属性表
        this.save(attrEntity);
        //只有基础属性才需要插入属性-属性分组关联表
        if (attrQo.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attrQo.getAttrGroupId() != null) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attrQo.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String attrType) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type", "base".equalsIgnoreCase(attrType) ? ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() : ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());

        if (catelogId != 0) {
            queryWrapper.eq("catelog_id", catelogId);
        }

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("attr_id", key).or().like("attr_name", key);
            });
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );

        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        //重新做一次设置,补充上列表缺失的字段,返回的是vo
        List<AttrVo> attrVos = records.stream().map((attrEntity) -> {
            AttrVo attrVo = new AttrVo();
            BeanUtils.copyProperties(attrEntity, attrVo);
            if ("base".equalsIgnoreCase(attrType)) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null && attrAttrgroupRelationEntity.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
                    attrVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }

            }
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrVo.setCatelogName(categoryEntity.getName());
            }
            return attrVo;
        }).collect(Collectors.toList());

        pageUtils.setList(attrVos);
        return pageUtils;
    }

    @Override
    public AttrVo getAttrInfo(Long attrId) {
        AttrVo attrVo = new AttrVo();
        AttrEntity attrEntity = this.getById(attrId);
        BeanUtils.copyProperties(attrEntity, attrVo);
        if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
            //设置分组信息
            AttrAttrgroupRelationEntity attrgroupRelation = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
            if (attrgroupRelation != null) {
                attrVo.setAttrGroupId(attrgroupRelation.getAttrGroupId());
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupRelation.getAttrGroupId());
                if (attrGroupEntity != null) {
                    attrVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
        }
        //设置分类信息
        Long catelogId = attrEntity.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrVo.setCatelogPath(catelogPath);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (categoryEntity != null) {
            attrVo.setCatelogName(categoryEntity.getName());
        }
        return attrVo;
    }

    @Transactional
    @Override
    public void updateAttr(AttrQo attrQo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrQo, attrEntity);
        this.updateById(attrEntity);
        if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
            //修改分组关联
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attrQo.getAttrGroupId());
            relationEntity.setAttrId(attrQo.getAttrId());
            //由于null不更新的特性,所以需要判断下数据到底是新增还是修改
            Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrQo.getAttrId()));
            if (count > 0) {
                relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrQo.getAttrId()));
            } else {
                relationDao.insert(relationEntity);
            }
        }
    }
}

测试效果:
新增数据后,销售属性列表效果如下:
在这里插入图片描述
查看详情如下:
在这里插入图片描述
同时新增销售属性不会增加属性-属性分组关联表数据,如下:

pms_attr表数据:
在这里插入图片描述

pms_attr_attrgroup_relation表数据:
在这里插入图片描述

可以发现明显属性-属性分组关联表中明显没有attr_id为6的数据。

销售属性维护功能完成。

猜你喜欢

转载自blog.csdn.net/weixin_38106322/article/details/107433383