package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.domain.Machine;
import com.ruoyi.system.mapper.MachineMapper;
import com.ruoyi.system.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ShopRecommendMapper;
import com.ruoyi.system.domain.ShopRecommend;
import com.ruoyi.system.service.IShopRecommendService;

/**
 * 推荐和今日特惠Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-28
 */
@Service
public class ShopRecommendServiceImpl implements IShopRecommendService 
{
    @Autowired
    private ShopRecommendMapper shopRecommendMapper;
    @Autowired
    private MachineMapper machineMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询推荐和今日特惠
     * 
     * @param id 推荐和今日特惠主键
     * @return 推荐和今日特惠
     */
    @Override
    public ShopRecommend selectShopRecommendById(String id)
    {
        return shopRecommendMapper.selectShopRecommendById(id);
    }

    /**
     * 查询推荐和今日特惠列表
     * 
     * @param shopRecommend 推荐和今日特惠
     * @return 推荐和今日特惠
     */
    @Override
    public List<Goods> selectShopRecommendList(ShopRecommend shopRecommend)
    {
        List<Goods> goodsList = shopRecommendMapper.selectShopRecommendGoods(shopRecommend.getShopId(),shopRecommend.getType());
        for (Goods goods : goodsList) {
            goods.setSalesVolume(orderMapper.selectSalesVolume(Long.parseLong(shopRecommend.getShopId()),goods.getId()));
        }
         return goodsList;
    }

    /**
     * 新增推荐和今日特惠
     * 
     * @param shopRecommend 推荐和今日特惠
     * @return 结果
     */
    @Override
    public int insertShopRecommend(ShopRecommend shopRecommend)
    {
        Machine machine = new Machine();
        machine.setShopId(Long.parseLong(shopRecommend.getShopId()));
        shopRecommend.setCreatedAt(new Date());
        shopRecommend.setRecDate(new Date());
        shopRecommendMapper.insertShopRecommend(shopRecommend);
        shopRecommend.setTurn(shopRecommend.getId());
        return shopRecommendMapper.updateShopRecommend(shopRecommend);
    }

    /**
     * 修改推荐和今日特惠
     * 
     * @param shopRecommend 推荐和今日特惠
     * @return 结果
     */
    @Override
    public int updateShopRecommend(ShopRecommend shopRecommend)
    {
        return shopRecommendMapper.updateShopRecommend(shopRecommend);
    }

    /**
     * 批量删除推荐和今日特惠
     * 
     * @param ids 需要删除的推荐和今日特惠主键
     * @return 结果
     */
    @Override
    public int deleteShopRecommendByIds(String[] ids)
    {
        return shopRecommendMapper.deleteShopRecommendByIds(ids);
    }

    /**
     * 删除推荐和今日特惠信息
     * 
     * @param id 推荐和今日特惠主键
     * @return 结果
     */
    @Override
    public int deleteShopRecommendById(String id)
    {
        return shopRecommendMapper.deleteShopRecommendById(id);
    }

    @Override
    public String turn(Long shopId, String fistId, String lastId, String type) {
        ShopRecommend shopRecommend = new ShopRecommend();
        shopRecommend.setShopId(String.valueOf(shopId));
        shopRecommend.setType(type);
        shopRecommend.setGoodsId(fistId);
        ShopRecommend frist = shopRecommendMapper.selectShopRecommendList(shopRecommend).get(0);
        String turn  = frist.getTurn();
        shopRecommend.setGoodsId(lastId);
        ShopRecommend last = shopRecommendMapper.selectShopRecommendList(shopRecommend).get(0);
        frist.setType(last.getTurn());
        last.setTurn(turn);
        shopRecommendMapper.updateShopRecommend(frist);
        shopRecommendMapper.updateShopRecommend(last);
        return null;
    }

    @Override
    public int deleteShopRecommend(String goodsId, String shopId, String type) {
        ShopRecommend shopRecommend = new ShopRecommend();
        shopRecommend.setShopId(String.valueOf(shopId));
        shopRecommend.setType(type);
        shopRecommend.setGoodsId(goodsId);
        ShopRecommend frist = shopRecommendMapper.selectShopRecommendList(shopRecommend).get(0);

        return shopRecommendMapper.deleteShopRecommendById(frist.getId());
    }
}
