package com.njcn.csdevice.controller.equipment;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csdevice.pojo.param.WlRecordTemplete;
import com.njcn.csdevice.pojo.po.PortableOffMainLog;
import com.njcn.csdevice.pojo.po.PortableOfflLog;
import com.njcn.csdevice.service.IPortableOfflLogService;
import com.njcn.csdevice.utils.ExcelStyleUtil;
import com.njcn.csdevice.param.UploadDataParam;
import com.njcn.minioss.config.MinIossProperties;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.poi.excel.ExcelUtil;
import com.njcn.poi.util.PoiUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.pojo.po.DictData;
import com.njcn.web.pojo.param.BaseParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import com.njcn.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  离线数据导入 前端控制器
 * </p>
 *
 * @author guofeihu
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/portableOfflLog")
@Api(tags = " 离线数据导入")
@AllArgsConstructor
public class PortableOfflLogController extends BaseController {

    private final IPortableOfflLogService iPortableOfflLogService;

    private final DicDataFeignClient dicDataFeignClient;

    private final FileStorageUtil fileStorageUtil;
    private final MinIossProperties minIossProperties;
    @ResponseBody
    @ApiOperation("导出设备基础数据模板")
    @GetMapping(value = "getExcelTemplate")
    public HttpResult<String> getExcelTemplate(HttpServletResponse response) {
        ExportParams exportParams = new ExportParams("批量导入模板（请严格按照模板标准填入数据,带*表示必填）", "便携式终端基础数据录入信息");
        exportParams.setStyle(ExcelStyleUtil.class);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, WlRecordTemplete.class, new ArrayList<WlRecordTemplete>());
        ExcelUtil.selectList(workbook, 5, 5, Stream.of("1分钟","3分钟","5分钟","10分钟").collect(Collectors.toList()).toArray(new String[]{}));
        ExcelUtil.selectList(workbook, 8, 8, Stream.of(dicDataFeignClient.getDicDataByTypeCode("Dev_Voltage").getData().stream().map(DictData::getName).collect(Collectors.toList())).collect(Collectors.toList()).get(0).toArray(new String[]{}));
        ExcelUtil.selectList(workbook, 13, 13, Stream.of(dicDataFeignClient.getDicDataByTypeCode("Dev_Connect").getData().stream().map(DictData::getName).collect(Collectors.toList())).collect(Collectors.toList()).get(0).toArray(new String[]{}));
        String fileName = "便携式设备模板.xlsx";
        PoiUtil.exportFileByWorkbook(workbook, fileName, response);
        return null;
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPage")
    @ApiOperation("便携式设备解析列表")
    public HttpResult<Page<PortableOfflLog>> queryPage(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("queryPage");
        Page<PortableOfflLog> list = iPortableOfflLogService.queryPage(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryMainLogPage")
    @ApiOperation("便携式设备解析日志")
    public HttpResult<Page<PortableOffMainLog>> queryMainLogPage(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("queryMainLogPage");
        Page<PortableOffMainLog> list = iPortableOfflLogService.queryMainLogPage(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("离线数据导入")
    @PostMapping(value = "importEquipment")
    public HttpResult<String> importEquipment(@RequestParam("devId") String devId
            , @RequestParam("lineId") String lineId, @RequestParam("paths") String paths,HttpServletRequest request) {
        String methodDescribe = getMethodDescribe("importEquipment");
        UploadDataParam uploadDataParam = new UploadDataParam();
        uploadDataParam.setDevId(devId);
        uploadDataParam.setLineId(lineId);
        uploadDataParam.setPaths(Arrays.asList(paths.split(",")));
        MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> files = Murequest.getFileMap() ;
        List<MultipartFile> fileList = new ArrayList<>();
        for(MultipartFile file : files.values()){
            fileList.add(file);
        }
        uploadDataParam.setFiles(fileList);
        iPortableOfflLogService.importEquipment(uploadDataParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("数据在线补招")
    @PostMapping(value = "dataOnlineRecruitment")
    public HttpResult<String> dataOnlineRecruitment(@RequestParam("devId") String devId
            , @RequestParam("lineId") String lineId, @RequestParam("path") String path) {
        String methodDescribe = getMethodDescribe("dataOnlineRecruitment");

        List<MultipartFile> excelreport = null;
        try {
            excelreport = fileStorageUtil.getAllFile(minIossProperties.getBucket(), path);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        UploadDataParam uploadDataParam = new UploadDataParam();
        uploadDataParam.setDevId(devId);
        uploadDataParam.setLineId(lineId);

        uploadDataParam.setFiles(excelreport);
        iPortableOfflLogService.importEquipment(uploadDataParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

}

