package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.orderTaking.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class OrderTakingServiceImpl {

    @Autowired
    private SysConfigServiceImpl sysConfigService;
    @Autowired
    private MachineMapper machineMapper;
    @Autowired
    private ShopRecommendMapper shopRecommendMapper;
    @Autowired
    private GoodsTagMapper goodsTagMapper;
    @Autowired
    private ShopGoodsSkuMapper shopGoodsSkuMapper;
    @Autowired
    private ShopGoodsMapper shopGoodsMapper;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;


    /**
     * 获取机器上的点单信息
     * @param machineCode
     * @return
     */
    public OrderTakingVo getMachineInfo(String machineCode) {

        Machine machine = new Machine();
        machine.setCode(machineCode);
        List<Machine> machines = machineMapper.selectMachineList(machine);
        if(machines==null && !machines.isEmpty()){
            throw new ServiceException("该机器没有绑定店铺");
        }
        Long shopId = machines.get(0).getShopId();
        return this.getOrderTakingInfo(shopId);


    }

    public OrderTakingVo getInfoByShop(String shopId) {
        return this.getOrderTakingInfo(Long.parseLong(shopId));

    }

    public OrderTakingVo getOrderTakingInfo(Long shopId){
        OrderTakingVo orderTakingVo = new OrderTakingVo();
        ImagesVo imagesVo = new ImagesVo();
        orderTakingVo.setImages(imagesVo);
        imagesVo.setLeft(sysConfigService.selectConfigByKey("menu.left"));
        imagesVo.setRight(sysConfigService.selectConfigByKey("menu.right"));
        orderTakingVo.setCountOfOrder(sysConfigService.selectConfigByKey("goods.limit"));
        orderTakingVo.setTips(sysConfigService.selectConfigByKey("tips.address"));
        List<CategoryVo> categoryVos = new ArrayList<>();
        CategoryVo preferentialCategoryVo = new CategoryVo();
        preferentialCategoryVo.setId(0L);
        preferentialCategoryVo.setName("今日特惠");
        //查询出该店铺今日特惠的商品
        List<GoodsVo> goodsVos = shopRecommendMapper.selectShopGoodsByTyepe("2",shopId);
        preferentialCategoryVo.setGoods(goodsVos);
        for (GoodsVo goodsVo : goodsVos) {
            GoodsTag goodsTag = new GoodsTag();
            goodsTag.setState("1");
            goodsTag.setGoodsId(Long.parseLong(goodsVo.getGoodId()));
            List<GoodsTag> goodsTags = goodsTagMapper.selectGoodsTagList(goodsTag);
            String tags = JSONArray.toJSONString(goodsTags.stream().map(GoodsTag::getTag).collect(Collectors.toList()));
            goodsVo.setTags(tags);
            //获取产品信息
            List<SkuVo> skuVos = shopGoodsSkuMapper.selectSkuByGoodAndShop(shopId, goodsVo.getGoodId());
            goodsVo.setSkus(skuVos);
        }
        //普通分类的产品

        List<GoodsCategory> shopCategories = goodsCategoryMapper.selectByShopId(shopId);
        for (GoodsCategory category : shopCategories) {
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(Long.parseLong(category.getId()));
            categoryVo.setName(category.getName());
            List<Goods> goodsList = shopGoodsMapper.selectShopCategoryGoodsByCategoryId(shopId, category.getId());
            for (Goods good : goodsList) {
                GoodsVo goodsVo =new GoodsVo();
                goodsVo.setGoodId(String.valueOf(good.getId()));
                goodsVo.setName(good.getName());
                goodsVo.setDesc(good.getDesc());
                goodsVo.setDiscount(good.getDiscount().stripTrailingZeros().toString());
                goodsVo.setPrice(good.getPrice().stripTrailingZeros().toString());
                goodsVo.setPics(good.getPics());
                goodsVo.setRemark(good.getRemark());
                goodsVo.setSpecs(JSONArray.parseArray(good.getSpec(),SpecVo.class));
                GoodsTag goodsTag = new GoodsTag();
                goodsTag.setState("1");
                goodsTag.setGoodsId(Long.parseLong(goodsVo.getGoodId()));
                List<GoodsTag> goodsTags = goodsTagMapper.selectGoodsTagList(goodsTag);
                String tags = JSONArray.toJSONString(goodsTags.stream().map(GoodsTag::getTag).collect(Collectors.toList()));
                goodsVo.setTags(tags);
                //获取产品信息
                List<SkuVo> skuVos = shopGoodsSkuMapper.selectSkuByGoodAndShop(shopId, goodsVo.getGoodId());
                goodsVo.setSkus(skuVos);
            }
            categoryVos.add(categoryVo);
        }
        List<RecommendVo> recommendVos = new ArrayList<>();
        List<GoodsVo> recommendGoods = shopRecommendMapper.selectShopGoodsByTyepe("1", shopId);
        for (GoodsVo goodsVo : recommendGoods) {
            RecommendVo recommendVo = new RecommendVo();
            recommendVos.add(recommendVo);
            ShopRecommend shopRecommend = new ShopRecommend();
            shopRecommend.setShopId(String.valueOf(shopId));
            shopRecommend.setGoodsId(goodsVo.getGoodId());
            shopRecommend.setType("1");
            ShopRecommend shopRecommend1 = shopRecommendMapper.selectShopRecommendList(shopRecommend).get(0);
            recommendVo.setDesc(shopRecommend1.getDesc());
            recommendVo.setGoodsName(shopRecommend1.getGoodsName());
            recommendVo.setPic(shopRecommend1.getPic());
            recommendVo.setGoods(recommendGoods);
            GoodsTag goodsTag = new GoodsTag();
            goodsTag.setState("1");
            goodsTag.setGoodsId(Long.parseLong(goodsVo.getGoodId()));
            List<GoodsTag> goodsTags = goodsTagMapper.selectGoodsTagList(goodsTag);
            String tags = JSONArray.toJSONString(goodsTags.stream().map(GoodsTag::getTag).collect(Collectors.toList()));
            goodsVo.setTags(tags);
            //获取产品信息
            List<SkuVo> skuVos = shopGoodsSkuMapper.selectSkuByGoodAndShop(shopId, goodsVo.getGoodId());
            goodsVo.setSkus(skuVos);
        }
        orderTakingVo.setCategorys(categoryVos);
        return orderTakingVo;

    }

    /**
     * 机器点单屏信息
     * {
     *     "images": {
     *         "left": [
     *             "http://baidu.com",
     *             "http://baidu.com"
     *         ],
     *         "right": [
     *             {
     *                 "code": 1,
     *                 "url": "http://baidu.com"
     *             },
     *             {
     *                 "code": 1,
     *                 "url": "http://baidu.com"
     *             },
     *             {
     *                 "code": 1,
     *                 "url": "http://baidu.com"
     *             },
     *             {
     *                 "code": 1,
     *                 "url": "http://baidu.com"
     *             }
     *         ]
     *     },
     *     "tips": "http://baidu.com",
     *     "countOfOrder": 9,
     *     "categorys": [
     *         {
     *             "id": 1,
     *             "name": "今日特惠",
     *             "goods": [
     *                 {
     *                     "goodId": 100,
     *                     "name": "冰茶咖啡",
     *                     "price": 29.9,
     *                     "discount": 19.9,
     *                     "pics": {
     *                         "thumbnail": "http://baidu.com",
     *                         "introImages": [
     *                             "http://baidu.com",
     *                             "http://baidu.com",
     *                             "http://baidu.com"
     *                         ],
     *                         "detailImages": [
     *                             "http://baidu.com",
     *                             "http://baidu.com",
     *                             "http://baidu.com"
     *                         ]
     *                     },
     *                     "desc": "",
     *                     "remark": "",
     *                     "tags": [
     *                         "YYDS",
     *                         "杨幂同款"
     *                     ],
     *                     "specs": [
     *                         {
     *                             "specId": 1,
     *                             "specName": "温度",
     *                             "rules": [
     *                                 {
     *                                     "ruleId": 1,
     *                                     "ruleName": "常温",
     *                                     "price": 0
     *                                 },
     *                                 {
     *                                     "ruleId": 2,
     *                                     "ruleName": "加冰",
     *                                     "price": 2
     *                                 },
     *                                 {
     *                                     "ruleId": 3,
     *                                     "ruleName": "加热",
     *                                     "price": 3
     *                                 }
     *                             ]
     *                         },
     *                         {
     *                             "specId": 2,
     *                             "specName": "糖度",
     *                             "rules": [
     *                                 {
     *                                     "ruleId": 4,
     *                                     "ruleName": "无糖",
     *                                     "price": 0
     *                                 },
     *                                 {
     *                                     "ruleId": 5,
     *                                     "ruleName": "少糖",
     *                                     "price": 1
     *                                 },
     *                                 {
     *                                     "ruleId": 6,
     *                                     "ruleName": "多糖",
     *                                     "price": 2
     *                                 }
     *                             ]
     *                         }
     *                     ],
     *                     "skus": [
     *                         {
     *                             "skuId": 1,
     *                             "state": 1,
     *                             "rules": [
     *                                 {
     *                                     "specId": 1,
     *                                     "specName": "温度",
     *                                     "ruleId": 1,
     *                                     "ruleName": "常温",
     *                                     "price": 0.00
     *                                 },
     *                                 {
     *                                     "specId": 2,
     *                                     "specName": "糖度",
     *                                     "ruleId": 3,
     *                                     "ruleName": "无糖",
     *                                     "price": 0.00
     *                                 }
     *                             ]
     *                         },
     *                         {
     *                             "skuId": 1,
     *                             "state": 1,
     *                             "rules": [
     *                                 {
     *                                     "specId": 1,
     *                                     "specName": "温度",
     *                                     "ruleId": 2,
     *                                     "ruleName": "冰",
     *                                     "price": 2.00
     *                                 },
     *                                 {
     *                                     "specId": 2,
     *                                     "specName": "糖度",
     *                                     "ruleId": 3,
     *                                     "ruleName": "无糖",
     *                                     "price": 0.00
     *                                 }
     *                             ]
     *                         },
     *                         {
     *                             "skuId": 1,
     *                             "state": 1,
     *                             "rules": [
     *                                 {
     *                                     "specId": 1,
     *                                     "specName": "温度",
     *                                     "ruleId": 1,
     *                                     "ruleName": "常温",
     *                                     "price": 0.00
     *                                 },
     *                                 {
     *                                     "specId": 2,
     *                                     "specName": "糖度",
     *                                     "ruleId": 3,
     *                                     "ruleName": "加糖",
     *                                     "price": 2.00
     *                                 }
     *                             ]
     *                         },
     *                         {
     *                             "skuId": 1,
     *                             "state": 1,
     *                             "rules": [
     *                                 {
     *                                     "specId": 1,
     *                                     "specName": "温度",
     *                                     "ruleId": 1,
     *                                     "ruleName": "冰",
     *                                     "price": 2.00
     *                                 },
     *                                 {
     *                                     "specId": 2,
     *                                     "specName": "糖度",
     *                                     "ruleId": 3,
     *                                     "ruleName": "加糖",
     *                                     "price": 4.00
     *                                 }
     *                             ]
     *                         }
     *                     ]
     *                 }
     *             ]
     *         }
     *     ],
     *     "recommends": [
     *         {
     *             "goodsName": "推荐名称",
     *             "pic": "http://baidu.com",
     *             "desc": "推荐描述",
     *             "goods": [
     *                 {
     *                     "goodId": 100,
     *                     "name": "冰茶咖啡",
     *                     "price": 29.9,
     *                     "discount": 19.9,
     *                     "pics": {
     *                         "thumbnail": "http://baidu.com",
     *                         "introImages": [
     *                             "http://baidu.com",
     *                             "http://baidu.com",
     *                             "http://baidu.com"
     *                         ],
     *                         "detailImages": [
     *                             "http://baidu.com",
     *                             "http://baidu.com",
     *                             "http://baidu.com"
     *                         ]
     *                     },
     *                     "desc": "",
     *                     "remark": "",
     *                     "tags": [
     *                         "YYDS",
     *                         "杨幂同款"
     *                     ],
     *                     "specs": [
     *                         {
     *                             "specId": 1,
     *                             "specName": "温度",
     *                             "rules": [
     *                                 {
     *                                     "ruleId": 1,
     *                                     "ruleName": "常温",
     *                                     "price": 0
     *                                 },
     *                                 {
     *                                     "ruleId": 2,
     *                                     "ruleName": "加冰",
     *                                     "price": 2
     *                                 },
     *                                 {
     *                                     "ruleId": 3,
     *                                     "ruleName": "加热",
     *                                     "price": 3
     *                                 }
     *                             ]
     *                         }
     *                     ],
     *                     "skus": [
     *                         {
     *                             "skuId": 1,
     *                             "state": 1,
     *                             "rules": [
     *                                 {
     *                                     "specId": 1,
     *                                     "specName": "温度",
     *                                     "ruleId": 1,
     *                                     "ruleName": "常温",
     *                                     "price": 0.00
     *                                 },
     *                                 {
     *                                     "specId": 2,
     *                                     "specName": "糖度",
     *                                     "ruleId": 3,
     *                                     "ruleName": "无糖",
     *                                     "price": 0.00
     *                                 }
     *                             ]
     *                         }
     *                     ]
     *                 }
     *             ]
     *         }
     *     ]
     * }
     */
}
