package com.yiqi.api.controller.common;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.app.domain.AppGoodsCollect;
import com.yiqi.app.service.IAppGoodsCollectService;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.SysBoolType;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.core.domain.*;
import com.yiqi.core.domain.dto.weapp.WeAppGoodsInfoQueryDTO;
import com.yiqi.core.domain.vo.weapp.WeAppGoodsInfoQueryVO;
import com.yiqi.core.domain.vo.weapp.WeAppGoodsInfoVO;
import com.yiqi.core.domain.vo.weapp.WeAppGoodsSkuVO;
import com.yiqi.core.mapper.GoodsSpecMapper;
import com.yiqi.core.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiaohei
 * @date 2023/10/4
 */
@Api(tags = "商品")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/goods/info")
public class WeAppGoodsInfoController {

    @Autowired
    private IGoodsInfoService goodsInfoService;

    @Autowired
    private IGoodsSkuService goodsSkuService;

    @Autowired
    IGoodsSpecService goodsSpecService;

    @Autowired
    private IGoodsSkuSpecService goodsSkuSpecService;

    @Autowired
    private IGoodsSkuStoreService goodsSkuStoreService;

    @Autowired
    private IAppGoodsCollectService appGoodsCollectService;

    @ApiOperation("分页获取商品列表")
    @PostMapping(value = "findPage")
    public R<List<WeAppGoodsInfoQueryVO>> findPage(@RequestBody @Valid WeAppGoodsInfoQueryDTO weAppGoodsInfoQueryDTO) throws Exception {
        return R.ok(goodsInfoService.findPage(weAppGoodsInfoQueryDTO));
    }

    @ApiOperation("获取商品详情")
    @GetMapping(value = "getinfo")
    public R<WeAppGoodsInfoVO> getinfo(@RequestParam @ApiParam("商品ID") long goodsId) throws Exception {
        GoodsInfo goodsInfo = goodsInfoService.getOne(new QueryWrapper<GoodsInfo>().lambda().eq(GoodsInfo::getId, goodsId), false);
        if (goodsInfo == null) {
            throw new ServiceException("未查询到商品信息");
        }
        List<GoodsSku> goodsSkus = goodsSkuService.list(new QueryWrapper<GoodsSku>().lambda()
                .eq(GoodsSku::getGoodsId, goodsId)
                .eq(GoodsSku::getDelFlag, DeleteStatus.OK.getCode()));
        WeAppGoodsInfoVO weAppGoodsInfoVO = new WeAppGoodsInfoVO();
        BeanUtils.copyProperties(goodsInfo, weAppGoodsInfoVO);
        List<WeAppGoodsSkuVO> weAppGoodsSkuVOS = new ArrayList<>();
        for (GoodsSku sku : goodsSkus) {
            WeAppGoodsSkuVO weAppGoodsSkuVO = new WeAppGoodsSkuVO();
            weAppGoodsSkuVO.setSkuId(sku.getId());
            weAppGoodsSkuVO.setStock(sku.getStock());
            weAppGoodsSkuVO.setSalePrice(sku.getSalePrice());
            // 设置规格值
            //values字符串数组转List
            if (StringUtils.isNotBlank(sku.getSpecs())) {
                weAppGoodsSkuVO.setSpecValLists(JSONUtil.toList(JSONUtil.parseArray(sku.getSpecs()), String.class));
            }
            weAppGoodsSkuVOS.add(weAppGoodsSkuVO);
        }
        List<GoodsSpec> goodsSpecList = goodsSpecService.selectByGoodsId(goodsId);
        weAppGoodsInfoVO.setSpecList(goodsSpecList);
        weAppGoodsInfoVO.setWeAppGoodsSkuVOS(weAppGoodsSkuVOS);
        AppGoodsCollect appGoodsCollect = appGoodsCollectService.getOne(new QueryWrapper<AppGoodsCollect>().lambda()
                .eq(AppGoodsCollect::getAppUserId, AuthHolder.userId())
                .eq(AppGoodsCollect::getGoodsId, weAppGoodsInfoVO.getId()), false);
        weAppGoodsInfoVO.setIsCollect(appGoodsCollect == null ? SysBoolType.NO.getCode() : SysBoolType.YES.getCode());
        return R.ok(weAppGoodsInfoVO);
    }

}
