package com.mortals.xhx.module.device.service.impl;

import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.pdu.LoginForm;
import com.mortals.xhx.common.pdu.device.DeviceReq;
import com.mortals.xhx.feign.device.IDeviceFeign;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;

/**
 * DeviceService
 * 设备管理 service实现
 *
 * @author zxfei
 * @date 2023-05-22
 */
@Service("deviceService")
@Slf4j
public class DeviceServiceImpl extends AbstractCRUDServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService {

    @Autowired
    private IDeviceFeign deviceFeign;

    @Override
    protected void saveAfter(DeviceEntity entity, Context context) throws AppException {
        super.saveAfter(entity, context);
        //todo 通知设备管理系统
        String token = getToken();
        DeviceReq deviceReq = new DeviceReq();
        deviceReq.setReceiveMethod(1);
        deviceFeign.deviceCall(deviceReq,token);


    }



    @Override
    protected void updateAfter(DeviceEntity entity, Context context) throws AppException {
        super.updateAfter(entity, context);
        //todo 通知设备管理系统
    }

    @Override
    public void active(String deviceCode, Context context) {

    }

    @Override
    public void deviceEnabled(Long id, Integer status, Context context) {

    }

    private String getToken() {
        LoginForm loginForm = new LoginForm();
        loginForm.setLoginName("admin");
        loginForm.setPassword("xhx@yb123");
        Rest<String> rest = deviceFeign.getToken(loginForm);
        String token = rest.getData();
        return token;
    }
}