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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.requisition.RequisitionBean;
import com.zfire.mall.manager.bean.requisition.RequisitionDetail;
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.requisition.RequisitionOrderLogic;
import com.zfire.mall.manager.plus.entity.RequisitionOrder;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
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;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "直接调拨单", tags = {"直接调拨单"})
@RequestMapping(value = "/requisition", produces = "application/json; charset=utf-8")
public class RequisitionController {


    @Autowired
    RequisitionOrderLogic requisitionOrderLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @ApiOperation(value = "直接调拨单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<RequisitionBean>> list(
            @ApiParam(required = false, value = "直接调拨单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "仓库") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "产品编码") @RequestParam(required = false) String materialCode,
            @ApiParam(required = false, value = "产品名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "调拨开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "调拨结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<RequisitionBean> requisitionBeanIPage = requisitionOrderLogic.list(id,examineStatus, customerName, correspondName, specification, startTime, endTime, customerNumber, materialCode, materialName, pageNum, pageSize);
        return ResponseHelper.success(requisitionBeanIPage);
    }

    @ApiOperation(value = "直接调拨单详情")
    @GetMapping("/detail")
    public ResponseHelper<RequisitionDetail> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        RequisitionDetail requisitionDetail = requisitionOrderLogic.detail(id);
        return ResponseHelper.success(requisitionDetail);
    }


    @PostMapping("/add")
    @ApiOperation("直接调拨添加")
    public ResponseHelper add(@RequestBody RequisitionOrder requisition, HttpServletRequest request) throws Exception {
        throw new RemoteServiceException("逻辑更改，无法做提前开票");
   /*     Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.REQUISITION_APPLICATION);
        try {

            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            requisitionOrderLogic.add(requisition, request);
        } finally {
            obtain.unlock();
        }*/
  /*      return ResponseHelper.success();*/
    }

    @PostMapping("/update")
    @ApiOperation("直接调拨关闭")
    public ResponseHelper update( @ApiParam(required = false, value = "调拨单号") @RequestParam(required = false) String id,
                                  HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.REQUISITION_APPLICATION+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            requisitionOrderLogic.update(id, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("直接调拨删除")
    public ResponseHelper delete( @ApiParam(required = false, value = "调拨单号") @RequestParam(required = false) List<String> ids,
                                  HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.REQUISITION_APPLICATION+ids);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            requisitionOrderLogic.delete(ids, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/updateRequisition")
    @ApiOperation("直接调拨修改")
    public ResponseHelper updateRequisition( @RequestBody RequisitionOrder requisitionOrder,
                                  HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.REQUISITION_APPLICATION+requisitionOrder.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            requisitionOrderLogic.updateRequisition(requisitionOrder, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }
    @ApiModelProperty(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭")
    private String examineStatus;

    @PostMapping("/approval")
    @ApiOperation("直接调拨审批")
    public ResponseHelper approval( @ApiParam(required = false, value = "调拨单号") @RequestParam(required = false) String id,
                                  @ApiParam(required = false, value = "单据状态 OK=通过 FAIL=不通过") @RequestParam(required = false) String examineStatus,
                                  @ApiParam(required = false, value = "审批备注") @RequestParam(required = false) String approvalRemark,
                                  HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.REQUISITION_APPLICATION+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            requisitionOrderLogic.approval(id,examineStatus,approvalRemark, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "直接调拨单列表导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "直接调拨单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "仓库") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "产品编码") @RequestParam(required = false) String materialCode,
            @ApiParam(required = false, value = "产品名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "调拨开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "调拨结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<RequisitionBean> requisitionBeanIPage = requisitionOrderLogic.list(id,examineStatus, customerName, correspondName, specification, startTime, endTime, customerNumber, materialCode, materialName, 1, -1);
        ExcelData excelData = requisitionOrderLogic.export(requisitionBeanIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"直接调拨单.xlsx",excelData);
    }

}