package com.bonus.bracelet.service.impl;

import com.bonus.bracelet.mapper.DeviceMapper;
import com.bonus.bracelet.mapper.SidebandMapper;
import com.bonus.bracelet.service.IDeviceService;
import com.bonus.bracelet.service.ISidebandService;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.bracelet.vo.DeviceVo;
import com.bonus.common.entity.bracelet.vo.ProjectPowerVo;
import com.bonus.common.entity.bracelet.vo.SidebandDeviceVo;
import com.bonus.common.entity.bracelet.vo.SidebandVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * DeviceServiceImpl
 */
@Service(value = "IDeviceService")
@Slf4j
public class DeviceServiceImpl implements IDeviceService {

    @Resource(name = "DeviceMapper")
    private DeviceMapper deviceMapper;

    @Override
    public List<DeviceVo> getDeviceLists(DeviceVo data) {
        List<DeviceVo> list = new ArrayList<>();
        try {
            list = deviceMapper.getDeviceLists(data);
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getLyType()==0){
                    String name = deviceMapper.getDeviceLyName(list.get(i).getLyId());
                    list.get(i).setLyName(name);
                }else if(list.get(i).getLyType()==1){
                    String name = deviceMapper.getDeviceLyNameTemp(list.get(i).getLyId());
                    list.get(i).setLyName(name);
                }
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    /**
     * 根据边带id获得详细信息
     * @param deviceId
     * @return
     */
    @Override
    public AjaxResult selectDeviceById(Integer deviceId) {
        try {
            DeviceVo vo = deviceMapper.selectDeviceById(deviceId);
            return AjaxResult.success(vo);
        } catch (Exception e) {
            log.error("设备详情", e);
            return AjaxResult.error();
        }
    }


    /**
     * 新增设备信息
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addDevice(DeviceVo vo) {
        try {
            DeviceVo result = deviceMapper.isNameExist(vo);
            if (result != null) {
                return AjaxResult.error("设备名称已存在");
            }
            int num =  deviceMapper.addDevice(vo);
            int numly = deviceMapper.getDeviceLy(vo.getDeviceId());
            if(numly==0){
                deviceMapper.addDeviceLy(vo.getDeviceId());
            }
            if(num==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("设备信息新增失败");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editDevice(DeviceVo vo) {
        try {
            DeviceVo result = deviceMapper.isExist(vo);
            if (result != null) {
                return AjaxResult.error("设备名称已存在");
            }

           int num =  deviceMapper.updateDevice(vo);
            if(num==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("设备信息修改失败");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteDeviceById(int deviceId) {
        try {
            int result = deviceMapper.deleteDeviceById(deviceId);
            int numly = deviceMapper.getDeviceLy(deviceId);
            if(numly==1){
                deviceMapper.deleteDeviceLy(deviceId);
            }
            if(result==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("设备信息删除失败");
        } catch (Exception e) {
            log.error(e.toString(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }
}
