package com.ruoyi.system.service.impl;

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

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IShopService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-28
 */
@Service
public class ShopServiceImpl implements IShopService 
{
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private MachineMapper machineMapper;
    @Autowired
    private GoodsSkuMapper goodsSkuMapper;
    @Autowired
    private ShopGoodsMapper shopGoodsMapper;
    @Autowired
    private ShopGoodsSkuMapper shopGoodsSkuMapper;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;




    /**
     * 查询店铺
     * 
     * @param id 店铺主键
     * @return 店铺
     */
    @Override
    public Shop selectShopById(String id)
    {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询店铺列表
     * 
     * @param shop 店铺
     * @return 店铺
     */
    @Override
    public List<Shop> selectShopList(Shop shop)
    {
        List<Shop> shops = shopMapper.selectShopList(shop);
        for (Shop shop1 : shops) {
            Machine machine = new Machine();
            machine.setShopId(shop1.getId());
            shop1.setMachineCount(machineMapper.selectMachineList(machine).size());
            shop1.setSalesAmount(orderMapper.selectSalesAmount(shop.getId()));
            shop1.setSalesVolume(orderMapper.selectSalesVolume(shop.getId(),null));
        }

        return shops;
    }

    /**
     * 新增店铺
     * 
     * @param shop 店铺
     * @return 结果
     */
    @Override
    public long insertShop(Shop shop)
    {
        shop.setCreatedAt(new Date());
        shop.setUpdatedAt(new Date());
        shop.setIsDefault(0L);
        shopMapper.insertShop(shop);
        return shop.getId();
    }

    /**
     * 修改店铺
     * 
     * @param shop 店铺
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop)
    {
        shop.setUpdatedAt(new Date());
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除店铺
     * 
     * @param ids 需要删除的店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(String[] ids)
    {
        for (String id : ids) {
            Shop shop = new Shop();
            shop.setId(Long.parseLong(id));
            shop.setIsDefault(1L);
            shop.setState("3");
            shopMapper.updateShop(shop);
        }
        return 1;

    }

    /**
     * 删除店铺信息
     * 
     * @param id 店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopById(String id)
    {
        return shopMapper.deleteShopById(id);
    }

    @Override
    public String addGoods(long shopId, String goodsIds) {
        String[] goodsIdString = goodsIds.split(",");
        for (String s : goodsIdString) {
            Long goodsId = Long.parseLong(s);
            addGoods(shopId,goodsId);

        }
        return null;
    }
    @Transactional
    public String addGoods(long shopId, Long goodsId) {
        ShopGoods shopGoods = new ShopGoods();
        shopGoods.setGoodsId(goodsId);
        shopGoods.setShopId(shopId);
        shopGoods.setUpdatedAt(new Date());
        shopGoods.setCreatedAt(new Date());
        shopGoodsMapper.insertShopGoods(shopGoods);
        shopGoods.setTurn(shopGoods.getId());
        shopGoodsMapper.updateShopGoods(shopGoods);
        GoodsSku goodsSku = new GoodsSku();
        goodsSku.setGoodsId(goodsId);
        goodsSku.setIsDeleted("0");
        List<GoodsSku> goodsSkus = goodsSkuMapper.selectGoodsSkuList(goodsSku);
        Machine machine = new Machine();
        machine.setShopId(shopId);
        List<Machine> machines = machineMapper.selectMachineList(machine);
        if(machines==null  ||machines.isEmpty()){
            throw new ServiceException("请先绑定机器");
        }
        for (Machine machine1 : machines) {
            for (GoodsSku skus : goodsSkus) {
                ShopGoodsSku shopGoodsSku = new ShopGoodsSku();
                shopGoodsSku.setGoodsId(goodsId);
                shopGoodsSku.setSkuId(skus.getId());
                shopGoodsSku.setShopId(shopId);
                shopGoodsSku.setState("1");
                shopGoodsSku.setIsDeleted("0");
                shopGoodsSku.setCreatedAt(new Date());
                shopGoodsSku.setUpdatedAt(new Date());
                shopGoodsSku.setMachineId(machine1.getId());
                shopGoodsSkuMapper.insertShopGoodsSku(shopGoodsSku);
            }
        }
        return null;
    }

    @Override
    public List<GoodsCategory>  getCategoryGoods(long shopId) {

        List<GoodsCategory> shopCategories = goodsCategoryMapper.selectByShopId(shopId);
        for (GoodsCategory category : shopCategories) {
            List<Goods> goodsList = shopGoodsMapper.selectShopCategoryGoodsByCategoryId(shopId,category.getId());
            for (Goods goods : goodsList) {
                goods.setSalesVolume(orderMapper.selectSalesVolume(shopId,goods.getId()));
            }
            category.setGoodsList(goodsList);
        }
        return shopCategories;
    }

    @Override
    public List<GoodsSku> getSkuByGoodsId(long shopId, long goodsId) {
        return shopGoodsSkuMapper.selectSkuByShopAndGoods(shopId,goodsId);

    }

    @Override
    public String turn(Long shopId, Long fristGoodsId, Long lastGoodsId) {
        ShopGoods shopGoods = new ShopGoods();
        shopGoods.setShopId(shopId);
        shopGoods.setGoodsId(fristGoodsId);
        ShopGoods fristShopGoods = shopGoodsMapper.selectShopGoodsList(shopGoods).get(0);
        String turn = fristShopGoods.getTurn();
        shopGoods.setGoodsId(lastGoodsId);
        ShopGoods lastShopGoods = shopGoodsMapper.selectShopGoodsList(shopGoods).get(0);
        fristShopGoods.setTurn(lastShopGoods.getTurn());
        lastShopGoods.setTurn(turn);
        shopGoodsMapper.updateShopGoods(fristShopGoods);
        shopGoodsMapper.updateShopGoods(lastShopGoods);
        return null;
    }

    @Override
    public List<String> getShopCity() {
        List<Shop> shops = shopMapper.selectShopList(new Shop());
        List<String> collect = shops.stream().map(Shop::getCity).collect(Collectors.toList());
        return collect;
    }




}
