package com.gree.mall.manager.controller.order;


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.es.ESGoodsVO;
import com.gree.mall.manager.bean.goods.GoodsPurchaseVO;
import com.gree.mall.manager.bean.order.OrderDeliveryDetailVO;
import com.gree.mall.manager.bean.order.OrderDeliveryVO;
import com.gree.mall.manager.bean.order.SendOrderBody;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.order.OrderDeliveryLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(value = "订单发货api", tags = {"订单发货api"})
@RequestMapping(value = "/orderDelivery", produces = "application/json; charset=utf-8")
public class OrderDeliveryController {


    @Autowired
    OrderDeliveryLogic orderDeliveryLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "发货单-列表")
    public ResponseHelper<Page<OrderDeliveryVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<OrderDeliveryVO> page = orderDeliveryLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OrderDeliveryVO>() {});
    }


    @PostMapping("/list/export")
    @ApiOperation("发货单-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<OrderDeliveryVO> baseVOIPage = orderDeliveryLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }




    @PostMapping("/goods/deliverV2")
    @ApiOperation("发货")
    public ResponseHelper deliverV2(
            @RequestBody SendOrderBody sendOrderBody
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + sendOrderBody.getOrderId());
        if(!obtain.tryLock(10,TimeUnit.SECONDS)){
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            orderDeliveryLogic.deliverV2(sendOrderBody);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/refundBatch")
    @ApiOperation("批量退货")
    public ResponseHelper refundBatch(
            @ApiParam(value = "发货id", required = true) @RequestParam(required = true) List<String> deliveryId
    ) throws Exception {


        List<String> collect = deliveryId.stream().distinct().collect(Collectors.toList());


        for (String s : collect) {
            orderDeliveryLogic.refundBatch(s);
        }


        return ResponseHelper.success();
    }


    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<OrderDeliveryDetailVO> detail(
            @ApiParam(value = "发货id", required = true) @RequestParam(required = true) String deliveryId
    ) throws Exception {

        OrderDeliveryDetailVO orderDeliveryDetailVO= orderDeliveryLogic.detail(deliveryId);

        return ResponseHelper.success(orderDeliveryDetailVO);
    }

}
