package com.xy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.dto.DeviceNetRecordDto;
import com.xy.entity.DeviceNetRecord;
import com.xy.mapper.DeviceNetRecordMapper;
import com.xy.utils.Emptys;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.PageBean;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
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.time.LocalDateTime;
import java.util.List;

import static com.xy.utils.Beans.copy;
import static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;

/**
 * <p>
 * 机器-联网记录job
 * </p>
 *
 * @author lijin
 * @since 2023-01-09
 */
@Service
@AllArgsConstructor
@Api(tags = "机器-联网记录")
public class DeviceNetRecordServiceImpl extends ServiceImpl<DeviceNetRecordMapper, DeviceNetRecord> implements DeviceNetRecordService {

    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated DeviceNetRecordDto.Save save) {
        DeviceNetRecord deviceNetRecord = copy(DeviceNetRecord.class, save)
                .setCreateTime(LocalDateTime.now());
        save(deviceNetRecord);
        return R.ok();
    }

    @Override
    @ApiOperation("分页查询")
    public R<PageBean<DeviceNetRecordDto.Vo>> page(@RequestBody DeviceNetRecordDto.Page page) {
        PageBean pageBean = page.getPage();
        LambdaQueryWrapper<DeviceNetRecord> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceNetRecord.class)
                .ge(DeviceNetRecord::getCreateTime, page.getBeginCreateTime())
                .le(DeviceNetRecord::getCreateTime, page.getEndCreateTime())
                .build()
                .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceNetRecord::getCreateTime);
        IPage<DeviceNetRecord> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
        return R.ok(toPageBean(DeviceNetRecordDto.Vo.class, iPage));
    }

    @PostMapping("list")
    @ApiOperation("集合查询")
    public R<List<DeviceNetRecordDto.Vo>> list(@RequestBody @Validated DeviceNetRecordDto.SelectList selectList) {
        LambdaQueryWrapper<DeviceNetRecord> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceNetRecord.class)
                .in(DeviceNetRecord::getId, selectList.getIds())
                .in(DeviceNetRecord::getDeviceId, selectList.getDeviceIds())
                .in(DeviceNetRecord::getMercId, selectList.getMercIds())
                .in(DeviceNetRecord::getNetType, selectList.getNetTypes())
                .build();
        List<DeviceNetRecord> list = list(lambdaQueryWrapper);
        return R.ok(copy(DeviceNetRecordDto.Vo.class, list));
    }

    @PostMapping("offLineDevice")
    @ApiOperation("离线设备列表")
    public R<List<DeviceNetRecordDto.OffLineDeviceVo>> offLineDevice(@RequestBody @Validated DeviceNetRecordDto.OffLineDevice offLineDevice) {
        List<DeviceNetRecordDto.OffLineDeviceVo> offLineDeviceVos = baseMapper.offLineDevice(offLineDevice);
        return R.ok(offLineDeviceVos);
    }
}