package com.xy.service;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.dto.DeviceSysinfoDto;
import com.xy.entity.DeviceSim;
import com.xy.entity.DeviceSysinfo;
import com.xy.mapper.DeviceSysinfoMapper;
import com.xy.utils.Emptys;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

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

/**
 * <p>
 * 设备-系统信息 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2022-12-23
 */
@Slf4j
@Service
@Api(tags = "设备-系统信息")
@AllArgsConstructor
public class DeviceSysinfoServiceImpl extends ServiceImpl<DeviceSysinfoMapper, DeviceSysinfo> implements DeviceSysinfoService {

    private DeviceSimServiceImpl deviceSimService;

    @PostMapping("obj")
    @ApiOperation("对象查询")
    public R<DeviceSysinfoDto.Vo> get(@RequestBody DeviceSysinfoDto.Vo vo) {
        LambdaQueryWrapper<DeviceSysinfo> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(vo, DeviceSysinfo.class).build();
        List<DeviceSysinfo> list = list(lambdaQueryWrapper);
        if (!Emptys.check(list)) {
            return R.ok(new DeviceSysinfoDto.Vo());
        }
        return R.ok(copy(DeviceSysinfoDto.Vo.class, list.get(0)));
    }

    @PostMapping("get")
    @ApiOperation("集合查询")
    public R<List<DeviceSysinfoDto.Vo>> list(@RequestBody DeviceSysinfoDto.SelectList selectList) {
        LambdaQueryWrapper<DeviceSysinfo> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceSysinfo.class)
                .in(DeviceSysinfo::getDeviceId, selectList.getDeviceIds())
                .in(DeviceSysinfo::getDeviceSn, selectList.getDeviceSns())
                .build();
        List<DeviceSysinfo> list = list(lambdaQueryWrapper);
        return R.ok(copy(DeviceSysinfoDto.Vo.class, list));
    }

    @PostMapping("up")
    @ApiOperation("上报信息")
    public R up(@RequestBody DeviceSysinfoDto.Up up) {
        //上报信息
        log.info("设备系统信息上报：{}", JSONUtil.toJsonStr(up));
        updateById(copy(DeviceSysinfo.class, up));
        //同步流量卡
        DeviceSysinfo deviceSysinfo = getById(up.getDeviceId());
        String simIccid = deviceSysinfo.getSimIccid();
        if (Emptys.check(simIccid)) {
            LocalDateTime now = LocalDateTime.now();
            DeviceSim deviceSim = deviceSimService.getById(simIccid);
            if (deviceSim == null) {
                deviceSim = new DeviceSim()
                        .setId(simIccid)
                        .setType(deviceSysinfo.getSimIsp())
                        .setCreateTime(now)
                        .setUpdateTime(now);
                deviceSimService.save(deviceSim);
            }
        }
        return R.ok();
    }
}
