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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.goods.model.PreSale;
import com.fzm.mall.server.front.goods.model.SkuProp;
import com.fzm.mall.server.front.goods.model.vo.*;
import com.fzm.mall.server.front.goods.service.*;
import com.fzm.mall.server.front.user.model.Header;
import com.fzm.mall.server.front.user.service.ICouponService;
import com.fzm.mall.server.front.user.service.IRedisService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RestController
@RequestMapping("mall/preSale")
public class PreSaleController {

    @Autowired
    private IRedisService redisService;

    @Autowired
    private ISkuPropService iSkuPropService;

    @Autowired
    private ISpuService iSpuService;

    @Autowired
    private IGoodCommentService goodCommentService;

    @Autowired
    private ICouponService couponService;

    @Autowired
    private IMarketingPromotionService marketingPromotionService;

    @Autowired
    private ISkuService iSkuService;

    @Autowired
    private IPreSaleService preSaleService;

    //平台-商户号
    public static final String PLATFORM_MERCHANT_ID = "e3ac4b4a4e65460dbd9f1ec6cbd7a900";

    @ApiOperation(value = "商品详情 参数：goodsId")
    @PostMapping("/without/getDetail")
    public ResponseVO<PreSaleGoodsVo> getdetail(@ApiIgnore @RequestAttribute Header header, @RequestBody JSONObject data) {
        String uid = header.getUid();
        Integer type = header.getType();
        String goodsId = data.getString("goodsId");
        GoodsVo goodsVo = iSpuService.getByGoodId(goodsId);
        if (goodsVo == null) {
            return new ResponseVO<>(MallResponseEnum.GOOD_NOT_EXIST);
        }
        BigDecimal comment = goodCommentService.getScope(goodsVo.getMerchantId());
        goodsVo.setComment(comment==null?BigDecimal.ZERO:comment.setScale(2, RoundingMode.HALF_UP));
        goodsVo.setImgListJson(JSONArray.parseArray(goodsVo.getImgList(), String.class));
        //优惠券
        List<UserCouponVo> userCouponVos = couponService.getCouponByGoodsId(uid, goodsId, type, PLATFORM_MERCHANT_ID, goodsVo.getMerchantId());
        System.err.println(header.getUid());
        for (UserCouponVo u : userCouponVos) {
            if (StringUtil.isNullOrEmpty(header.getUid())) {

            } else {
                Boolean b = couponService.checkHave(header.getUid(), u.getCouponId());
                if (b) {
                    u.setUid(uid);
                }
            }
        }
        goodsVo.setMarketingCoupons(userCouponVos);
        //促销活动
        PromotionVo byGoodsId = marketingPromotionService.getByGoodsId(goodsId, type,goodsVo.getMerchantId());
        goodsVo.setPromotionVo(byGoodsId);
        List<SkuVo> skus = iSkuService.getByGoodsId(goodsId);
        System.out.println(skus.size());
        for (SkuVo skuVo : skus) {
            System.out.println(skuVo.getSkuId());
            List<SkuProp> skuProps = iSkuPropService.getBySkuId(skuVo.getSkuId());
            skuVo.setSkuProps(skuProps);

        }
        goodsVo.setSkus(skus);
        PreSaleGoodsVo preSaleGoodsVo = new PreSaleGoodsVo();
        preSaleGoodsVo.setGoodsVo(goodsVo);
        PreSale preSale = preSaleService.getByGoodsId(goodsVo.getGoodsId());
        preSaleGoodsVo.setPreSale(preSale);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, preSaleGoodsVo);
    }

}
