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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ApiNotAuth;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.PoySaleCountBean;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.sale.*;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.saletype.SaleOrderLogic;
import com.zfire.mall.manager.logic.saletype.SaleRetreatOrderLogic;
import com.zfire.mall.manager.logic.stock.StorageFeeLogic;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.plus.entity.SalesOrder;
import com.zfire.mall.manager.plus.service.SalesOrderService;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
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.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(value = "仓储费报表", tags ={"仓储费报表"} )
@RequestMapping(value = "/sale/storageFee", produces = "application/json; charset=utf-8")
public class StorageFeeController {

    @Autowired
    StorageFeeLogic storageFeeLogic;




    @ApiOperation(value = "仓储费报表列表")
    @GetMapping("/listStorageFee")
    public ResponseHelper<IPage<StorageFeeBean>> listStorageFee(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单类型 TRADE=商用 HOME=家用 RETAIL=零售单 RETAIL_POLICY 销售政策单  PERMU_HOME = 置换单家用        PERMU_TRADE 置换商用   PERMU_RETAIL  置换零售   PERMU_RETAIL_POLICY 置换销售政策单  REQUISITION_RETAIL 调拨单零售   REQUISITION_RETAIL_POLICY 调拨单销售政策REQUISITION_TRADE 调拨单商用 REQUISITION_HOME 调拨单家用") @RequestParam(required = false) String orderType,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @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 invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "物料分类编码") @RequestParam(required = false) List<String> categoryNum,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @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 specification,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "出库单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String saleExamineStatus,
            @ApiParam(required = false, value = "出库开始时间") @RequestParam(required = false) String saleStartTime,
            @ApiParam(required = false, value = "出库结束时间") @RequestParam(required = false) String saleEndTime,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<StorageFeeBean> list = storageFeeLogic.listStorageFee(categoryNum,approvalStartTime,approvalEndTime,saleExamineStatus,saleStartTime,saleEndTime,invoiceStartTime,invoiceEndTime,mainOrderId,id,orderNo,materialNumber, customerName,correspondId,specification,materialName,customerNumber
                ,startTime,endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "仓储费报表导出")
    @GetMapping("/listStorageFeeExport")
    public void listStorageFeeExport(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单类型 TRADE=商用 HOME=家用 RETAIL=零售单 RETAIL_POLICY 销售政策单  PERMU_HOME = 置换单家用        PERMU_TRADE 置换商用   PERMU_RETAIL  置换零售   PERMU_RETAIL_POLICY 置换销售政策单  REQUISITION_RETAIL 调拨单零售   REQUISITION_RETAIL_POLICY 调拨单销售政策REQUISITION_TRADE 调拨单商用 REQUISITION_HOME 调拨单家用") @RequestParam(required = false) String orderType,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @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 invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "物料分类编码") @RequestParam(required = false) List<String> categoryNum,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @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 specification,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "出库单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String saleExamineStatus,
            @ApiParam(required = false, value = "出库开始时间") @RequestParam(required = false) String saleStartTime,
            @ApiParam(required = false, value = "出库结束时间") @RequestParam(required = false) String saleEndTime,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<StorageFeeBean> list = storageFeeLogic.listStorageFee(categoryNum,approvalStartTime,approvalEndTime,saleExamineStatus,saleStartTime,saleEndTime,invoiceStartTime,invoiceEndTime,mainOrderId,id,orderNo,materialNumber, customerName,correspondId,specification,materialName,customerNumber
                ,startTime,endTime, 1, -1);
        ExcelData excelData = storageFeeLogic.listStorageFeeExport(list.getRecords());
        ExcelUtils.exportExcel(request,response,"仓储费报表.xlsx",excelData);
    }


}
