package com.xzzn.web.controller.ems;

import com.xzzn.common.annotation.Log;
import com.xzzn.common.config.RuoYiConfig;
import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.common.core.page.TableDataInfo;
import com.xzzn.common.enums.BusinessType;
import com.xzzn.common.utils.file.FileUploadUtils;
import com.xzzn.common.utils.file.MimeTypeUtils;
import com.xzzn.ems.domain.EmsSiteSetting;
import com.xzzn.ems.domain.vo.DeviceUpdateRequest;
import com.xzzn.ems.domain.vo.DevicesSettingVo;
import com.xzzn.ems.domain.vo.PointDataRequest;
import com.xzzn.ems.domain.vo.PointQueryResponse;
import com.xzzn.ems.domain.vo.SiteMonitorProjectPointMappingSaveRequest;
import com.xzzn.ems.domain.vo.SiteDeviceListVo;
import com.xzzn.ems.domain.vo.WorkStatusEnumMappingSaveRequest;
import com.xzzn.ems.service.IEmsDeviceSettingService;
import com.xzzn.ems.service.IEmsSiteService;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * 站点配置
 *
 */
@RestController
@RequestMapping("/ems/siteConfig")
public class EmsSiteConfigController extends BaseController{

    @Autowired
    private IEmsSiteService iEmsSiteService;

    @Autowired
    private IEmsDeviceSettingService iEmsDeviceSettingService;

    /**
     * 获取站点列表
     */
    @GetMapping("/getSiteInfoList")
    public TableDataInfo getSiteInfoList(@RequestParam String siteName, @RequestParam String startTime, @RequestParam String endTime)
    {
        startPage();
        List<EmsSiteSetting> list = iEmsSiteService.getAllSiteInfoList(siteName,startTime,endTime);
        return getDataTable(list);
    }

    /**
     * 新增站点
     */
    @PostMapping("/addSite")
    public AjaxResult addSite(@RequestBody EmsSiteSetting emsSiteSetting)
    {
        emsSiteSetting.setCreateBy(getUsername());
        return toAjax(iEmsSiteService.addSite(emsSiteSetting));
    }

    /**
     * 编辑站点
     */
    @PostMapping("/updateSite")
    public AjaxResult updateSite(@RequestBody EmsSiteSetting emsSiteSetting)
    {
        emsSiteSetting.setUpdateBy(getUsername());
        return toAjax(iEmsSiteService.updateSite(emsSiteSetting));
    }

    /**
     * 获取设备列表-分页
     */
    @GetMapping("/getDeviceInfoList")
    public TableDataInfo getDeviceInfoList(@RequestParam(value = "siteId", required = false)  String siteId,
                                           @RequestParam(value = "deviceCategory", required = false) String deviceCategory)
    {
        startPage();
        List<SiteDeviceListVo> list = iEmsSiteService.getAllDeviceListNoDisp(siteId, deviceCategory);

        return getDataTable(list);
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/getDeviceDetailInfo")
    public AjaxResult getDeviceDetailInfo(@RequestParam Long id)
    {
        return success(iEmsDeviceSettingService.getDeviceDetailInfo(id));
    }

    /**
     * 获取设备列表-不分页
     */
    @GetMapping("/getDeviceList")
    public AjaxResult getDeviceInfoList2(@RequestParam String siteId)
    {
        return success(iEmsSiteService.getAllDeviceList(siteId, null));
    }

    /**
     * 获取所有设备类别
     */
    @GetMapping("/getDeviceCategory")
    public AjaxResult getDeviceCategory()
    {
        return success(iEmsDeviceSettingService.getDeviceCategory());
    }

    /**
     * 新增设备
     */
    @PostMapping("/addDevice")
    public AjaxResult addDevice(@RequestBody DevicesSettingVo devicesSetting)
    {
        int result = iEmsDeviceSettingService.addDevice(devicesSetting);
        if (result > 0)  {
            return AjaxResult.success(result);
        }   else  {
            return AjaxResult.error("该设备已存在");
        }
    }

    /**
     * 上传设备图片
     */
    @PostMapping("/uploadDeviceImg")
    public AjaxResult uploadDeviceImg(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getDevicePath(), file, MimeTypeUtils.IMAGE_EXTENSION);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 修改Modbus设备配置
     */
    @PostMapping("/updateDevice")
    public AjaxResult updateDevice(@RequestBody DevicesSettingVo emsDevicesSetting)
    {
        int result = iEmsDeviceSettingService.updateDevice(emsDevicesSetting);
        if (result > 0)  {
            return AjaxResult.success(result);
        }   else  if (result == -1)  {
            return AjaxResult.error("数据不存在");
        }   else  if (result == -2)  {
            return AjaxResult.error("该设备已存在");
        }
        return AjaxResult.success(result);
    }

    /**
     * 删除Modbus设备配置
     */
    @DeleteMapping("/deleteService/{id}")
    public AjaxResult deleteService(@PathVariable Long id)
    {
        return toAjax(iEmsDeviceSettingService.deleteEmsDevicesSettingById(id));
    }

    /**
     * 单个站点单个设备点位查询-点位清单
     */
    @GetMapping("/getDevicePointList")
    public TableDataInfo getDevicePointList(@Validated PointDataRequest request)
    {
        List<PointQueryResponse> result = iEmsDeviceSettingService.getSingleSiteDevicePoints(request);
        return getDataTable2(result);
    }

    /**
     * 获取指定站点下的所有设备类别
     */
    @GetMapping("/getSiteAllDeviceCategory")
    public AjaxResult getSiteAllDeviceCategory(String siteId)
    {
        return success(iEmsDeviceSettingService.getSiteAllDeviceCategory(siteId));
    }

    /**
     * 根据设备类别获取父类的设备id
     */
    @GetMapping("/getParentDeviceId")
    public AjaxResult getParentDeviceId(@RequestParam String siteId, @RequestParam String deviceCategory)
    {
        return success(iEmsSiteService.getParentCategoryDeviceId(siteId, deviceCategory));
    }

    /**
     * 获取指定站点下的指定设备类型的设备
     */
    @GetMapping("/getDeviceListBySiteAndCategory")
    public AjaxResult getDeviceListBySiteAndCategory(String siteId,String deviceCategory)
    {
        return success(iEmsDeviceSettingService.getDeviceListBySiteAndCategory(siteId, deviceCategory));
    }

    /**
     * 获取单站监控项目点位映射
     */
    @GetMapping("/getSingleMonitorProjectPointMapping")
    public AjaxResult getSingleMonitorProjectPointMapping(@RequestParam String siteId)
    {
        return success(iEmsDeviceSettingService.getSiteMonitorProjectPointMapping(siteId));
    }

    /**
     * 保存单站监控项目点位映射
     */
    @PostMapping("/saveSingleMonitorProjectPointMapping")
    public AjaxResult saveSingleMonitorProjectPointMapping(@RequestBody SiteMonitorProjectPointMappingSaveRequest request)
    {
        int rows = iEmsDeviceSettingService.saveSiteMonitorProjectPointMapping(request, getUsername());
        return AjaxResult.success(rows);
    }

    /**
     * 获取单站监控工作状态枚举映射（PCS）
     */
    @GetMapping("/getSingleMonitorWorkStatusEnumMappings")
    public AjaxResult getSingleMonitorWorkStatusEnumMappings(@RequestParam String siteId)
    {
        return success(iEmsDeviceSettingService.getSiteWorkStatusEnumMappings(siteId));
    }

    /**
     * 保存单站监控工作状态枚举映射（PCS）
     */
    @PostMapping("/saveSingleMonitorWorkStatusEnumMappings")
    public AjaxResult saveSingleMonitorWorkStatusEnumMappings(@RequestBody WorkStatusEnumMappingSaveRequest request)
    {
        int rows = iEmsDeviceSettingService.saveSiteWorkStatusEnumMappings(request.getSiteId(), request.getMappings(), getUsername());
        return AjaxResult.success(rows);
    }

    /**
     * PCS设备开关机
     */
//    @PreAuthorize("@ss.hasPermi('system:device:onAndOff')")
    @Log(title = "开关机", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeviceStatus")
    public AjaxResult updateDeviceStatus(@Valid @RequestBody DeviceUpdateRequest request)
    {
        return success(iEmsDeviceSettingService.updateDeviceStatus(request));
    }
}
