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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.invoice.InvoiceRequestRefund;
import com.zfire.mall.manager.bean.sale.SaleOrderBean;
import com.zfire.mall.manager.bean.sale.SaleRetreatOrderBody;
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.saletype.SaleRetreatOrderLogic;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.plus.entity.SalesOrder;
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;

@Slf4j
@RestController
@Api(value = "销售退货单", tags ={"销售退货单"} )
@RequestMapping(value = "/saleRetreat/order", produces = "application/json; charset=utf-8")
public class SaleRetreatOrderController {

    @Autowired
    SaleRetreatOrderLogic saleRetreatOrderLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @ApiOperation(value = "销售出库单列表")
    @GetMapping("/listSales")
    public ResponseHelper<IPage<SaleOrderBean>> listSales(
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) String orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) String correspondId,
            @ApiParam(required = false, value = "仓库名称") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "单据开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "单据结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<SaleOrderBean> list = saleRetreatOrderLogic.salesList(id,mainOrderId,orderNo, correspondName, materialNumber, customerName, correspondId, specification, materialName, customerNumber, startTime, endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }


    @PostMapping("/add")
    @ApiOperation("退库单添加")
    public ResponseHelper add(@RequestBody SaleRetreatOrderBody saleRetreatOrderBody, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            saleRetreatOrderLogic.add(saleRetreatOrderBody, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("退库单编辑")
    public ResponseHelper update(@RequestBody SaleRetreatOrderBody saleRetreatOrderBody, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            saleRetreatOrderLogic.update(saleRetreatOrderBody, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }



    @ApiOperation(value = "审批")
    @PostMapping("/approval")
    public ResponseHelper<SaleType> approval(
            @RequestBody(required = true) SalesOrder salesOrder
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER+salesOrder.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            saleRetreatOrderLogic.approval(salesOrder);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }



    @ApiOperation(value = "反审")
    @PostMapping("/trialApproval")
    public ResponseHelper<SaleType> trialApproval(
            @ApiParam(required = false, value = "销售出库单id") @RequestParam(required = false) String id,
            @ApiParam(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=已关闭",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "备注",required = false) @RequestParam(required = false) String approvalRemark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            saleRetreatOrderLogic.trialApproval(id,examineStatus,approvalRemark);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }




}
