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.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.front.CustomerStockBean;
import com.zfire.mall.manager.bean.sale.CodeManagerBean;
import com.zfire.mall.manager.bean.sale.SaleOrderBean;
import com.zfire.mall.manager.bean.sale.SaleOrderBody;
import com.zfire.mall.manager.bean.sale.StorageFeeBean;
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.saletype.SaleCodeLogic;
import com.zfire.mall.manager.logic.saletype.SaleOrderLogic;
import com.zfire.mall.manager.logic.saletype.SaleRetreatOrderLogic;
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.CommonUtils;
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 org.springframework.web.multipart.MultipartFile;

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

/**
 * @author change
 */
@Slf4j
@RestController
@Api(value = "出库条码管理", tags ={"出库条码管理"} )
@RequestMapping(value = "/sale/code", produces = "application/json; charset=utf-8")
public class SaleCodeController {


    @Autowired
    RedisLockRegistry redisLockRegistry;

    @Autowired
    SaleCodeLogic saleCodeLogic;



    @PostMapping("/importData")
    @ApiOperation("导入出库条码")
    public ResponseHelper importData(
            MultipartFile file
    ) throws IOException {
        saleCodeLogic.importData(file);
        return ResponseHelper.success();
    }

    @GetMapping("/download")
    @ApiOperation("出库条码管理下载模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("tmdr.xlsx",response);
    }


    @ZfireList
    @PostMapping("/list")
    @ApiOperation("出库条码管理列表")
    public ResponseHelper<IPage<CodeManagerBean>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CodeManagerBean> stockBeanIPage = saleCodeLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<CodeManagerBean>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("出库条码管理导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CodeManagerBean> stockBeanIPage = saleCodeLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

}
