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.DeviceRecordsDto;
import com.xy.entity.DeviceRecords;
import com.xy.mapper.DeviceRecordsMapper;
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 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>
 * 设备操作变更记录表 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-01-11
 */
@Service
@AllArgsConstructor
@Api(tags = "设备操作变更记录表")
public class DeviceRecordsServiceImpl extends ServiceImpl<DeviceRecordsMapper, DeviceRecords> implements DeviceRecordsService {

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


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

    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated DeviceRecordsDto.Save save) {
        DeviceRecords saveInfo = copy(DeviceRecords.class, save)
                .setCreateTime(LocalDateTime.now())
                .setCreateUser(AuthorizeUtils.getLoginId(Long.class));
        save(saveInfo);
        return R.ok();
    }

}