package com.hssx.pcbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hssx.pcbms.entity.Goods;
import com.hssx.pcbms.entity.GoodsTags;
import com.hssx.pcbms.mapper.GoodsMapper;
import com.hssx.pcbms.mapper.GoodsNoMapper;
import com.hssx.pcbms.mapper.GoodsTagsMapper;
import com.hssx.pcbms.service.GoodsTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 吴涛涛
 * @since 2019-10-25
 */
@Service
public class GoodsTagsServiceImpl extends ServiceImpl<GoodsTagsMapper, GoodsTags> implements GoodsTagsService {
    @Resource
    private GoodsTagsMapper goodsTagsMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public HttpRespMsg add(GoodsTags goodsTags) {
        HttpRespMsg msg = new HttpRespMsg();
        Integer count = goodsTagsMapper.selectCount(new QueryWrapper<GoodsTags>().eq("name", goodsTags.getName()));
        if (count > 0) {
            msg.setError("该分类标签已存在");
            return msg;
        } else {
            if (null == goodsTags.getId()) {
                goodsTagsMapper.insert(goodsTags);
            } else {
                goodsTagsMapper.updateById(goodsTags);
            }
        }
        return msg;
    }

    @Override
    public HttpRespMsg getListByKeyName(String keyName) {
        HttpRespMsg msg = new HttpRespMsg();
        QueryWrapper<GoodsTags> qw = new QueryWrapper();
        if (keyName != null) {
            qw.like("name", keyName);
        }
        msg.data = goodsTagsMapper.selectList(qw);
        return msg;
    }

    @Override
    public HttpRespMsg delTags(GoodsTags goodsTags) {
        HttpRespMsg msg = new HttpRespMsg();
        Integer count = goodsMapper.selectCount(new QueryWrapper<Goods>().eq("tag_id", goodsTags.getId()).last("limit 1"));
        if (count > 0) {
            msg.setError("该标签下存在资产，暂不支持该操作。");
            return msg;
        }
        goodsTagsMapper.deleteById(goodsTags.getId());
        return msg;
    }
}
