package com.psdc.service.impl;

import com.psdc.entity.PsdcControlLog;
import com.psdc.entity.PsdcDevice;
import com.psdc.entity.vo.DeviceStatusVo;
import com.psdc.exception.ControlException;
import com.psdc.mapper.PsdcControlLogMapper;
import com.psdc.mapper.PsdcDeviceMapper;
import com.psdc.service.IPsdcDeviceService;
import com.psdc.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PsdcDeviceServiceImpl implements IPsdcDeviceService{

    @Autowired
    private PsdcDeviceMapper psdcDeviceMapper;

    @Autowired
    private PsdcControlLogMapper psdcControlLogMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param deviceId 主键
     * @return 实例对象
     */
    public PsdcDevice queryById(Integer deviceId){
        return psdcDeviceMapper.queryById(deviceId);
    }

    /**
     * 根据用户id查询设备列表
     * @return
     */
    public List<PsdcDevice> queryByUserId(){
        return psdcDeviceMapper.queryByUserId(SecurityUtils.getUserId());
    }

    public List<DeviceStatusVo> queryDeviceStatus(){
        List<PsdcDevice> psdcDevices = psdcDeviceMapper.queryByUserId(SecurityUtils.getUserId());
        return psdcDevices.stream().map(psdcDevice -> {
            DeviceStatusVo deviceStatusVo = new DeviceStatusVo();
            BeanUtils.copyProperties(psdcDevice, deviceStatusVo);
            return deviceStatusVo;
        }).collect(Collectors.toList());
    }

    /**
     * 新增数据
     *
     * @param psdcDevice 实例对象
     * @return 实例对象
     */
    public PsdcDevice insert(PsdcDevice psdcDevice){
        psdcDeviceMapper.insert(psdcDevice);
        return psdcDevice;
    }

    /**
     * 更新数据
     *
     * @param psdcDevice 实例对象
     * @return 实例对象
     */
    public PsdcDevice update(PsdcDevice psdcDevice){
        psdcDeviceMapper.update(psdcDevice);
        return queryById(psdcDevice.getDeviceId());
    }

    /**
     * 通过主键删除数据
     *
     * @param deviceId 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer deviceId){
        int total = psdcDeviceMapper.deleteById(deviceId);
        return total > 0;
    }

    /**
     * 更新设备状态
     * @param deviceId
     * @param runStatus
     * @return
     */
    @Override
    public int updateDeviceRunStatus(Integer deviceId, Integer runStatus) {
        return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId,runStatus);
    }

    /**
     * 控制设备启动停止
     * @param deviceId
     * @param runStatus
     * @return
     */
    @Override
    public int controlDeviceStartAndStop(Integer deviceId, Integer runStatus) {

        String s = "";
        if(runStatus == 1){
            s = "启动";
        } else if ( runStatus == 2 ) {
            s = "停止";
        }
        log.info("设备id：{}",deviceId);

        PsdcDevice psdcDevice = psdcDeviceMapper.queryById(deviceId);
        if(psdcDevice == null){
            psdcControlLogMapper.insert(new PsdcControlLog(deviceId,null,null,"设备启停",s,2,"未找到该设备",SecurityUtils.getUsername()));
            throw new ControlException("控制失败，未找到该设备");
        }

        //TODO 发送MQTT指令
        // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),"设备启停",s,2,"等待终端响应超时",SecurityUtils.getUsername()));

        //发送成功
        psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),"设备启停",s,2,"控制成功",SecurityUtils.getUsername()));
        return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId,runStatus);
    }


}