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


import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.asset.service.IUserAssetService;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.constant.cache.GoodCache;
import com.fzm.mall.server.front.goods.model.Comment;
import com.fzm.mall.server.front.goods.model.CommentAppend;
import com.fzm.mall.server.front.goods.model.GoodsComment;
import com.fzm.mall.server.front.goods.model.Shop;
import com.fzm.mall.server.front.goods.model.vo.*;
import com.fzm.mall.server.front.goods.service.*;
import com.fzm.mall.server.front.order.model.OrderDetail;
import com.fzm.mall.server.front.order.model.vo.ShopInfoVo;
import com.fzm.mall.server.front.order.service.IOrderDetailService;
import com.fzm.mall.server.front.redis.OrderRedis;
import com.fzm.mall.server.front.user.model.Header;
import com.fzm.mall.server.front.user.model.MarketingMemberIntegralRecurrence;
import com.fzm.mall.server.front.user.model.Member;
import com.fzm.mall.server.front.user.service.IMarketingMemberIntegralRecurrenceService;
import com.fzm.mall.server.front.user.service.IMemberService;
import com.fzm.mall.server.front.user.service.IRedisService;
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-04-27
 */
@RestController
@RequestMapping("/mall/comment")
public class CommentController {

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IGoodCommentService goodCommentService;

    @Autowired
    private ISkuService skuService;

    @Autowired
    private ICommentAppendService commentAppendService;

    @Autowired
    private IRedisService redisService;
    @Autowired
    private OrderRedis orderRedis;
    
    @Autowired
    private ISpuService spuService;

    @Autowired
    private IShopService shopService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IMarketingMemberIntegralRecurrenceService iMarketingMemberIntegralRecurrenceService;


    @Autowired
    private IUserAssetService userAssetService;

    @ApiOperation(value = "订单评价")
    @PostMapping("/addComment")
    public ResponseVO<String> addComment(@ApiIgnore @RequestAttribute Header header, @RequestBody OrderCommentVo orderCommentVo){
        int i = orderDetailService.updateCommend(header.getUid(),orderCommentVo.getOid(),orderCommentVo.getSkuId());
        if(i != 1){
           return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        orderRedis.delListOrderDetail(orderCommentVo.getOid());
        SkuVo skuVo = skuService.getBySkuId(orderCommentVo.getSkuId());
        String commentId = goodCommentService.addComment(header.getUid(),orderCommentVo,skuVo.getGoodsId(),skuVo.getMerchantId());
        return new ResponseVO<>(MallResponseEnum.SUCCESS,commentId);
    }
    @ApiOperation(value = "订单追评")
    @PostMapping("/addCommentAppend")
    public ResponseVO<List<String>> addCommentAppend(@ApiIgnore @RequestAttribute Header header, @RequestBody CommentAppendVo commentAppendVo){
        if(commentAppendVo.getContent().length()>200){
            return new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        int i = orderDetailService.updateCommendAppend(commentAppendVo.getCommentId());
        if(i != 1){
            return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        String oid = goodCommentService.getOidByCommentId(commentAppendVo.getCommentId());
        orderRedis.delListOrderDetail(oid);
        commentAppendService.addCommentAppend(commentAppendVo);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS);
    }

    @ApiOperation(value = "评价列表 param：goodsId")
    @PostMapping("/without/ListComment")
    public ResponseVO<CommentViewList> ListComment(@ApiIgnore @RequestAttribute Header header, @RequestBody PageVoWithOne pageVoWithOne){
        String goodsId = pageVoWithOne.getParam();
        GoodsVo byGoodId = spuService.getByGoodId(goodsId);
        Shop shop = shopService.getByMerchantId(byGoodId.getMerchantId());
        CommentViewList commentViewList = new CommentViewList();
        commentViewList.setShopUrl(shop.getShopImage());
        commentViewList.setShopName(shop.getShopName());
        Integer count = goodCommentService.getCount(goodsId);
        commentViewList.setCount(count);
        List<CommentView> list = new ArrayList<>();
        List<CommentVo> commentVos  = goodCommentService.getByGoodsId(goodsId,pageVoWithOne);
        System.err.println("size"+commentVos.size());
        for (CommentVo vo:commentVos) {
            CommentView commentView = new CommentView();
            if(vo.getAnonymous()==1){
                vo.setNickName("匿名");
            }
            CommentAppend commentAppend =  commentAppendService.getByCommentId(vo.getCommentId());
            commentView.setCommentVo(vo);
            commentView.setCommentAppend(commentAppend);
            list.add(commentView);
        }
        commentViewList.setList(list);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,commentViewList);
    }

    @ApiOperation(value = "评价详情 param：commentId")
    @PostMapping("/without/commentDetail")
    public ResponseVO<CommentViewList> ListComment(@ApiIgnore @RequestAttribute Header header, @RequestBody JSONObject data){
        String commentId = data.getString("commentId");
        GoodsComment comment = goodCommentService.getByCommentId(commentId);
        if(comment == null){
            return new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        String goodsId = skuService.getGoodsIdByskuId(comment.getSkuId());
        GoodsVo byGoodId = spuService.getByGoodId(goodsId);
        Shop shop = shopService.getByMerchantId(byGoodId.getMerchantId());
        CommentViewList commentViewList = new CommentViewList();
        commentViewList.setShopUrl(shop.getShopImage());
        commentViewList.setShopName(shop.getShopName());
        List<CommentVo> commentVos  = goodCommentService.getByGoodsIdByCommentId(commentId);
        List<CommentView> list = new ArrayList<>();
        System.err.println("size"+commentVos.size());
        for (CommentVo vo:commentVos) {
            CommentView commentView = new CommentView();
            if(vo.getAnonymous()==1){
                vo.setNickName("匿名");
            }
            CommentAppend commentAppend =  commentAppendService.getByCommentId(vo.getCommentId());
            commentView.setCommentVo(vo);
            commentView.setCommentAppend(commentAppend);
            list.add(commentView);
        }
        commentViewList.setList(list);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,commentViewList);
    }


    @ApiOperation(value = "评价积分领取 参数：commentId")
    @PostMapping("/getReward")
    public ResponseVO<String> getReward(@ApiIgnore @RequestAttribute Header header, @RequestBody JSONObject data){
        String commentId = data.getString("commentId");
        String uid = header.getUid();
        GoodsComment byCommentId = goodCommentService.getByCommentId(commentId);
        if(!byCommentId.getUid().equals(uid)){
            return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        if(!redisService.setNx(uid+commentId+"_getReward")){
           return new ResponseVO<>(MallResponseEnum.OPERATION_OFTEN);
        }
        int level = 1;
        Member member = memberService.getByUid(uid);
        if(member != null){
            level = member.getMemberLevel();
        }
        MarketingMemberIntegralRecurrence byLevel = iMarketingMemberIntegralRecurrenceService.getByLevel(level);
        BigDecimal evaluationRefund = byLevel.getEvaluationRefund();
        if(evaluationRefund.compareTo(BigDecimal.ZERO)==1){
            int i = userAssetService.addReward(uid,commentId,evaluationRefund);
            if(i != 1){
                return new ResponseVO<>(MallResponseEnum.AREADLY_REWARD_COMMENT);
            }
        }
        return  new ResponseVO<>(MallResponseEnum.SUCCESS);
    }
}
