package com.dgtly.goods.service.impl;

import java.util.List;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.goods.domain.GoodsAttribute;
import com.dgtly.goods.domain.GoodsColor;
import com.dgtly.goods.domain.GoodsSize;
import com.dgtly.goods.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.goods.mapper.GoodsInfoMapper;
import com.dgtly.goods.domain.GoodsInfo;
import com.dgtly.goods.service.IGoodsInfoService;
import com.dgtly.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品基础详情Service业务层处理
 * 
 * @author dgtly
 * @date 2020-02-13
 */
@Service
public class GoodsInfoServiceImpl implements IGoodsInfoService 
{
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    private GoodsColorMapper goodsColorMapper;

    @Autowired
    private GoodsSizeMapper goodsSizeMapper;

    /**
     * 查询商品基础详情
     * 
     * @param id 商品基础详情ID
     * @return 商品基础详情
     */
    @Override
    public GoodsInfo selectGoodsInfoById(Long id)
    {
        return goodsInfoMapper.selectGoodsInfoById(id);
    }

    /**
     * 查询商品基础详情列表
     * 
     * @param goodsInfo 商品基础详情
     * @return 商品基础详情
     */
    @Override
    public List<GoodsInfo> selectGoodsInfoList(GoodsInfo goodsInfo)
    {
        return goodsInfoMapper.selectGoodsInfoList(goodsInfo);
    }

    /**
     * 新增商品基础详情
     * 
     * @param goodsInfo 商品基础详情
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGoodsInfo(GoodsInfo goodsInfo)
    {
        goodsInfo.setCreateTime(DateUtils.getNowDate());
        int num = goodsInfoMapper.insertGoodsInfo(goodsInfo);
        /*处理 属性 颜色 规格等数据*/
        handleAttributeSizeColor(goodsInfo);
        return num;
    }

    /**
     * 修改商品基础详情
     * 
     * @param goodsInfo 商品基础详情
     * @return 结果
     */
    @Override
    @Transactional
    public int updateGoodsInfo(GoodsInfo goodsInfo)
    {
        goodsInfo.setUpdateTime(DateUtils.getNowDate());
        /*处理 属性 颜色 规格等数据*/
        handleAttributeSizeColor(goodsInfo);

        return goodsInfoMapper.updateGoodsInfo(goodsInfo);
    }

    /**
     * 删除商品基础详情对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsInfoByIds(String ids)
    {
        return goodsInfoMapper.deleteGoodsInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商品基础详情信息
     * 
     * @param id 商品基础详情ID
     * @return 结果
     */
    @Override
    public int deleteGoodsInfoById(Long id)
    {
        return goodsInfoMapper.deleteGoodsInfoById(id);
    }

    /**
     * 查询热门商品
     *
     * @return 结果
     */
    @Override
    public List<GoodsInfo> selectGoodsInfoHotList() {
        return goodsInfoMapper.selectGoodsInfoHotList();
    }

    /**
     * @descption: 根据分类id查询商品数量包括小雷的
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @Override
    public int selectGoodsInfoCountByTypeId(Long id) {
        return goodsInfoMapper.selectGoodsInfoCountByTypeId(id);
    }


    /**
     * @descption: 为前端网页提供商品查询，价格筛选
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 10:43
     */
    @Override
    public List<GoodsInfo> selectGoodsInfoListInWeb(GoodsInfo gi) {
        return goodsInfoMapper.selectGoodsInfoListInWeb(gi);
    }

    /**
     * @descption: 批量查询商品 根据ids
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 10:43
     */
    @Override
    public List<GoodsInfo> selectGoodsInfoByIds(String ids) {
        return goodsInfoMapper.selectGoodsInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * @descption: 为前端网页提供商品查询，价格筛选
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 10:43
     */
    @Override
    public int putawayGooodsInfo(String ids, String putawayFlag) {
        return  goodsInfoMapper.putawayGooodsInfo(Convert.toStrArray(ids),putawayFlag);
    }

    /**
     * @descption: 用于更新或插入 商品属性、颜色、规格
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 10:43
     */
    @Transactional
    public void handleAttributeSizeColor(GoodsInfo goodsInfo){

        goodsAttributeMapper.deleteGoodsAttributeByGoodsId(goodsInfo.getId());
        if(goodsInfo.getGoodsAttributes()!=null&&goodsInfo.getGoodsAttributes().size()>0)
        {
            List<GoodsAttribute> goodsAttributeList = goodsInfo.getGoodsAttributes();
            int i = goodsAttributeMapper.insertGoodsAttributeList(goodsAttributeList,goodsInfo.getId());
            if (i!=goodsAttributeList.size()){
                throw  new RuntimeException("插入商品属性数量不符");
            }
        }

        goodsColorMapper.deleteGoodsColorByGoodsId(goodsInfo.getId());
        if(goodsInfo.getGoodsColors()!=null&&goodsInfo.getGoodsColors().size()>0)
        {
            List<GoodsColor> goodsColorList = goodsInfo.getGoodsColors();
            int i = goodsColorMapper.insertGoodsColorList(goodsColorList,goodsInfo.getId());
            if (i!=goodsColorList.size()){
                throw  new RuntimeException("插入商品颜色数量不符");
            }
        }

        goodsSizeMapper.deleteGoodsSizeByGoodsId(goodsInfo.getId());
        if(goodsInfo.getGoodsSizes()!=null&&goodsInfo.getGoodsSizes().size()>0)
        {
            List<GoodsSize> goodsSizeList = goodsInfo.getGoodsSizes();
            int i = goodsSizeMapper.insertGoodsSizeList(goodsSizeList,goodsInfo.getId());
            if (i!=goodsSizeList.size()){
                throw  new RuntimeException("插入商品规格数量不符");
            }
        }
    }

    @Override
    public int countGoodsInfo() {
        return goodsInfoMapper.countGoodsInfo();
    }
}
