package com.fzm.mall.server.front.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.asset.model.vo.good.GoodSkuVO;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.goods.mapper.GoodSkuMapper;
import com.fzm.mall.server.front.goods.model.GoodSkuProp;
import com.fzm.mall.server.front.goods.model.vo.GoodsVo;
import com.fzm.mall.server.front.goods.model.vo.PromotionVo;
import com.fzm.mall.server.front.goods.service.IGoodSkuPropService;
import com.fzm.mall.server.front.goods.service.IMarketingPromotionService;
import com.fzm.mall.server.front.goods.service.ISpuService;
import com.fzm.mall.server.front.shopping.model.UserShoppingCart;
import com.fzm.mall.server.front.shopping.model.dto.ShoppingCartDto;
import com.fzm.mall.server.front.shopping.service.IUserShoppingCartService;
import com.fzm.mall.server.front.user.model.Header;
import com.fzm.mall.server.front.util.IdUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

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

/**
 * @author tdz
 * @date 2021/03/22 11:26
 */
@Api(tags = "购物车接口", description = "用户购物")
@Slf4j
@RestController
@RequestMapping("/mall/shoppingCart")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserShoppingCartController {
    private final IUserShoppingCartService shoppingCartService;
    private final IGoodSkuPropService goodSkuPropService;
    private final GoodSkuMapper goodSkuMapper;
    private final IMarketingPromotionService marketingPromotionService;
    private final IUserShoppingCartService userShoppingCartService;
    private final ISpuService iSpuService;


    /**
     * 新增或修改购物车
     *
     * @param
     * @param userShoppingCart
     */
    @ApiOperation(value = "新增或修改购物车")
    @PostMapping("/addUserShoppingCart")
    public ResponseVO<String> saveOrUpdateShoppingCart(@ApiIgnore @RequestAttribute Header header, @RequestBody ShoppingCartDto userShoppingCart) {
        String uid = header.getUid();
        if (StringUtil.isNullOrEmpty(uid)) {
            return new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        if (StringUtil.isNullOrEmpty(userShoppingCart.getSkuId())) {
            return new ResponseVO<>(MallResponseEnum.SKUID_NULL);
        }
        if (userShoppingCart.getIsActive() == null) {
            userShoppingCart.setIsActive(1);
        }
        String id = userShoppingCart.getId();
        //修改的购物车信息必须存在
        if (!StringUtil.isNullOrEmpty(id)) {
            UserShoppingCart shopCart = userShoppingCartService.getShopById(id);
            if (shopCart == null) {
                return new ResponseVO<>(MallResponseEnum.MODIFY_SHOPCART_NULL);
            }
        }
        int i = shoppingCartService.saveOrUpdateShoppingCart(uid, userShoppingCart);
        if (i < 1) {
            return new ResponseVO<>(MallResponseEnum.DB_ERROR);
        }
        return new ResponseVO<>(MallResponseEnum.SUCCESS, null);
    }

    /**
     * 查询用户购物车列表
     *
     * @param
     * @return
     * @author tdz
     */
    @ApiOperation(value = "查询用户购物车列表")
    @GetMapping("/getShoppingCartList")
    public ResponseVO<JSONObject> getShoppingCartList(@ApiIgnore @RequestAttribute Header header) {
        String uid = header.getUid();
        List<UserShoppingCart> shopList = shoppingCartService.getList(uid);
        List<ShoppingCartDto> shopListFinal = new ArrayList<>();
        for (UserShoppingCart i : shopList) {
            ShoppingCartDto a = new ShoppingCartDto();
            BeanUtils.copyProperties(i, a);
            GoodSkuVO goodSkuVo = goodSkuMapper.getGoodSkuVo(i.getSkuId());
            if (goodSkuVo != null) {
                //BigDecimal originalPrice = new BigDecimal(goodSkuVo.getOriginalPrice());
                //BigDecimal price = marketingPromotionService.getPrice(goodsId, originalPrice.multiply(new BigDecimal(number)), number, header.getType());
                a.setAmount(new BigDecimal(goodSkuVo.getOriginalPrice()));
                List<GoodSkuProp> list = goodSkuPropService.getList(goodSkuVo.getSkuId());
                a.setGoodsName(goodSkuVo.getGoodsName());
                a.setPicture(goodSkuVo.getPicture());
                a.setStock(goodSkuVo.getStock());
                a.setGoodsId(goodSkuVo.getGoodsId());
                a.setCoinName(goodSkuVo.getCoinName());
                a.setEnterpriseName(goodSkuVo.getEnterpriseName());
                a.setMerchantId(goodSkuVo.getMerchantId());
                a.setDescription(goodSkuVo.getDescription());
                a.setWeight(goodSkuVo.getWeight());
                if (!CollectionUtils.isEmpty(list)) {
                    a.setGoodSkuProp(list);
                }
            }
            shopListFinal.add(a);
        }
        //Map<String, List<ShoppingCartDto>> map = shopListFinal.stream().collect(Collectors.groupingBy(ShoppingCartDto::getEnterpriseName));
       /* Map<String, Integer> numMap = shopListFinal.stream().collect(Collectors.groupingBy(ShoppingCartDto::getSkuId, Collectors.summingInt(ShoppingCartDto::getNumber)));
        List<ShoppingCartDto> arrayFinal = new ArrayList<>();
        Map<String, List<ShoppingCartDto>> collectMap = shopListFinal.stream().collect(Collectors.groupingBy(ShoppingCartDto::getSkuId));
        for (String key : numMap.keySet()) {
            ShoppingCartDto shoppingCartDto = collectMap.get(key).get(0);
            shoppingCartDto.setNumber(numMap.get(key));
            arrayFinal.add(shoppingCartDto);
        }*/
        JSONObject data = new JSONObject();
        data.put("result", shopListFinal);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, data);
    }

    /**
     * 删除用户购物车列表支持批量操作
     *
     * @param
     * @return
     * @author tdz
     */
    @ApiOperation(value = "删除用户购物车列表支持批量操作")
    @PostMapping("/delShoppingCartList")
    public ResponseVO<String> delShoppingCartList(@ApiIgnore @RequestAttribute Header header, @RequestBody List<String> skuIds) {
        String uid = header.getUid();
        if (CollectionUtils.isEmpty(skuIds)) {
            return new ResponseVO<>(MallResponseEnum.ORDERNO_NULL_ERROR);
        }
        String skuIdsStr = IdUtil.listToString(skuIds, ',');
        //String substring = skuIdsStr.substring(1, skuIdsStr.length() - 1);
        int i = shoppingCartService.delList(uid, skuIdsStr);
        if (i < 0) {
            return new ResponseVO<>(MallResponseEnum.DB_ERROR);
        }
        return new ResponseVO<>(MallResponseEnum.SUCCESS);
    }
}
