package com.ygtx.gxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.gxt.domain.GxtEquipment;
import com.ygtx.gxt.service.IGxtEquipmentService;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 设备管理Controller
 * 
 * @author ouyj
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/gxt/equipment")
@Api(tags = "设备管理")
public class GxtEquipmentController extends BaseController
{
    @Autowired
    private IGxtEquipmentService gxtEquipmentService;

    /**
     * 查询设备管理列表
     */
    @ApiOperation("查询设备管理列表")
    @GetMapping("/list")
    public TableDataInfo list(GxtEquipment gxtEquipment)
    {
        startPage();
        List<GxtEquipment> list = gxtEquipmentService.selectGxtEquipmentList(gxtEquipment);
        return getDataTable(list);
    }

    /**
     * 驾驶舱获取设备数据筛选树状图
     * @param gxtEquipment
     * @return
     */
    @ApiOperation("驾驶舱获取设备数据筛选树状图")
    @GetMapping("/listAll")
    public TableDataInfo listAll(GxtEquipment gxtEquipment)
    {
        List<GxtEquipment> list = gxtEquipmentService.selectGxtEquipmentList(gxtEquipment);
        return getDataTable(list);
    }

    /**
     * 获取所有品牌列表（去重）
     */
    @ApiOperation("获取所有品牌列表（去重）")
    @GetMapping("/brands")
    public AjaxResult listBrands()
    {
        List<String> list = gxtEquipmentService.selectBrandList();
        return AjaxResult.success(list);
    }

    /**
     * 根据品牌获取机型列表（去重）
     */
    @ApiOperation("根据品牌获取机型列表（去重）")
    @ApiImplicitParam(name = "brand", value = "品牌名称", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping("/models/{brand}")
    public AjaxResult listModelsByBrand(@PathVariable("brand") String brand)
    {
        List<String> list = gxtEquipmentService.selectModelListByBrand(brand);
        return AjaxResult.success(list);
    }

    /**
     * 获取所有维保中心列表（去重）
     */
    @ApiOperation("获取所有维保中心列表（去重）")
    @GetMapping("/maintenanceCenters")
    public AjaxResult listMaintenanceCenters()
    {
        List<String> list = gxtEquipmentService.selectMaintenanceCenterList();
        return AjaxResult.success(list);
    }

    /**
     * 根据维保中心获取场站列表（去重）
     */
    @ApiOperation("根据维保中心获取场站列表（去重）")
    @ApiImplicitParam(name = "maintenanceCenter", value = "维保中心名称", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping("/stations/{maintenanceCenter}")
    public AjaxResult listStationsByMaintenanceCenter(@PathVariable("maintenanceCenter") String maintenanceCenter)
    {
        List<String> list = gxtEquipmentService.selectStationListByMaintenanceCenter(maintenanceCenter);
        return AjaxResult.success(list);
    }

    /**
     * 获取所有机型列表（去重）
     */
    @ApiOperation("获取所有机型列表（去重）")
    @GetMapping("/models")
    public AjaxResult listAllModels()
    {
        List<String> list = gxtEquipmentService.selectAllModels();
        return AjaxResult.success(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @ApiOperation("导出设备管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxtEquipment gxtEquipment,
                       @RequestParam(value = "exportFields", required = false) String exportFieldsStr) {
        List<GxtEquipment> list = gxtEquipmentService.selectGxtEquipmentList(gxtEquipment);
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        if (exportFieldsStr != null && !exportFieldsStr.isEmpty()) {
            // 将字符串分割成数组
            String[] exportFields = exportFieldsStr.split(",");
            util.showColumn(exportFields);
        }
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理导出字段列表
     */ 
    @GetMapping("/exportFields")
    @ApiOperation("获取设备管理导出字段列表")
    public AjaxResult getExportFields() {
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        return success(util.getExportFields());
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:query')")
    @ApiOperation("获取设备管理详细信息")
    @ApiImplicitParam(name = "equipmentId", value = "设备ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return success(gxtEquipmentService.selectGxtEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @ApiOperation("新增设备管理")
    @PostMapping
    public AjaxResult add(@RequestBody GxtEquipment gxtEquipment)
    {
        gxtEquipment.setCreateBy(getUsername());
        return toAjax(gxtEquipmentService.insertGxtEquipment(gxtEquipment));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @ApiOperation("修改设备管理")
    @PutMapping
    public AjaxResult edit(@RequestBody GxtEquipment gxtEquipment)
    {
        gxtEquipment.setUpdateBy(getUsername());
        return toAjax(gxtEquipmentService.updateGxtEquipment(gxtEquipment));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @ApiOperation("删除设备管理")
    @ApiImplicitParam(name = "equipmentIds", value = "设备ID数组", required = true, dataType = "Long[]", paramType = "path", dataTypeClass = Long[].class)
    @DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(gxtEquipmentService.deleteGxtEquipmentByEquipmentIds(equipmentIds));
    }

    /**
     * 导入设备管理数据
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:import')")
    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @ApiOperation("导入设备管理数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "上传文件", required = true, dataType = "MultipartFile", paramType = "form", dataTypeClass = MultipartFile.class),
        @ApiImplicitParam(name = "updateSupport", value = "是否支持更新", required = true, dataType = "Boolean", paramType = "query", dataTypeClass = Boolean.class)
    })
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        List<GxtEquipment> equipmentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = gxtEquipmentService.importGxtEquipment(equipmentList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载设备管理导入模板
     */
    @ApiOperation("下载设备管理导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        util.importTemplateExcel(response, "设备管理数据");
    }
}