package com.cico.nest.lcdp.boot.core.qxsd.adapter.web;

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelEquipment.TunnelEquipmentExcelVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelEquipment.TunnelEquipmentPageReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelEquipment.TunnelEquipmentRespVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelEquipment.TunnelEquipmentSaveReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelEquipmentService;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.impl.TunnelEquipmentServiceImpl;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelFacilityMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelEquipmentDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelFacilityDO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.cico.nest.lcdp.framework.common.pojo.PageParam;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import com.cico.nest.lcdp.framework.common.util.object.BeanUtils;
import static com.cico.nest.lcdp.framework.common.pojo.CommonResult.success;

import com.cico.nest.lcdp.framework.excel.core.util.ExcelUtils;

import com.cico.nest.lcdp.framework.operatelog.core.annotations.OperateLog;
import org.springframework.web.multipart.MultipartFile;

import static com.cico.nest.lcdp.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 隧道设备")
@RestController
@RequestMapping("/qxsd/tunnel-equipment")
@Validated
public class TunnelEquipmentController {

    @Resource
    private TunnelEquipmentService tunnelEquipmentService;
    @Resource
    private TunnelFacilityMapper tunnelFacilityMapper;

    @PostMapping("/create")
    @Operation(summary = "创建隧道设备")
    public CommonResult<String> createTunnelEquipment(@Valid @RequestBody TunnelEquipmentSaveReqVO createReqVO) {
        return success(tunnelEquipmentService.createTunnelEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新隧道设备")
    public CommonResult<Boolean> updateTunnelEquipment(@Valid @RequestBody TunnelEquipmentSaveReqVO updateReqVO) {
        tunnelEquipmentService.updateTunnelEquipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除隧道设备")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTunnelEquipment(@RequestParam("id") String id) {
        tunnelEquipmentService.deleteTunnelEquipment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得隧道设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TunnelEquipmentRespVO> getTunnelEquipment(@RequestParam("id") String id) {
        TunnelEquipmentDO tunnelEquipment = tunnelEquipmentService.getTunnelEquipment(id);
        return success(BeanUtils.toBean(tunnelEquipment, TunnelEquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得隧道设备分页")
    public CommonResult<PageResult<TunnelEquipmentRespVO>> getTunnelEquipmentPage(@Valid TunnelEquipmentPageReqVO pageReqVO) {
        PageResult<TunnelEquipmentDO> pageResult = tunnelEquipmentService.getTunnelEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TunnelEquipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出隧道设备 Excel")
    @OperateLog(type = EXPORT)
    public void exportTunnelEquipmentExcel(@Valid TunnelEquipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TunnelEquipmentDO> list = tunnelEquipmentService.getTunnelEquipmentPage(pageReqVO).getList();

        Set<String> belongFacilityIds = list.stream().map(TunnelEquipmentDO::getBelongFacilityId).collect(Collectors.toSet());
        List<TunnelFacilityDO> tunnelFacilityDOList = tunnelFacilityMapper.selectBatchIds(belongFacilityIds);
        Map<String,String> tunnelFacilityMap = tunnelFacilityDOList
                .stream()
                .collect(Collectors.toMap(TunnelFacilityDO::getId, TunnelFacilityDO::getFacilityName));

        List<TunnelEquipmentRespVO> listVO = new ArrayList<>();
        for(TunnelEquipmentDO tunnelEquipmentDO : list){
            String associateMappingSubclass = TunnelEquipmentServiceImpl
                    .associateMappingSubclassMap
                    .get(tunnelEquipmentDO.getAssociateMappingSubclass());
            TunnelEquipmentRespVO tunnelEquipmentRespVO = BeanUtils.toBean(tunnelEquipmentDO, TunnelEquipmentRespVO.class);
            tunnelEquipmentRespVO.setAssociateMappingSubclass(associateMappingSubclass);
            if(tunnelEquipmentDO.getUplinkDownlinkRoad() == 0){
                tunnelEquipmentRespVO.setUplinkDownlinkRoadStr("上行");
            }else{
                tunnelEquipmentRespVO.setUplinkDownlinkRoadStr("下行");
            }
            tunnelEquipmentRespVO.setBelongFacilityId(tunnelFacilityMap.get(tunnelEquipmentRespVO.getBelongFacilityId()));
            listVO.add(tunnelEquipmentRespVO);
        }
        // 导出 Excel
        ExcelUtils.write(response, "隧道设备.xls", "数据", TunnelEquipmentRespVO.class,listVO);
    }

    @PostMapping("/import")
    @Operation(summary = "导入设备")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<TunnelEquipmentExcelVO> list = ExcelUtils.read(file, TunnelEquipmentExcelVO.class);
        tunnelEquipmentService.importExcel(list,updateSupport);
        return CommonResult.success("导入成功");
    }

}
