package com.zfire.mall.manager.controller.invoice;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.DirectOrderBean;
import com.zfire.mall.manager.bean.engin.order.EnginOrderBean;
import com.zfire.mall.manager.bean.invoice.InvoiceRequestJson;
import com.zfire.mall.manager.bean.invoice.InvoiceRequestRefund;
import com.zfire.mall.manager.bean.sale.SaleOrderBean;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.invoice.InvoiceRefundLogic;
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 java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "直调申请发货单", tags = {"直调申请发货单"})
@RequestMapping(value = "/invoice/rak", produces = "application/json; charset=utf-8")
public class InvoiceRakController {


    @Autowired
    InvoiceRefundLogic invoiceRefundLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;




    @PostMapping("/addRak")
    @ApiOperation("零售直调申请发货单添加")
    public ResponseHelper addRak(@RequestBody DirectOrderBean directOrderBean, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceRefundLogic.addRak(directOrderBean, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }



    @PostMapping("/addProjectRak")
    @ApiOperation("工程直调申请发货单添加")
    public ResponseHelper addProjectRak(@RequestBody EnginOrderBean enginOrderBean, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.ENGIN_ORDER + enginOrderBean.getRefEnginRecordNo());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceRefundLogic.addProjectRak(enginOrderBean, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/updateRak")
    @ApiOperation("直调申请发货单编辑")
    public ResponseHelper updateRak(@RequestBody InvoiceRequestJson invoiceRequest, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + invoiceRequest.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            invoiceRefundLogic.updateRak(invoiceRequest, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/deleteRak")
    @ApiOperation("直调申请发货单删除")
    public ResponseHelper deleteRak(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id
            , HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceRefundLogic.deleteRak(id);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/giveUpRak")
    @ApiOperation("直调申请发货单弃审")
    public ResponseHelper giveUp(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id
            , HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceRefundLogic.giveUpRak(id);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }



    @PostMapping("/approvalInvoiceRak")
    @ApiOperation("零售直调申请发货单审核")
    public ResponseHelper approvalInvoiceRak(
            @RequestBody InvoiceRequestJson invoiceRequestJson,
            HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + invoiceRequestJson.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceRefundLogic.approvalInvoiceRak(invoiceRequestJson, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/ifRak")
    @ApiOperation("判断是否为生活电器品类")
    public ResponseHelper ifRak(
            @ApiParam(required = true, value = "物料id") @RequestParam(required = true) List<String> id
            , HttpServletRequest request) throws Exception {

        Boolean boo = invoiceRefundLogic.ifRak(id);

        return ResponseHelper.success(boo);
    }

}