package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.vo.GoodsProductVO;
import com.ruoyi.mall.domain.vo.IndexGoodsVO;
import com.ruoyi.mall.service.*;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ApiGoodsController
 * @Description 请描述类的业务用途
 * @Author linlinee
 * @Date 2021/11/29 10:17 AM
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/Goods")
@ResponseBody
public class ApiGoodsController {
    @Autowired
    private IMallImgShowService mallImgShowService;
    @Autowired
    IMallGoodsClassService classService;

    @Autowired
    IMallGoodsGoodsService goodsService;

    @Autowired
    IMallGoodsProductService productService;

    @Autowired
    IMallMemberUserService userService;

    @Autowired
    IMallOrderCommentService orderCommentService;

    @Autowired
    IMallImgService imgService;
    @Autowired
    private IMallAppGoodsService mallAppGoodsService;

    @Autowired
    private IMallGoodsTagService mallGoodsTagService;

    @Autowired
    private IMallMemberCartService mallMemberCartService;
    @Autowired
    private IMallGoodsGoodsService mallGoodsGoodsService;
    /**
     * 商品列表修改数量
     *
     * @param param
     * @return
     */
    @PostMapping("/goodsListEditCount")
    public ApiResult goodsListEditCount(@RequestBody Map<String, Object> param) {
        if (!param.containsKey("userId") || MapUtils.getLong(param, "userId") == null) {
            return ApiResult.error("缺少userId");
        }
        if (!param.containsKey("goodsId") || MapUtils.getLong(param, "goodsId") == null) {
            return ApiResult.error("缺少goodsId");
        }
        if (!param.containsKey("productId") || MapUtils.getLong(param, "productId") == null) {
            return ApiResult.error("缺少productId");
        }
        if (!param.containsKey("type") || MapUtils.getLong(param, "type") == null) {
            return ApiResult.error("缺少type");
        }
        if (!param.containsKey("count") || MapUtils.getLong(param, "count") == null) {
            return ApiResult.error("缺少count");
        }
        try {

            return mallMemberCartService.goodsListEditCount(param);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }

    }

    /**
     * 首页商品专区
     *
     * @return
     */
    @GetMapping("/getGoodsTags")
    public ApiResult getGoodsTags() {
        try {
            return ApiResult.ok().put("data", mallGoodsTagService.getGoodsTags());
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 用户常用清单
     *
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/commonGoodsList")
    public ApiResult commonGoodsList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize
            , @RequestParam(value = "userId") String userId
    ) {
        PageInfo<IndexGoodsVO> pageInfo = goodsService.commonGoodsList(pageNum, pageSize, userId);
        return ApiResult.ok().put("data", pageInfo);
    }

    /**
     * 根据条件查商品列表
     *
     * @param pageNum
     * @param pageSize
     * @param goodsModel
     * @param userId
     * @return
     */
    @GetMapping("/getGoodsByParams")
    public ApiResult getGoodsByParams(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize, MallGoodsGoodsDTO goodsModel, String userId) {
        goodsModel.setSortType(5);
//        SysUser user = SecurityUtils.getLoginUser().getUser();
//        userId=user.getUserId().toString();
        PageInfo<IndexGoodsVO> pageInfo = goodsService.selectMallGoodsGoodsListByPage(pageNum, pageSize, goodsModel, userId);
        return ApiResult.ok().put("data", pageInfo);
    }
    @GetMapping("/allGoods")
    public ApiResult allGoods(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum
            , @RequestParam(value = "pageSize", defaultValue = "20", required = false) Integer pageSize,String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<MallAppGoods> list = mallAppGoodsService.selectMallAppGoodsListByname(name);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
        return ApiResult.ok().put("data", pageInfo);
    }
    /**
     * 获取所有分类
     *
     * @param param
     * @return
     */
    @GetMapping("/getClasses")
    public ApiResult getGoodsClasses(@RequestParam Map<String, Object> param) {
        List<MallGoodsClass> list = classService.selectDefaultMallGoodsClasses(MapUtils.getLong(param, "parentId"));
        return ApiResult.ok().put("data", list);
    }

    /**
     * 根据id查商品
     *
     * @param params
     * @return
     */
    @GetMapping("/getGoodById")
    public ApiResult getGoodById(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("goodsId")
        ) {
            return ApiResult.error("参数异常");
        }

        if (params.containsKey("userId")) {
            //添加足迹
            userService.insertIntoFoodMarkBD(params);
        }

        IndexGoodsVO good = goodsService.selectMallGoodsGoodsByIdVO(params);
        if (good != null) {
            return ApiResult.ok().put("data", good);
        } else {
            return ApiResult.error("查无数据").put("data", "");
        }
    }

    /**
     * 根据商品id 查GoodProduct
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/getGoodMsgByParams")
    public ApiResult getGoodMsgByParams(String goodsId) {
        MallGoodsProduct pro = new MallGoodsProduct();
        pro.setGoodsId(Long.valueOf(goodsId));
        List<GoodsProductVO> list = productService.selectMallGoodProByParams(pro);
        return ApiResult.ok().put("data", list);
    }

    /**
     * 根据商品id 查评价
     *
     * @param pageNum
     * @param pageSize
     * @param params
     * @return
     */
    @GetMapping("/getCommentList")
    public ApiResult getCommentList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize, @RequestParam Map<String, Object> params) {
        if (!params.containsKey("goodsId")
        ) {
            return ApiResult.error("参数异常");
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = orderCommentService.selectCommentListByGoodsId(Long.valueOf(params.get("goodsId").toString()));
        for (Map<String, Object> map : list) {
            String commentId = map.get("id").toString();
            MallImg img = new MallImg();
            img.setType("2");
            img.setResId(Long.parseLong(commentId));
            List<String> imgList = imgService.selectMallImgUrlVoList(img);
            map.put("urls", imgList);
        }
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
        return ApiResult.ok().put("data", pageInfo);
    }

    /**
     * 查询小程序图片展示列表
     */
   
    @GetMapping("/list")
    public ApiResult list(MallImgShow mallImgShow) {
        mallImgShow.setDelFlag("0");
        List<MallImgShow> list = mallImgShowService.selectMallImgShowList(mallImgShow);
      return   ApiResult.ok().put("data", list);
    }
}
