package com.yonge.cooleshow.portal.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.security.AuthUser;
import com.yonge.cooleshow.common.security.SecurityUtils;
import com.yonge.cooleshow.mall.common.api.CommonPage;
import com.yonge.cooleshow.mall.common.api.CommonResult;
import com.yonge.cooleshow.mbg.mapper.OmsCartItemMapper;
import com.yonge.cooleshow.mbg.model.OmsCartItem;
import com.yonge.cooleshow.mbg.model.OmsCartItemExample;
import com.yonge.cooleshow.mbg.model.PmsProduct;
import com.yonge.cooleshow.mbg.model.PmsSkuStock;
import com.yonge.cooleshow.portal.domain.PmsPortalProductDetail;
import com.yonge.cooleshow.portal.dto.ProductSearch;
import com.yonge.cooleshow.portal.dto.ProductSearchConditionVo;
import com.yonge.cooleshow.portal.service.PmsPortalProductService;
import com.yonge.cooleshow.portal.service.UmsMemberService;
import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * 前台商品管理Controller
 * Created by macro on 2020/4/6.
 */
@Controller
@Api(tags = "PmsPortalProductController 前台商品管理")
@RequestMapping("/product")
public class PmsPortalProductController {
    @Autowired
    private UmsMemberService umsMemberService;
    @Autowired
    private PmsPortalProductService portalProductService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private OmsCartItemMapper omsCartItemMapper;

    @ApiOperation(value = "综合搜索、筛选、排序")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<PmsProduct>> search(@RequestBody ProductSearch search) {
        List<PmsProduct> productList = portalProductService.search(search.getKeyword(),search.getBrandId(),search.getProductCategoryId(),
                       search.getProductAttributeCategoryId(), search.getPageNum(), search.getPageSize(), search.getSort());
        return CommonResult.success(CommonPage.restPage(productList));
    }

    @ApiOperation("获取商品列表下拉条件")
    @RequestMapping(value = "/search/condition", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProductSearchConditionVo> searchCondition() {
        ProductSearchConditionVo list = portalProductService.searchCondition();
        return CommonResult.success(list);
    }

    @ApiOperation("获取前台商品详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsPortalProductDetail> detail(@PathVariable Long id) {
        PmsPortalProductDetail productDetail = portalProductService.detail(id);
        return CommonResult.success(productDetail);
    }


    @ApiOperation("购物车数量")
    @RequestMapping(value = "/cart/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult cart(@PathVariable Long id) {
        try {
            OmsCartItemExample omsCartItemExample = new OmsCartItemExample();
            omsCartItemExample.createCriteria().andProductSkuIdEqualTo(id)
                              .andHiddenEqualTo(0)
                              .andDeleteStatusEqualTo(0)
                              .andMemberIdEqualTo(umsMemberService.getCurrentMember().getId());
            List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(omsCartItemExample);
            if (CollectionUtils.isEmpty(omsCartItems)) {
                return CommonResult.success(0);
            } else {
                return CommonResult.success(omsCartItems.get(0).getQuantity());

            }
        }catch (Exception e) {
            return CommonResult.success(0);
        }
    }


}
