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


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
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.listV2.LogisticsConfigurationVO;
import com.zfire.mall.manager.bean.stock.StockVO;
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.logistics.LogisticsLogic;
import com.zfire.mall.manager.plus.entity.LogisticsConfiguration;
import com.zfire.mall.manager.plus.entity.StockReserve;
import com.zfire.mall.manager.plus.entity.StockToll;
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.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;
import java.util.List;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "物流配置管理", tags ={"物流配置管理"} )
@RequestMapping(value = "/logistics", produces = "application/json; charset=utf-8")
public class LogisticsController {

    @Autowired
    LogisticsLogic logisticsLogic;


    @PostMapping("/add")
    @ApiOperation("新增物流配置")
    public ResponseHelper add(@RequestBody LogisticsConfiguration logisticsConfiguration, HttpServletRequest request){
        logisticsLogic.add(logisticsConfiguration,request);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改物流配置")
    public ResponseHelper update(@RequestBody LogisticsConfiguration logisticsConfiguration, HttpServletRequest request){
        logisticsLogic.update(logisticsConfiguration,request);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("删除物流配置")
    public ResponseHelper delete(@RequestBody List<String> ids){
        logisticsLogic.delete(ids);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "物流配置列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<LogisticsConfiguration>> list(
            @ApiParam(value = "仓库名称",required = false)@RequestParam(required = false) String logisticsCompany,
            @ApiParam(value = "仓位名称",required = false)@RequestParam(required = false) String customerTel,
            @ApiParam(value = "页号",required = true)@RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam(required = true) Integer pageSize
    ) throws Exception {
        IPage<LogisticsConfiguration> logis = logisticsLogic.list(logisticsCompany,customerTel,pageNum,pageSize);
        return ResponseHelper.success(logis);
    }


    @ZfireList
    @PostMapping("/listV2")
    @ApiOperation("物流配置-列表V2")
    public ResponseHelper<IPage<LogisticsConfigurationVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<LogisticsConfigurationVO> list = logisticsLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StockVO>() {});
    }

    @PostMapping("/listV2/export")
    @ApiOperation("物流配置-列表V2导出")
    public void listV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<LogisticsConfigurationVO> list = logisticsLogic.listV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }




    @ApiOperation(value = "物流配置详情")
    @GetMapping("/detail")
    public ResponseHelper<LogisticsConfiguration> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        LogisticsConfiguration logisticsConfiguration = logisticsLogic.detail(id);
        return ResponseHelper.success(logisticsConfiguration);
    }

    @GetMapping("/export")
    @ApiOperation(value = "物流配置导出")
    public void exportToll(
            @ApiParam(value = "仓库名称",required = false)@RequestParam(required = false) String logisticsCompany,
            @ApiParam(value = "仓位名称",required = false)@RequestParam(required = false) String customerTel,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<LogisticsConfiguration> logis = logisticsLogic.list(logisticsCompany,customerTel,1,-1);
        ExcelData excelData = logisticsLogic.export(logis.getRecords());
        ExcelUtils.exportExcel(request,response,"物流配置导出.xlsx",excelData);
    }

}
