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


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.sale.StorageFeeBeanVO;
import com.zfire.mall.manager.bean.stock.*;
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.stock.StockCommonLogic;
import com.zfire.mall.manager.logic.stock.StockOutInLogic;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.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 java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "仓库入库出库管理", tags ={"仓库入库出库管理"} )
@RequestMapping(value = "/stock/orderOutIn", produces = "application/json; charset=utf-8")
public class StockInAndOutController {

    @Autowired
    StockOutInLogic stockOutInLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "采购退料列表")
    @GetMapping("/listPurchaseOrderOut")
    public ResponseHelper<IPage<KingDeePurchaseStockOutBean>> list(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,
            @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 materialOldNumber,
            @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 = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = "供货商") @RequestParam(required = false) String supplierName,

            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeePurchaseStockOutBean> stockBeans = stockOutInLogic.listPurchaseOrderOut(supplierName,correspondId,startTime,endTime,materialOldNumber,materialName,materialNumber,specification,billNo,pageNum,pageSize);
        return ResponseHelper.success(stockBeans);
    }



    @ZfireList
    @PostMapping("/listPurchaseOrderOutV2")
    @ApiOperation("采购退料-列表V2")
    public ResponseHelper<IPage<KingDeePurchaseStockOutVO>> listPurchaseOrderOutV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<KingDeePurchaseStockOutVO> list = stockOutInLogic.listPurchaseOrderOutV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<KingDeePurchaseStockOutVO>() {});
    }

    @PostMapping("/listPurchaseOrderOutV2/export")
    @ApiOperation("采购退料-列表V2导出")
    public void listPurchaseOrderOutV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<KingDeePurchaseStockOutVO> list = stockOutInLogic.listPurchaseOrderOutV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ApiOperation(value = "其他出库列表")
    @GetMapping("/listOtherStockOut")
    public ResponseHelper<IPage<KingDeeOtherStockOutBean>> listOtherStockOut(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,
            @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 materialOldNumber,
            @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 = "单据编号") @RequestParam(required = false) String billNo,

            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeeOtherStockOutBean> stockBeans = stockOutInLogic.listOtherStockOut(correspondId,startTime,endTime,materialOldNumber,materialName,materialNumber,specification,billNo,pageNum,pageSize);
        return ResponseHelper.success(stockBeans);
    }


    @ZfireList
    @PostMapping("/listOtherStockOutV2")
    @ApiOperation("其他出库-列表V2")
    public ResponseHelper<IPage<KingDeeOtherStockOutVO>> listOtherStockOutV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<KingDeeOtherStockOutVO> list = stockOutInLogic.listOtherStockOutV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<KingDeeOtherStockOutVO>() {});
    }

    @PostMapping("/listOtherStockOutV2/export")
    @ApiOperation("其他出库-列表V2导出")
    public void listOtherStockOutV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<KingDeeOtherStockOutVO> list = stockOutInLogic.listOtherStockOutV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ApiOperation(value = "其他入库列表")
    @GetMapping("/listOtherStockIn")
    public ResponseHelper<IPage<KingDeeOtherStockInBean>> listOtherStockIn(
            @ApiParam(required = false, value = "仓库id") @RequestParam(required = false) List<String> correspondId,
            @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 materialOldNumber,
            @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 = "单据编号") @RequestParam(required = false) String billNo,

            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeeOtherStockInBean> stockBeans = stockOutInLogic.listOtherStockIn(correspondId,startTime,endTime,materialOldNumber,materialName,materialNumber,specification,billNo,pageNum,pageSize);
        return ResponseHelper.success(stockBeans);
    }


    @ZfireList
    @PostMapping("/listOtherStockInV2")
    @ApiOperation("其他入库-列表V2")
    public ResponseHelper<IPage<KingDeeOtherStockInVO>> listOtherStockInV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<KingDeeOtherStockInVO> list = stockOutInLogic.listOtherStockInV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<KingDeeOtherStockInVO>() {});
    }

    @PostMapping("/listOtherStockInV2/export")
    @ApiOperation("其他入库-列表V2导出")
    public void listOtherStockInV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<KingDeeOtherStockInVO> list = stockOutInLogic.listOtherStockInV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ApiOperation(value = "反审采购退料单")
    @PostMapping("/trialApprovalPurchaseOrderOut")
    public ResponseHelper<SaleType> trialApprovalPurchaseOrderOut(
            @ApiParam(required = true, value = "单据号") @RequestParam(required = true) String billNo,
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stockOutInLogic.trialApprovalPurchaseOrderOut(billNo,id);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "反审采购入库单")
    @PostMapping("/trialApprovalPurchaseOrderIn")
    public ResponseHelper<SaleType> trialApprovalPurchaseOrderIn(
            @ApiParam(required = true, value = "单据号") @RequestParam(required = true) String billNo,
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stockOutInLogic.trialApprovalPurchaseOrderIn(id,billNo);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "反审其他入库单")
    @PostMapping("/trialApprovalOtherStockIn")
    public ResponseHelper<SaleType> trialApproval(
            @ApiParam(required = true, value = "单据号") @RequestParam(required = true) String billNo,
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stockOutInLogic.trialApprovalOtherStockIn(id,billNo);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "反审其他出库单")
    @PostMapping("/trialApprovalOtherStockOut")
    public ResponseHelper<SaleType> trialApprovalOtherStockOut(
            @ApiParam(required = true, value = "单据号") @RequestParam(required = true) String billNo,
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.SALES_RETREAT_ORDER+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            stockOutInLogic.trialApprovalOtherStockOut(id,billNo);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


}

