package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.GenerateCode;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.service.IGoodsCategoryService;
import com.ruoyi.system.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GoodsCategoryMapper;
import com.ruoyi.system.domain.GoodsCategory;

/**
 * 商品分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-28
 */
@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService
{
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品分类
     * 
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public GoodsCategory selectGoodsCategoryById(String id)
    {
        return goodsCategoryMapper.selectGoodsCategoryById(id);
    }

    /**
     * 查询商品分类列表
     * 
     * @param goodsCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory)
    {
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
        for (GoodsCategory category : goodsCategories) {
            category.setGoodsCount(goodsMapper.selectCount(category.getId()));
        }
        return goodsCategories;
    }

    /**
     * 新增商品分类
     * 
     * @param goodsCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertGoodsCategory(GoodsCategory goodsCategory)
    {
        goodsCategory.setCode(GenerateCode.getCode("PC"));
        goodsCategory.setCreatedAt(new Date());
        goodsCategory.setUpdatedAt(new Date());
        goodsCategory.setIsDeleted("0");
        return goodsCategoryMapper.insertGoodsCategory(goodsCategory);
    }

    /**
     * 修改商品分类
     * 
     * @param goodsCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateGoodsCategory(GoodsCategory goodsCategory)
    {
        if("2".equals(goodsCategory.getState())){
            Goods goods = new Goods();
            goods.setCategory(goodsCategory.getId());
            goods.setIsDeleted(0L);
            int size = goodsMapper.selectGoodsList(goods).size();
            if(size>0){
                throw new ServiceException("有商品还在使用，请先删除商品");
            }
        }
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    /**
     * 批量删除商品分类
     * 
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryByIds(String[] ids)
    {
        for (String id : ids) {
            Goods goods = new Goods();
            goods.setCategory(id);
            goods.setIsDeleted(0L);
            int size = goodsMapper.selectGoodsList(goods).size();
            if(size>0){
                throw new ServiceException("有商品还在使用，请先删除商品");
            }
        }
        return goodsCategoryMapper.deleteGoodsCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     * 
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryById(String id)
    {
        GoodsCategory goodsCategory = goodsCategoryMapper.selectGoodsCategoryById(id);
        goodsCategory.setIsDeleted("1");
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    @Override
    public int turnOrder(String previous, String latter) {
        GoodsCategory previousCategory = goodsCategoryMapper.selectGoodsCategoryById(previous);
        GoodsCategory latterCategory = goodsCategoryMapper.selectGoodsCategoryById(latter);
        String turn = previousCategory.getTurn();
        previousCategory.setTurn(latterCategory.getTurn());
        latterCategory.setTurn(turn);
        goodsCategoryMapper.updateGoodsCategory(previousCategory);
        goodsCategoryMapper.updateGoodsCategory(latterCategory);
        return 1;
    }
}
