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.k3.StockPosition;
import com.zfire.mall.manager.bean.k3.StockPositionStock;
import com.zfire.mall.manager.bean.retail.RetailOrderAdd;
import com.zfire.mall.manager.bean.ship.*;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3StockLogic;
import com.zfire.mall.manager.logic.ship.ShipLogic;
import com.zfire.mall.manager.plus.entity.*;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;

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


    @Autowired
    ShipLogic shipLogic;

    @ApiOperation(value = "零售发货单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<ShipDocumentBean>> list(
            @ApiParam(required = false, value = "发货单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "订单查询") @RequestParam(required = false) String mainOrderId,
            @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(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String K3serviceId,
            @ApiParam(required = false, value = "存货类别") @RequestParam(required = false) String categoryName,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<ShipDocumentBean> kingDeeSupplier = shipLogic.list(K3serviceId,id,mainOrderId,categoryName, customerName, materialName, specification, startTime, endTime,
                customerNumber, pageNum, pageSize,serviceId);
        return ResponseHelper.success(kingDeeSupplier);
    }

    @ApiOperation(value = "零售发货单详情")
    @GetMapping("/detail")
    public ResponseHelper<ShipDocumentDetail> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        ShipDocumentDetail shipDocumentDetail = shipLogic.detail(id);
        return ResponseHelper.success(shipDocumentDetail);
    }


    @PostMapping("/update")
    @ApiOperation("修改发货单信息")
    public ResponseHelper update(
            @RequestBody ShipDocument shipDocument) throws InterruptedException {
        shipLogic.update(shipDocument);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "工程发货单列表")
    @GetMapping("/listProject")
    public ResponseHelper<IPage<ShipDocumentProjectBean>> listProject(
            @ApiParam(required = false, value = "发货单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "销售订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "工程订单编号") @RequestParam(required = false) String enginOrderNo,
            @ApiParam(required = false, value = "工程登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @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(required = false, value = "2家用工程  3商用工程") @RequestParam(required = false)  String orderType,
            @ApiParam(required = false, value = "存货类别") @RequestParam(required = false) String categoryName,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "区域") @RequestParam(required = false) String refRegionWork,

            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<ShipDocumentProjectBean> shipDocumentProjectBeans = shipLogic.listProject(refRegionWork,mainOrderId,id,enginOrderNo,categoryName,refEnginRecordNo, customerName,
                materialName, specification, startTime, endTime, customerNumber, pageNum, pageSize,orderType,serviceId);
        return ResponseHelper.success(shipDocumentProjectBeans);
    }

    @ApiOperation(value = "工程发货单详情")
    @GetMapping("/detailProject")
    public ResponseHelper<ShipDocumentProjectDetail> detailProject(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        ShipDocumentProjectDetail shipDocumentProjectBean = shipLogic.detailProject(id);
        return ResponseHelper.success(shipDocumentProjectBean);
    }

    @PostMapping("/shipPassword")
    @ApiOperation("获取密码")
    public ResponseHelper<Integer> shipPassword(
            @ApiParam(required = true, value = "发货单号") @RequestParam(required = true) String id) throws InterruptedException {
        Integer password = shipLogic.shipPassword(id);
        return ResponseHelper.success(password);
    }
    @ApiOperation(value = "密码记录列表")
    @GetMapping("/listShipPassword")
    public ResponseHelper<IPage<ShipDocumentPassword>> listShipPassword(
            @ApiParam(required = true, value = "发货单号") @RequestParam(required = true) String id,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<ShipDocumentPassword> shipDocumentProjectBeans = shipLogic.listShipPassword(id, pageNum, pageSize);
        return ResponseHelper.success(shipDocumentProjectBeans);
    }


    @ApiOperation(value = "零售发货单列表导出")
    @GetMapping("/eExport")
    public void eExport(
            @ApiParam(required = false, value = "发货单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "订单查询") @RequestParam(required = false) String mainOrderId,
            @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(required = false, value = "存货类别") @RequestParam(required = false) String categoryName,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String K3serviceId,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<ShipDocumentBean> kingDeeSupplier = shipLogic.list(K3serviceId,id, mainOrderId,categoryName,customerName, materialName, specification,
                startTime, endTime, customerNumber, 1, -1,serviceId);
        ExcelData excelData = shipLogic.eExport(kingDeeSupplier.getRecords());
        ExcelUtils.exportExcel(request,response,"零售发货单.xlsx",excelData);
    }

    @ApiOperation(value = "工程发货单列表导出")
    @GetMapping("/pExport")
    public void pExport(
            @ApiParam(required = false, value = "发货单号") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "工程订单编号") @RequestParam(required = false) String enginOrderNo,
            @ApiParam(required = false, value = "订单查询") @RequestParam(required = false) String mainOrderId,

            @ApiParam(required = false, value = "工程登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @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(required = false, value = "存货类别") @RequestParam(required = false) String categoryName,
            @ApiParam(required = false, value = "2家用工程  3商用工程") @RequestParam(required = false)  String orderType,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "区域") @RequestParam(required = false) String refRegionWork,

            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<ShipDocumentProjectBean> shipDocumentProjectBeans = shipLogic.listProject(refRegionWork,mainOrderId,id,enginOrderNo,categoryName,refEnginRecordNo, customerName,
                materialName, specification, startTime, endTime, customerNumber, 1, -1,orderType,serviceId);
        ExcelData excelData = shipLogic.pExport(shipDocumentProjectBeans.getRecords());
        String fileName = "商用工程发货单";
        if(null != orderType && "2".equals(orderType)){
            fileName = "家用工程发货单";
            excelData = shipLogic.pProjectExport(shipDocumentProjectBeans.getRecords());
        }
        ExcelUtils.exportExcel(request,response,fileName + ".xlsx",excelData);
    }


}

