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


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.PayDetail;
import com.gree.mall.manager.bean.listvo.OrderInfoVO;
import com.gree.mall.manager.bean.order.OrderInfoAddBean;
import com.gree.mall.manager.bean.order.OrderInfoDetailBean;
import com.gree.mall.manager.bean.order.refund.OrderOfflineRefundAddBean;
import com.gree.mall.manager.bean.order.refund.OrderOfflineRefundDetailBean;
import com.gree.mall.manager.bean.order.refund.OrderOfflineRefundVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.order.OrderOfflineLogic;
import com.gree.mall.manager.logic.order.OrderOfflineRefundLogic;
import com.gree.mall.manager.plus.entity.OrderDetailCode;
import com.gree.mall.manager.utils.IpUtil;
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 javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "线下销售单退货管理", tags = {"线下销售单退货管理"})
@RequestMapping(value = "/order/offline/refund", produces = "application/json; charset=utf-8")
public class OrderOfflineRefundController {

    @Autowired
    OrderOfflineRefundLogic orderOfflineRefundLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<OrderOfflineRefundVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<OrderOfflineRefundVO> page = orderOfflineRefundLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OrderOfflineRefundVO>() {});
    }

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


    @PostMapping("/save")
    @ApiOperation("保存(传id则编辑)")
    public ResponseHelper addOffline(
            @ApiParam(required = true)@Valid @RequestBody OrderOfflineRefundAddBean bean
    ) throws RemoteServiceException, InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + bean.getOrderId());
        if(!obtain.tryLock(10, TimeUnit.SECONDS)){
            throw new RemoteServiceException("系统繁忙，请勿操作太频繁");
        }
        try {
            orderOfflineRefundLogic.save(bean);
            return ResponseHelper.success();
        }finally {
            obtain.unlock();
        }
    }

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<OrderOfflineRefundDetailBean> detail(
            @RequestParam String id
    ){
        OrderOfflineRefundDetailBean detail = orderOfflineRefundLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/examine")
    @ApiOperation("审核")
    public ResponseHelper examine(
            @RequestParam String id,
            @RequestParam ExamineStatusEnum examineStatus,
            @RequestParam(required = false) String examineRemark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + id);
        if(!obtain.tryLock(10, TimeUnit.SECONDS)){
            throw new RemoteServiceException("系统繁忙，请勿操作太频繁");
        }
        try {
            orderOfflineRefundLogic.examine(id, examineStatus,examineRemark);
            return ResponseHelper.success();
        }finally {
            obtain.unlock();
        }
    }


    @PostMapping("/code/list")
    @ApiOperation("订单条码")
    public ResponseHelper<List<OrderDetailCode>> codeList(
            @RequestParam String deliveryId
    ){
        List<OrderDetailCode> orderDetailCodes = orderOfflineRefundLogic.orderDetailCodes(deliveryId);
        return ResponseHelper.success(orderDetailCodes);
    }


}
