package com.securitycontrol.background.service.impl;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.securitycontrol.background.mapper.DeviceMapper;
import com.securitycontrol.background.service.DeviceService;
import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.utils.aes.StringHelper;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.entity.background.dto.DeviceDto;
import com.securitycontrol.entity.background.vo.DeviceVo;
import com.securitycontrol.entity.system.SystemGlobal;
import com.securitycontrol.system.api.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.model.SingleXmlCells;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 黑子
 */
@Service
@Slf4j
public class DeviceServiceImpl  implements DeviceService {

    @Autowired
    private DeviceMapper mapper;

    @Override
    public List<DeviceVo> getDeviceList(DeviceDto dto) {
        return mapper.getDeviceList(dto);

    }

    /**
     * 新增设备接口
     * @param dto
     * @return
     */
    @Override
    public Result<String> addDevice(DeviceVo dto) {
          try{
              if(StringHelper.isEmpty(dto.getPuId())){
                 return Result.fail("设备编码不能为空");
              }
              if(StringHelper.isEmpty(dto.getGbCode())){
                  return  Result.fail("国标编码不能为空");
              }
              if(StringHelper.isEmpty(dto.getMacId())){
                  return  Result.fail("设备编码不能为空");
              }
              if(dto.getIdx()<0){
                  return  Result.fail("摄像头编码不能小于0");
              }
              if(StringHelper.isEmpty(dto.getDeviceName())){
                  return  Result.fail("设备名称不能为空");
              }
             String result= checkData(dto);
              if(StringHelper.isNotEmpty(result)){
                  return  Result.fail(result);
              }
              dto.setDelFlag(0);
              if(StringHelper.isEmpty(dto.getDeviceId())){
                  dto.setDeviceId(IdUtils.getUUId());
                  Integer num= mapper.insertData(dto);
                  if(num==1) {
                      return    Result.ok("新增成功","新增成功");
                  }
              }else {
                  Integer num= mapper.updateData(dto);
                  if(num==1) {
                      return   Result.ok("修改成功","修改成功");
                  }
              }
              return  Result.fail("系统升级中,请稍后重试");
          }catch (Exception e){
              log.error(e.toString(),e);
              return  Result.fail("系统升级中,请稍后重试");
          }
    }

    /**
     *
     * @param dto
     * @return
     */
    @Override
    public Result<String> updateDevice(DeviceVo dto) {
        return addDevice(dto);
    }

    /**
     *
     * @param dto
     * @return
     */
    @Override
    public Result<String> deleteDevice(DeviceVo dto) {
          try{
              if(StringHelper.isEmpty(dto.getDeviceId())){
                  return  Result.fail("数据不存在");
              }
               int num=mapper.deleteDevice(dto);
              if(num==1) {
                  return   Result.ok("删除成功","删除成功");
              }else {
                  return   Result.fail("设备已被删除或设备不存在","设备已被删除或设备不存在");
              }
          }catch (Exception e){
              log.error(e.toString(),e);
              return   Result.fail("删除失败");
          }
    }

    @Override
    public Result<DeviceVo> getDeviceDetail(DeviceDto dto) {
          try{
              if(StringHelper.isEmpty(dto.getDeviceId())){
                  return   Result.fail("设备id不能为空");
              }
              DeviceVo vo= mapper.getDeviceDetail(dto);
              return   Result.ok(vo);
          }catch (Exception e){
              log.error(e.toString(),e);
          }
        return   Result.ok(new DeviceVo());
    }

    /**
     *
     * @param dto
     * @return
     */
    @Override
    public Result<List<DeviceVo>> getDeviceListByType(DeviceDto dto) {
      try{
          List<DeviceVo> list= mapper.getDeviceListByType(SystemGlobal.AQM_TYPE);
          return Result.ok(list);
      }catch (Exception e){
          log.error(e.toString(),e);
          return Result.fail(new ArrayList<DeviceVo>());
      }

    }

    public  String checkData(DeviceVo deviceVo){
            try{
                Integer num=mapper.getDeviceNumByQuery(deviceVo,"1");
                if(num> SystemGlobal.MIN_NUM){
                        return "设备名称已存在";
                }
                 num=mapper.getDeviceNumByQuery(deviceVo,"2");
                if(num>SystemGlobal.MIN_NUM){
                        return "设备编码及摄像头已存在";
                }
                 num=mapper.getDeviceNumByQuery(deviceVo,"3");
                if(num>SystemGlobal.MIN_NUM){
                    return "国标编码已存在";
                }
                 num=mapper.getDeviceNumByQuery(deviceVo,"4");
                if(num>SystemGlobal.MIN_NUM){
                    return "macId已存在";
                }

            }catch (Exception e){
                log.error(e.toString(),e);
                return "数据校验异常,请稍后重试";
            }
              return null;
    }
}
