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.KingDeePurchaseStockInBean;
import com.zfire.mall.manager.bean.k3.KingDeePurchaseStockInData;
import com.zfire.mall.manager.bean.stock.KingDeePurchaseStockInAdd;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3PurchaseStockInLogic;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseStockIn;
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 = "/purchaseStockIn", produces = "application/json; charset=utf-8")
public class K3PurchaseStockInController {


    @Autowired
    K3PurchaseStockInLogic k3PurchaseStockInLogic;

    @ApiOperation(value = "采购入库单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<KingDeePurchaseStockInBean>> list(
            @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 materialNumber,
            @ApiParam(required = false, value = "产品编码") @RequestParam(required = false) String materialCode,
            @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 supplyName,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeePurchaseStockInBean> kingDeePurchaseStockInBean = k3PurchaseStockInLogic.list(materialCode,billNo,materialOldNumber,materialName,materialNumber,supplyName,startTime,endTime,pageNum,pageSize);
        return ResponseHelper.success(kingDeePurchaseStockInBean);
    }

    @ApiOperation(value = "采购入库单详情")
    @PostMapping("/detail")
    public ResponseHelper<KingDeePurchaseStockInData> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        KingDeePurchaseStockInData kingDeePurchaseStockInData = k3PurchaseStockInLogic.detail(id);
        return ResponseHelper.success(kingDeePurchaseStockInData);
    }

    @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 materialNumber,
            @ApiParam(required = false, value = "产品编码") @RequestParam(required = false) String materialCode,

            @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 supplyName,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<KingDeePurchaseStockInBean> kingDeePurchaseStockInBean = k3PurchaseStockInLogic.list(materialCode,billNo,materialOldNumber,materialName,materialNumber,supplyName,startTime,endTime,1,-1);
        ExcelData excelData = k3PurchaseStockInLogic.export(kingDeePurchaseStockInBean.getRecords());
        ExcelUtils.exportExcel(request,response,"采购入库单.xlsx",excelData);
    }


    @ApiOperation(value = "新增采购入库单")
    @PostMapping("/add")
    public ResponseHelper add(
            @RequestBody KingDeePurchaseStockInAdd kingDeePurchaseStockInAdd
    ) throws Exception {
        k3PurchaseStockInLogic.add(kingDeePurchaseStockInAdd);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "修改采购入库单")
    @PostMapping("/updateCustomer")
    public ResponseHelper updateCustomer(
            @RequestBody KingDeePurchaseStockInAdd kingDeePurchaseStockInAdd
    ) throws Exception {
        k3PurchaseStockInLogic.update(kingDeePurchaseStockInAdd);
        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 {
        k3PurchaseStockInLogic.examine(billNo,cancelStatus);
        return ResponseHelper.success();
    }


}
