package com.zfire.mall.manager.controller.displace.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.displace.order.DisplaceOrderBean;
import com.zfire.mall.manager.bean.displace.order.DisplaceOrderListBean;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.displace.order.DisplaceOrderLogic;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "置换单V1（废弃）", tags ={"置换单V1（废弃）"} )
@RequestMapping(value = "/displace-order", produces = "application/json; charset=utf-8")
public class DisplaceOrderController {

    @Resource
    DisplaceOrderLogic displaceOrderLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "置换单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<DisplaceOrderListBean>> list(
            @ApiParam(value = "客户编码/客户名称") @RequestParam(required = false) String customerKeyword,
            @ApiParam(value = "置换单编号") @RequestParam(required = false) String displaceOrderId,
            @ApiParam(value = "原订单编号") @RequestParam(required = false) String originOrderNo,
            @ApiParam(value = "订单类型") @RequestParam(required = false) String orderType,
            @ApiParam(value = "产品名称") @RequestParam(required = false) String productName,
            @ApiParam(value = "订单日期-开始") @RequestParam(required = false) String startOrderTime,
            @ApiParam(value = "订单日期-结束") @RequestParam(required = false) String endOrderTime,
            @ApiParam(value = "创建人") @RequestParam(required = false) String createName,
            @ApiParam(value = "审核人") @RequestParam(required = false) String confirmName,
            @ApiParam(value = "状态") @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<DisplaceOrderListBean> listPage = displaceOrderLogic.listPage(customerKeyword, displaceOrderId, originOrderNo,
                orderType, productName, startOrderTime, endOrderTime, createName, confirmName, examineStatus, serviceId, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "置换单详情")
    @GetMapping("/detail")
    public ResponseHelper<DisplaceOrderBean> detail(
            @ApiParam(value = "置换单id") @RequestParam String id
    ) throws Exception {
        DisplaceOrderBean displaceOrderBean = displaceOrderLogic.detail(id);
        return ResponseHelper.success(displaceOrderBean);
    }

    @ApiOperation(value = "查询引用单明细")
    @GetMapping("/query-ref-detail")
    public ResponseHelper<DisplaceOrderBean> queryRefDetail(
            @ApiParam(value = "原订单编号") @RequestParam(required = false) String originOrderNo
    ) throws Exception {
        DisplaceOrderBean displaceOrderBean = displaceOrderLogic.queryRefDetail(originOrderNo);
        return ResponseHelper.success(displaceOrderBean);
    }

    @ApiOperation(value = "新增置换单")
    @PostMapping("/add")
    public ResponseHelper tradeAdd(
            @ApiParam(required = true, value = "置换单") @RequestBody DisplaceOrderBean displaceOrderBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DISPLACE_ORDER_ADD + displaceOrderBean.getOrderNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                String id = displaceOrderLogic.add(displaceOrderBean);
                return ResponseHelper.success(id);
            }
        } catch(Exception e) {
            log.error("【置换单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.error("新增失败");
    }

    @ApiOperation(value = "编辑置换单")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "置换单") @RequestBody DisplaceOrderBean displaceOrderBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DISPLACE_ORDER + displaceOrderBean.getDisplaceOrderId());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                displaceOrderLogic.edit(displaceOrderBean);
            }
        } catch(Exception e) {
            log.error("【置换单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/submit")
    @ApiOperation("提交置换单")
    public ResponseHelper submit(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DISPLACE_ORDER + id);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                displaceOrderLogic.submit(id);
            }
        } catch(Exception e) {
            log.error("【置换单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "撤回置换单")
    @PostMapping("/revoke")
    public ResponseHelper revoke(
            @ApiParam(required = true, value = "家用工程订单id") @RequestParam String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DISPLACE_ORDER + id);
        try {

            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                displaceOrderLogic.revoke(id);
            }
        } catch(Exception e) {
            log.error("【置换单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "审批置换单")
    @PostMapping("/examine")
    public ResponseHelper examine(
            @ApiParam(required = true, value = "审批置换单") @RequestBody DisplaceOrderBean displaceOrderBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DISPLACE_ORDER + displaceOrderBean.getDisplaceOrderId());
        try {

            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                displaceOrderLogic.examine(displaceOrderBean);
            }
        } catch(Exception e) {
            log.error("【置换单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除置换单")
    @PostMapping("/del")
    public ResponseHelper homeDel(
            @ApiParam(required = true, value = "置换单id") @RequestParam List<String> ids
    ) throws Exception {
        displaceOrderLogic.del(ids);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "退单置换单")
    @PostMapping("/refund")
    public ResponseHelper refund(
            @ApiParam(required = true, value = "置换单") @RequestBody DisplaceOrderBean displaceOrderBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DISPLACE_ORDER + displaceOrderBean.getDisplaceOrderId());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                displaceOrderLogic.refund(displaceOrderBean);
            }
        } catch(Exception e) {
            log.error("【置换单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }
}
