package com.ruoyi.web.controller.api;

import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.vo.IndexBannerVO;
import com.ruoyi.mall.domain.vo.IndexGoodsVO;
import com.ruoyi.mall.domain.vo.IndexNoticeVO;
import com.ruoyi.mall.service.*;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApiIndexController
 * @Description 请描述类的业务用途
 * @Author linlinee
 * @Date 2021/11/29 4:25 PM
 * @Version 1.0
 **/

@RestController
@RequestMapping("/api/index")
@ResponseBody
public class ApiIndexController {

    @Autowired
    IMallGoodsGoodsService iMallGoodsGoodsService;

    @Autowired
    IMallBannerService iMallBannerService;

    @Autowired
    IMallImgShowService iMallImgShowService;

    @Autowired
    IMallCmsNoticeService iMallCmsNoticeService;
    @Autowired
    IMallImgService imgService;
    @Autowired
    IMallGoodsTagService mallGoodsTagService;
    @Autowired
    IMallGoodsClassService mallGoodsClassService;

    @GetMapping("/getIndexData")
    public ApiResult getIndexData(Long shopId){
        // 促销
        List<IndexGoodsVO> timeList = new ArrayList<>();

        List<IndexGoodsVO> indexGoodsVOS = iMallGoodsGoodsService.selectIndexMallGoodsByTag(1, 2);

        if (indexGoodsVOS.size() > 0) {
            for (IndexGoodsVO indexGoodsVO: indexGoodsVOS) {
                if (shopId != null) {
                    if (indexGoodsVO.getShopId().equals(shopId) || indexGoodsVO.getShopId() ==0 ) {
                        MallImg mallImg = new MallImg();
                        mallImg.setResId(indexGoodsVO.getId());
                        mallImg.setType("0");
                        List<String> list = imgService.selectMallImgUrlVoList(mallImg);
                        indexGoodsVO.setBanners(list);

                        mallImg.setType("1");
                        List<String> detailList = imgService.selectMallImgUrlVoList(mallImg);
                        indexGoodsVO.setDetailUrl(detailList);

                        timeList.add(indexGoodsVO);
                    }
                }

            }
        }


        List<IndexGoodsVO> specialList = new ArrayList<>();

        // 特价
        List<IndexGoodsVO> indexGoodsVOS1 = iMallGoodsGoodsService.selectIndexMallGoodsByTag(3, 9999);

        if (indexGoodsVOS1.size() > 0) {
            for (IndexGoodsVO indexGoodsVO: indexGoodsVOS1) {
                if (shopId != null) {
                    if (indexGoodsVO.getShopId().equals(shopId) || indexGoodsVO.getShopId() ==0 ) {
                        MallImg mallImg = new MallImg();
                        mallImg.setResId(indexGoodsVO.getId());
                        mallImg.setType("0");
                        List<String> list = imgService.selectMallImgUrlVoList(mallImg);
                        indexGoodsVO.setBanners(list);

                        mallImg.setType("1");
                        List<String> detailList = imgService.selectMallImgUrlVoList(mallImg);
                        indexGoodsVO.setDetailUrl(detailList);

                        specialList.add(indexGoodsVO);
                    }
                }

            }
        }

        List<IndexNoticeVO> notices = iMallCmsNoticeService.indexSelectMallCmsNoticeList(new MallCmsNotice());
        Map<String, Object> map = new HashMap<>();
        map.put("timeList",timeList);
        map.put("specialList",specialList);
        map.put("notices",notices);
        return ApiResult.ok().put("data",map);
    }

    /**
     * 首页轮播图列表
     * @return
     */
    @GetMapping("/getIndexBannerList")
    public ApiResult getIndexBannerList(){
        try {
            List<IndexBannerVO> banners = iMallBannerService.indexSelectMallBannerList(new MallBanner());
            if (banners.size() > 0){
                for (IndexBannerVO banner : banners) {
                    if ("3".equals(banner.getType())){
                        MallGoodsTag mallGoodsTag = mallGoodsTagService.selectMallGoodsTagById(banner.getResId().intValue());
                        if (ObjectUtils.isNotEmpty(mallGoodsTag)){
                            banner.setTagName(mallGoodsTag.getTagName());
                        }
                    }
                    if ("2".equals(banner.getType())){
                        MallGoodsClass goodsClass = mallGoodsClassService.selectMallGoodsClassById(banner.getResId());
                        if (goodsClass != null) {
                            banner.setGoodsClassOneId(goodsClass.getParentId());
                        }

                    }
                }
            }

            return ApiResult.ok().put("data",banners);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    @GetMapping("/getClassIndexBanner")
    public ApiResult getClassIndexBanner(){
        List<IndexBannerVO> banners = iMallBannerService.classIndexSelectMallBannerList(new MallBanner());
        return ApiResult.ok().put("data",banners);
    }

    /**
     * 小程序图片展示
     * @return
     */
    @GetMapping("/getXCXImgShow")
    public ApiResult getXCXImgShow(Long id){
        MallImgShow mallImgShow = iMallImgShowService.selectMallImgShowById(id);
        if (mallImgShow != null && mallImgShow.getStatus().equals("0")) {
            return ApiResult.ok().put("data",mallImgShow);
        }
        return ApiResult.ok();
    }
}
