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


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.*;
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.IFollowService;
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.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺中心-店铺 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/mall/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private IShopNavigationService shopNavigationService;

    @Autowired
    private IShopNavigationGoodsService shopNavigationGoodsService;

    @Autowired
    private IFollowService followService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IOprationBannerService oprationBannerService;

    @Autowired
    private IMarketingCouponService marketingCouponService;

    @Autowired
    private ICouponService couponService;

    @Autowired
    private ISpuService spuService;

    @ApiOperation(value = "参数 merchantId")
    @PostMapping("/without/getShop")
    public ResponseVO<Shop> getSkuDetail(@RequestBody JSONObject data) {
        String merchantId = data.getString("merchantId");
        Shop shop = shopService.getByMerchantId(merchantId);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, shop);
    }

    @ApiOperation(value = "获取店铺导航 参数 merchantId")
    @PostMapping("/without/getShopNavigation")
    public ResponseVO<List<ShopNavigation>> getShopNavigation(@RequestBody JSONObject data) {
        String merchantId = data.getString("merchantId");
        List<ShopNavigation> shopNavigations = shopNavigationService.getByMerchantId(merchantId);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, shopNavigations);
    }

    @ApiOperation(value = "获取店铺导航商品 paramA:merchantId,paramB:navigationId")
    @PostMapping("/without/getShopNavigationGoods")
    public ResponseVO<GoodsViewVo> getShopNavigationGoods(@RequestBody PageVoWithTwo pageVoWithTwo) {
        String merchantId = pageVoWithTwo.getParamA();
        String navigationId = pageVoWithTwo.getParamB();
        List<SaleHotVo> saleHotVos = shopNavigationGoodsService.getGoodsVo(pageVoWithTwo);
        Integer count = shopNavigationGoodsService.getCount(merchantId, navigationId);
        GoodsViewVo g = new GoodsViewVo();
        g.setList(saleHotVos);
        g.setTotal(count);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, g);
    }

    @ApiOperation(value = "店铺统计参数 merchantId")
    @PostMapping("/without/getShopCount")
    public ResponseVO<ShopCount> getShopCount(@RequestBody JSONObject data) {
        String merchantId = data.getString("merchantId");
        Integer follow = followService.getCountBymerchantId(merchantId);
        BigDecimal comment = commentService.getEvaluate(merchantId);
        ShopCount shopCount = new ShopCount();
        shopCount.setCommont(comment);
        shopCount.setFollow(follow);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, shopCount);
    }

    @ApiOperation(value = "店铺banner merchantId")
    @PostMapping("/without/getBanner")
    public ResponseVO<List<OprationBanner>> getBanner(@RequestBody JSONObject data) {
        String merchantId = data.getString("merchantId");
        List<OprationBanner> list = oprationBannerService.getList(merchantId);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, list);
    }

    @ApiOperation(value = "参数 merchantId")
    @PostMapping("/without/getCoupon")
    public ResponseVO<ShopCouponVo> getCoupon(@ApiIgnore @RequestAttribute Header header, @RequestBody JSONObject data) {
        String merchantId = data.getString("merchantId");
        List<UserCouponVo> userCouponVos = marketingCouponService.getByMerchantId(merchantId, header.getType());
        Integer count = marketingCouponService.getCount(merchantId, header.getType());
        if (StringUtil.isNullOrEmpty(header.getUid())) {

        } else {
            for (UserCouponVo u : userCouponVos) {
                if (StringUtil.isNullOrEmpty(header.getUid())) {

                } else {
                    Boolean b = couponService.checkHave(header.getUid(), u.getCouponId());
                    if (b) {
                        u.setUid(header.getUid());
                    }
                }
            }
        }
        ShopCouponVo shopCouponVo = new ShopCouponVo();
        shopCouponVo.setList(userCouponVos);
        shopCouponVo.setCount(count);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,shopCouponVo);
    }

    @ApiOperation(value = "参数 merchantId")
    @PostMapping("/without/getRecommend")
    public ResponseVO<List<SaleHotVo>> getCoupon(@RequestBody JSONObject data) {
        String merchantId = data.getString("merchantId");
        List<SaleHotVo> list = spuService.getByMerchantId(merchantId);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,list);
    }

    @ApiOperation(value = "商家所有商品 参数 merchantId")
    @PostMapping("/without/getAll")
    public ResponseVO<GoodsViewVo> getAll(@RequestBody PageVoWithOne pageVoWithOne) {
        String merchantId = pageVoWithOne.getParam();
        if(StringUtil.isNullOrEmpty(merchantId)||pageVoWithOne.getPage()==null||pageVoWithOne.getPage()<1||pageVoWithOne.getSize()==null||pageVoWithOne.getSize()<1){
            return new ResponseVO<GoodsViewVo>(MallResponseEnum.PARAMETER_ERR);
        }
        //String navigationId = pageVoWithTwo.getParamB();
        List<SaleHotVo> saleHotVos = spuService.getAllByMerchantId(pageVoWithOne);
        Integer count = spuService.getAllCountByMerchantId(merchantId);
        GoodsViewVo g = new GoodsViewVo();
        g.setList(saleHotVos);
        g.setTotal(count);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, g);
    }
}


