package com.tuoheng.admin.service.iotequipment.add;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.IotEquipmentConverMapper;
import com.tuoheng.admin.entity.IotEquipment;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.IotEquipmentMapper;
import com.tuoheng.admin.request.iotequipment.AddIotEquipmentRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 物联设备 业务类
 *
 * @author wanjing
 * @since 2023-11-25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AddIotEquipmentService {

    private final IotEquipmentMapper iotEquipmentMapper;

    public JsonResult add(AddIotEquipmentRequest request) {
        User user = CurrentUserUtil.getUserInfo();
        JsonResult result = this.check(user.getTenantId(), request);
        if (0 != result.getCode()) {
            log.info("添加物联设备：校验失败：{}", result.getMsg());
            return result;
        }
        IotEquipment iotEquipment = IotEquipmentConverMapper.INSTANCE.fromAddIotEquipmentToIotEquipment(request);
        iotEquipment.setTenantId(user.getTenantId());
        iotEquipment.setCreateUser(user.getId());
        iotEquipment.setCreateTime(DateUtils.now());
        Integer rowId = iotEquipmentMapper.insert(iotEquipment);
        if (rowId <= 0) {
            log.info("添加物联设备：失败：{}", result.getMsg());
            return JsonResult.error("添加物联设备失败");
        }
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param request
     * @return
     */
    private JsonResult check(String tenantId, AddIotEquipmentRequest request) {
        if (StringUtils.isEmpty(request.getCode())) {
            throw new ServiceException("设备编码为空");
        }
        if (StringUtils.isEmpty(request.getName())) {
            throw new ServiceException("设备名称为空");
        }
        if (StringUtils.isEmpty(request.getDeptId())) {
            throw new ServiceException("部门Id为空");
        }
        if (StringUtils.isEmpty(request.getRoadId())) {
            throw new ServiceException("高速为空");
        }
        if (StringUtils.isEmpty(request.getSectionId())) {
            throw new ServiceException("路段为空");
        }
        if (StringUtils.isEmpty(request.getLocation())) {
            throw new ServiceException("位置为空");
        }
        if (StringUtils.isEmpty(request.getLongitude()) || StringUtils.isEmpty(request.getLatitude())) {
            throw new ServiceException("经纬度为空");
        }
        if (StringUtils.isEmpty(request.getPlayUrl())) {
            throw new ServiceException("设备地址为空");
        }
        IotEquipment iotEquipment = iotEquipmentMapper.selectOne(new LambdaQueryWrapper<IotEquipment>()
                .eq(IotEquipment::getTenantId, tenantId)
                .eq(IotEquipment::getDeptId, request.getDeptId())
                .eq(IotEquipment::getCode, request.getCode())
                .eq(IotEquipment::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNotEmpty(iotEquipment)) {
            throw new ServiceException("该部门下已存在该编号设备");
        }
        return JsonResult.success();
    }

}
