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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.invoice.InvoiceBean;
import com.zfire.mall.manager.bean.invoice.InvoiceRequestJson;
import com.zfire.mall.manager.bean.pick.PickManagerBean;
import com.zfire.mall.manager.bean.ship.*;
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.ship.RetreatLogic;
import com.zfire.mall.manager.logic.ship.ShipLogic;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
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.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "退货单", tags ={"退货单"} )
@RequestMapping(value = "/retreat", produces = "application/json; charset=utf-8")
public class RetreatController {


    @Autowired
    RetreatLogic retreatLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "退货单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<RetreatDocumentBean>> list(
            @ApiParam(required = false, value = "退货单号") @RequestParam(required = false) String retreatOrderNo,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @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 = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<RetreatDocumentBean> kingDeeSupplier = retreatLogic.list(retreatOrderNo,customerName,materialName,specification,startTime,endTime,customerNumber,pageNum,pageSize);
        return ResponseHelper.success(kingDeeSupplier);
    }

    @ApiOperation(value = "退货单详情")
    @GetMapping("/detail")
    public ResponseHelper<RetreatDocumentDetail> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        RetreatDocumentDetail retreatDocumentDetail = retreatLogic.detail(id);
        return ResponseHelper.success(retreatDocumentDetail);
    }


    @ApiOperation(value = "退货申请发货单添加列表")
    @GetMapping("/listInvoice")
    public ResponseHelper<IPage<RetreatInvoiceBean>> listInvoice(
            @ApiParam(required = true, value = "发货申请单") @RequestParam(required = true) String id,
            @ApiParam(required = false, value = "经销商id") @RequestParam(required = false) String customerId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<RetreatInvoiceBean> invoiceBean = retreatLogic.listInvoice(id,customerId, pageNum, pageSize);
        return ResponseHelper.success(invoiceBean);
    }


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


    @ApiOperation(value = "退货单列表导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "退货单号") @RequestParam(required = false) String retreatOrderNo,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @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 = "经销商编号") @RequestParam(required = false) String customerNumber,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<RetreatDocumentBean> kingDeeSupplier = retreatLogic.list(retreatOrderNo,customerName,materialName,specification,startTime,endTime,customerNumber,1,-1);
        ExcelData excelData = retreatLogic.export(kingDeeSupplier.getRecords());
        ExcelUtils.exportExcel(request,response,"退货单.xlsx",excelData);
    }

}