package com.gree.mall.miniapp.controller.es;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.es.ESGoodsBean;
import com.gree.mall.miniapp.bean.es.ESGoodsOtherCountBean;
import com.gree.mall.miniapp.bean.es.GuessBean;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.enums.es.ESGoodsStatusEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.es.ESGoodsLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@Validated
@RestController
@Api(value = "二手商品API", tags ={"二手商品API"} )
@RequestMapping(value = "/es/goods", produces = "application/json; charset=utf-8")
public class ESGoodsController {

    @Resource
    ESGoodsLogic esGoodsLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @PostMapping("/list")
    @ApiOperation(value = "二手商品-列表")
    public ResponseHelper<IPage<ESGoodsBean>> list(
            @ApiParam(value = "排序类型 A=默认 B=在售商品 C=人气最高") @RequestParam(required = false) String sortType,
            @ApiParam(value = "商品名称") @RequestParam(required = false) String goodsName,
            @ApiParam(value = "商品分类") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId,
            @ApiParam(value = "品牌") @RequestParam(required = false) String brand,
            @ApiParam(value = "功率") @RequestParam(required = false) String power,
            @ApiParam(value = "能效") @RequestParam(required = false) String mark,
            @ApiParam(value = "状态 ON=上架 OFF=下架 SALE=已卖出") @RequestParam(required = false) List<String> status,
            @ApiParam(value = "页号") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "页大小") @RequestParam(required = false, defaultValue = "-1")  Integer pageSize
    ) {
        IPage<ESGoodsBean> page = esGoodsLogic.list(pageNum, pageSize, sortType, goodsName, categoryId, userId, brand,
                power, mark, status);
        return ResponseHelper.success(page);
    }

    @PostMapping("/top/list")
    @ApiOperation(value = "二手商品-置顶列表")
    public ResponseHelper<List<ESGoodsBean>> topList() {
        List<ESGoodsBean> page = esGoodsLogic.topList();
        return ResponseHelper.success(page);
    }

    @PostMapping("/guess/list")
    @ApiOperation(value = "二手商品-猜你喜欢")
    public ResponseHelper<GuessBean> guessList() {
        GuessBean bean = esGoodsLogic.guessList();
        return ResponseHelper.success(bean);
    }

    @PostMapping("/other/count")
    @ApiOperation(value = "二手商品-其他统计")
    public ResponseHelper<ESGoodsOtherCountBean> otherCount(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        ESGoodsOtherCountBean bean = esGoodsLogic.otherCount(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "二手商品-详情")
    public ResponseHelper<ESGoodsBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.ES_GOODS_LOCK + ":" + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            ESGoodsBean bean = esGoodsLogic.detail(id);
            return ResponseHelper.success(bean);
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "二手商品-新增")
    public ResponseHelper add(
            HttpServletRequest request,
            @RequestBody ESGoodsBean bean
    ) throws Exception {
        esGoodsLogic.add(request, bean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "二手商品-编辑")
    public ResponseHelper update(
            HttpServletRequest request,
            @RequestBody ESGoodsBean bean
    ) throws Exception {
        esGoodsLogic.update(request, bean);
        return ResponseHelper.success();
    }

    @PostMapping("/copy")
    @ApiOperation(value = "二手商品-克隆")
    public ResponseHelper<String> copy(
            @ApiParam(value = "商品id", required = true) @RequestParam String id
    ) throws Exception {
        String newId = esGoodsLogic.copy(id);
        return ResponseHelper.success(newId);
    }

    @PostMapping("/del")
    @ApiOperation(value = "二手商品-删除")
    public ResponseHelper del(
            @ApiParam(value = "商品id", required = true) @RequestParam String id
    ) throws Exception {
        esGoodsLogic.del(id);
        return ResponseHelper.success();
    }

    @PostMapping("/praise")
    @ApiOperation(value = "二手商品-点赞")
    public ResponseHelper<ESGoodsBean> praise(
            @ApiParam(value = "商品id", required = true) @RequestParam String id,
            @ApiParam(value = "true=是 false=否", required = true) @RequestParam Boolean isPraise
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.ES_GOODS_LOCK + ":p" + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            ESGoodsBean bean = esGoodsLogic.praise(id, isPraise);
            return ResponseHelper.success(bean);
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/message")
    @ApiOperation(value = "二手商品-留言")
    public ResponseHelper<ESGoodsBean> message(
            @ApiParam(value = "商品id", required = true) @RequestParam String id,
            @ApiParam(value = "留言id") @RequestParam String messageId,
            @ApiParam(value = "内容", required = true) @NotBlank(message = "留言不能为空") @RequestParam String content
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.ES_GOODS_LOCK + ":m" + id + messageId);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            esGoodsLogic.message(id, messageId, content);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/update/status")
    @ApiOperation(value = "二手商品-操作")
    public ResponseHelper<ESGoodsBean> updateStatus(
            @ApiParam(value = "商品id", required = true) @RequestParam String id,
            @ApiParam(value = "ON=上架 OFF=下架 SALE=已卖出", required = true) @RequestParam ESGoodsStatusEnum status
    ) throws Exception {
        esGoodsLogic.updateStatus(id, status);
        return ResponseHelper.success();
    }

}
