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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.k3.KingTransfer;
import com.zfire.mall.manager.bean.k3.KingTransferAddOrUp;
import com.zfire.mall.manager.bean.k3.KingTransferAddOrUp;
import com.zfire.mall.manager.bean.k3.KingTransferBean;
import com.zfire.mall.manager.bean.pick.PickManagerBean;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3SupplierLogic;
import com.zfire.mall.manager.logic.k3.K3TransferLogic;
import com.zfire.mall.manager.plus.entity.KingDeeReceipt;
import com.zfire.mall.manager.plus.entity.KingDeeSupplier;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "调拨单管理", tags ={"调拨单管理"} )
@RequestMapping(value = "/transfer", produces = "application/json; charset=utf-8")
public class K3TransferController {


    @Autowired
    K3TransferLogic k3TransferLogic;


    @ApiOperation(value = "调拨单详情")
    @PostMapping("/detail")
    public ResponseHelper<KingTransferBean> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        KingTransferBean kingTransferBean = k3TransferLogic.detail(id);
        return ResponseHelper.success(kingTransferBean);
    }

    @ApiOperation(value = "调拨单导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "提货开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "提货结束时间") @RequestParam(required = false) String endTime,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<KingTransfer> kingTransfer = k3TransferLogic.list(billNo,materialName,startTime,endTime,1,-1);
        ExcelData excelData = k3TransferLogic.export(kingTransfer.getRecords());
        ExcelUtils.exportExcel(request,response,"调拨单.xlsx",excelData);
    }


    @ApiOperation(value = "新增调拨单")
    @PostMapping("/add")
    public ResponseHelper add(
            @RequestBody KingTransferAddOrUp kingTransferAdd
    ) throws Exception {
        k3TransferLogic.add(kingTransferAdd);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "修改调拨单")
    @PostMapping("/update")
    public ResponseHelper update(
            @RequestBody KingTransferAddOrUp kingTransferAdd
    ) throws Exception {
        k3TransferLogic.update(kingTransferAdd);
        return ResponseHelper.success();
    }


    @ApiOperation(value = "审批调拨单")
    @PostMapping("/examine")
    public ResponseHelper examine(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = "  OK通过  WAIT待审 FAIL 不通过") @RequestParam(required = false) String cancelStatus
    ) throws Exception {
        k3TransferLogic.examine(billNo,cancelStatus);
        return ResponseHelper.success();
    }




}
