package com.xy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.annotation.LogOperate;
import com.xy.device.EnumEnvParams;
import com.xy.dto.DevicePartDto;
import com.xy.dto.MqttUserDto;
import com.xy.entity.*;
import com.xy.enums.LogEnum;
import com.xy.mapper.MqttUserMapper;
import com.xy.utils.Emptys;
import com.xy.utils.R;
import com.xy.utils.SysDictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

import static com.xy.utils.Beans.copy;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-02-17
 */
@Slf4j
@Service
@Api(tags = "mqtt认证")
@AllArgsConstructor(onConstructor_ = @Lazy)
public class MqttUserServiceImpl extends ServiceImpl<MqttUserMapper, MqttUser> implements MqttUserService {

    private DeviceInfoServiceImpl deviceInfoService;

    private DeviceSysinfoServiceImpl deviceSysinfoService;

    private DevicePartServiceImpl devicePartService;

    @PostMapping("obj")
    @ApiOperation("对象查询")
    @LogOperate(logType = LogEnum.LogType.V2, optType = LogEnum.OptType.V3, logContent = "mqtt认证查询")
    public R<MqttUserDto.ObjVo> obj(@RequestBody MqttUserDto.Obj obj) {
        MqttUser mqttUser = getOne(new LambdaQueryWrapper<MqttUser>().eq(MqttUser::getSn, obj.getSn()));
        if (!Emptys.check(mqttUser)) {
            return R.ok().setLogMsg("结果=false,sn=" + obj.getSn());
        }
        DeviceInfo deviceInfo = deviceInfoService.getById(mqttUser.getDeviceId());
        DeviceSysinfo deviceSysinfo = deviceSysinfoService.getById(mqttUser.getDeviceId());
        List<DevicePart> deviceParts = devicePartService.list(new LambdaQueryWrapper<DevicePart>().eq(DevicePart::getDeviceId, deviceInfo.getDeviceId()));
        Map<String, SysDictRedis> map = SysDictUtils.get(EnumEnvParams.Code.CODE.getCode());
        SysDictRedis mqttUrl = map.get(EnumEnvParams.MQTT_URL.getCode());
        SysDictRedis serverUrl = map.get(EnumEnvParams.SERVER_URL.getCode());
        MqttUserDto.ObjVo objVo = copy(MqttUserDto.ObjVo.class, mqttUser)
                .setAlgorithmId(deviceInfo.getAlgorithmId())
                .setDeviceType(deviceInfo.getDeviceType())
                .setScreenType(deviceSysinfo.getScreenType())
                .setDeviceModel(deviceSysinfo.getDeviceModel())
                .setDeviceParts(copy(DevicePartDto.Vo.class, deviceParts));
        objVo.setServerTime(System.currentTimeMillis());
        objVo.setIpAddress(mqttUrl.getValue());
        objVo.setServerUrl(serverUrl.getValue());
        return R.ok(objVo).setLogMsg("结果=true,sn=" + obj.getSn());
    }

    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated MqttUserDto.Save save) {
        MqttUser saveInfo = copy(MqttUser.class, save);
        save(saveInfo);
        return R.ok();
    }
}
