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.DeviceLogDto;
import com.xy.entity.DeviceLog;
import com.xy.mapper.DeviceLogMapper;
import com.xy.utils.*;
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 static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;


/**
 * <p>
 * 设备-日志 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-03-16
 */
@Service
@AllArgsConstructor
@Api(tags = "设备-日志")
public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog> implements DeviceLogService {

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

    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated DeviceLogDto.Save save) {
        LocalDateTime now = LocalDateTime.now();
        DeviceLog saveInfo = Beans.copy(DeviceLog.class, save)
                .createId()
                .setCreateTime(now)
                .setUpdateTime(now);
        save(saveInfo);
        return R.ok();
    }
}