package com.sw.mes.eam.controller;

import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.eam.service.MesToEamService;
import com.sw.mes.outside.dto.ToMESData;
import com.xxl.job.core.biz.model.ReturnT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xushu
 * @Date: 2024/02/24/15:03
 * @Description:
 */
@RestController
@RequestMapping("/eam")
@Slf4j
@Api(description = "晶科eam数据交互接口")
@Jinko(value="晶科eam对接接口控制器",author = "xushu")
public class MesToEamController {


    @Autowired
    MesToEamService mesToEamService;
    @Autowired
    private EquipmentService equipmentService;


    @ApiOperation(value = "测试-拉取同步eam的设备信息")
    @GetMapping("/syncEquipment")
    public Result syncEquipment() {
        ReturnT<String> stringReturnT;
        try {
            stringReturnT = mesToEamService.syncEquipment();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("拉取同步eam的设备信息: ",e);
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(stringReturnT);
    }


    @ApiOperation(value = "上传eam的设备预警信息")
    @PostMapping("/syncEquipmentWarningInfo")
    public Result syncEquipmentWarningInfo(@RequestBody ToMESData data) {
        try {
            mesToEamService.syncEquipmentWarningInfo(data);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传eam的设备预警信息: ",e);
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }


    @ApiOperation(value = "测试-拉取同步eam的设备状态信息")
    @GetMapping("/syncEquipmentStatus")
    public Result syncEquipmentStatus(String deviceCode) {
        try {
            Equipment equipment = equipmentService.findByCode(deviceCode);
            mesToEamService.syncEquipmentStatus(equipment);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }
}
