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.KingDeePurchaseOrderBean;
import com.zfire.mall.manager.bean.k3.KingDeePurchaseOrderData;
import com.zfire.mall.manager.bean.k3.KingTransfer;
import com.zfire.mall.manager.bean.k3.KingTransferBean;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3PurchaseOrderLogic;
import com.zfire.mall.manager.logic.k3.K3TransferLogic;
import com.zfire.mall.manager.plus.entity.KingDeePurchaseOrder;
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 = "/purchaseOrder", produces = "application/json; charset=utf-8")
public class K3PurchaseOrderController {


    @Autowired
    K3PurchaseOrderLogic k3PurchaseOrderLogic;

//    @ApiOperation(value = "同步采购订单列表")
//    @PostMapping("/syncPurchaseOrder")
//    public ResponseHelper syncPurchaseOrder(
//            @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 {
//        k3PurchaseOrderLogic.allSync(orgId,orgNumber,startTime,endTime);
//        return ResponseHelper.success();
//    }

    @ApiOperation(value = "采购订单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<KingDeePurchaseOrderBean>> list(
            @ApiParam(required = false, value = "业务类型") @RequestParam(required = false) String businessType,
            @ApiParam(required = false, value = "供应商") @RequestParam(required = false) String assignSupplierId,
            @ApiParam(required = false, value = "采购开始日期") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "采购结束日期") @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeePurchaseOrderBean> kingTransfer = k3PurchaseOrderLogic.list(businessType,assignSupplierId,startTime,endTime,pageNum,pageSize);
        return ResponseHelper.success(kingTransfer);
    }

    @ApiOperation(value = "采购订单详情")
    @GetMapping("/detail")
    public ResponseHelper<KingDeePurchaseOrderData> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        KingDeePurchaseOrderData kingDeePurchaseOrderData = k3PurchaseOrderLogic.detail(id);
        return ResponseHelper.success(kingDeePurchaseOrderData);
    }


    @ApiOperation(value = "采购订单导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "业务类型") @RequestParam(required = false) String businessType,
            @ApiParam(required = false, value = "供应商") @RequestParam(required = false) String assignSupplierId,
            @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<KingDeePurchaseOrderBean> kingTransfer = k3PurchaseOrderLogic.list(businessType,assignSupplierId,startTime,endTime,1,-1);
        ExcelData excelData = k3PurchaseOrderLogic.export(kingTransfer.getRecords());
        ExcelUtils.exportExcel(request,response,"采购订单.xlsx",excelData);
    }

}
