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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.front.EquipmentCodeBatchVO;
import com.zfire.mall.manager.bean.front.EquipmentCodeVO;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.front.EquipmentCodeLogic;
import com.zfire.mall.manager.utils.CommonUtils;
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.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.io.InputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备条码管理 前端控制器
 * </p>
 *
 * @author chang
 * @since 2022-03-21
 */
@Slf4j
@RestController
@Api(value = "外部查询得条码核验", tags = {"外部查询得条码核验"})
@RequestMapping(value = "/externalCode/equipment", produces = "application/json; charset=utf-8")
public class CodeSupplyController {

    @Autowired
    EquipmentCodeLogic equipmentCodeLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @GetMapping("/list")
    @ApiOperation("设备条码记录List")
    public ResponseHelper<IPage<EquipmentCodeVO>> list(
            @ApiParam(value = "条形码", required = false) @RequestParam(required = false) String barCode,
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderNumber,
            @ApiParam(value = "销售公司", required = false) @RequestParam(required = false) String salesCompany,
            @ApiParam(value = "客户地址", required = false) @RequestParam(required = false) String address,
            @ApiParam(value = "扫描开始时间", required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "扫描结束时间", required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "核验开始时间", required = false) @RequestParam(required = false) String verificationStartTime,
            @ApiParam(value = "核验结束时间", required = false) @RequestParam(required = false) String verificationEndTime,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String productName,
            @ApiParam(value = "仓库", required = false) @RequestParam(required = false) String warehouse,
            @ApiParam(value = "网点信息", required = false) @RequestParam(required = false) String websitNumber,
            @ApiParam(value = "服务人员信息", required = false) @RequestParam(required = false) String workerName,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNo,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) {
        IPage<EquipmentCodeVO> list = equipmentCodeLogic.list(barCode,orderNumber,pageNo,salesCompany,address,startTime,endTime,verificationStartTime,
                verificationEndTime,productName,warehouse,websitNumber,workerName, pageSize, request);
        return ResponseHelper.success(list);
    }

    @GetMapping("/export")
    @ApiOperation("设备条码记录-导出")
    public void export(
            @ApiParam(value = "条形码", required = false) @RequestParam(required = false) String barCode,
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderNumber,
            @ApiParam(value = "销售公司", required = false) @RequestParam(required = false) String salesCompany,
            @ApiParam(value = "客户地址", required = false) @RequestParam(required = false) String address,
            @ApiParam(value = "扫描开始时间", required = true) @RequestParam(required = true) String startTime,
            @ApiParam(value = "扫描结束时间", required = true) @RequestParam(required = true) String endTime,
            @ApiParam(value = "核验开始时间", required = false) @RequestParam(required = false) String verificationStartTime,
            @ApiParam(value = "核验结束时间", required = false) @RequestParam(required = false) String verificationEndTime,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String productName,
            @ApiParam(value = "仓库", required = false) @RequestParam(required = false) String warehouse,
            @ApiParam(value = "网点信息", required = false) @RequestParam(required = false) String websitNumber,
            @ApiParam(value = "服务人员信息", required = false) @RequestParam(required = false) String workerName,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ExcelData excelData = equipmentCodeLogic.export(orderNumber,salesCompany,barCode,address,verificationStartTime,
                startTime,endTime, verificationEndTime,productName,warehouse,websitNumber,workerName, request);
        ExcelUtils.exportExcel(request, response, "条码记录导出.csv", excelData);
    }



    /////////////////////////批量条码核验////////////////////////////////////////////////////////



    @PostMapping("/importBatch")
    @ApiOperation("批量导入判断是否广佛设备")
    public ResponseHelper<String> importBatch(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request
    ) throws RemoteServiceException, IOException, ParseException {
        InputStream inputStream = file.getInputStream();
        String filePath = equipmentCodeLogic.importBatch(file,request);
        return ResponseHelper.success(filePath);
    }


    @GetMapping("/listBatch")
    @ApiOperation("批量条码核验记录List")
    public ResponseHelper<IPage<EquipmentCodeBatchVO>> listBatch(
            @ApiParam(value = "开始时间", required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "外机条码", required = false) @RequestParam(required = false) String barCode,
            @ApiParam(value = "批次号", required = false) @RequestParam(required = false) String batch,
            @ApiParam(value = "单个筛选时间", required = false) @RequestParam(required = false) String startTimeSingle,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNo,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) {
        IPage<EquipmentCodeBatchVO> list = equipmentCodeLogic.listBatch(startTimeSingle,batch,barCode,pageNo,startTime,endTime, pageSize, request);
        return ResponseHelper.success(list);
    }


    @GetMapping("/exportBatch")
    @ApiOperation("批量条码核验记录List导出")
    public void exportBatch(
            @ApiParam(value = "开始时间", required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "外机条码", required = false) @RequestParam(required = false) String barCode,
            @ApiParam(value = "批次号", required = false) @RequestParam(required = false) String batch,
            @ApiParam(value = "单个筛选时间", required = false) @RequestParam(required = false) String startTimeSingle,
            @ApiParam(value = "页号", required = false) @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "页大小", required = false) @RequestParam(required = false) Integer pageSize,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        // IPage<EquipmentCodeCheck> list = equipmentCodeLogic.listCodeCheck(startTimeSingle,batch,outsideCode,withinCode,pageNo,informationKey,startTime,endTime, pageSize, request);
        ExcelData excelData = equipmentCodeLogic.exportBatch(startTimeSingle,batch,barCode,pageNo,startTime,endTime, pageSize, request);
        ExcelUtils.exportExcel(request, response, "条码复验记录导出.csv", excelData);
    }


    @GetMapping("/downloadBatch")
    @ApiOperation("批量条码下载模板")
    public void downloadBatche(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        CommonUtils.downloadFile("tiaoma.xlsx",response);
    }



}

