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.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3PurchaseStockInLogic;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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 = "同步采购入库单列表")
//    @PostMapping("/syncPurchaseStockIn")
//    public ResponseHelper sync(
//            @ApiParam(required = true, value = "组织机构") @RequestParam(required = true) String orgId,
//            @ApiParam(required = true, value = "组织机构编码") @RequestParam(required = true) String orgNumber,
//            @ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String startTime,
//            @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime
//    ) throws Exception {
//        k3PurchaseStockInLogic.allSync(orgId,orgNumber,startTime,endTime);
//        return ResponseHelper.success();
//    }

    @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 = "采购入库单详情")
    @GetMapping("/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);
    }
}
