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.DeviceFaultLogDto;
import com.xy.entity.DeviceFaultLog;
import com.xy.mapper.DeviceFaultLogMapper;
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.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 谭斌
* @since 2024-01-08
*/
@Service
@AllArgsConstructor
@Api(tags = "设备故障日志")
public class DeviceFaultLogServiceImpl extends ServiceImpl<DeviceFaultLogMapper, DeviceFaultLog> implements DeviceFaultLogService {

    @PostMapping("obj")
    @ApiOperation("对象查询")
    public R<DeviceFaultLogDto.Vo> obj(@RequestBody DeviceFaultLogDto.Vo vo) {
        DeviceFaultLogDto.SelectList selectList = copy(DeviceFaultLogDto.SelectList.class, vo);
        List<DeviceFaultLogDto.Vo> list = list(selectList).getData();
        if(Emptys.check(list)) {
            return R.ok(list.get(0));
        }
        return R.ok();
    }

    @PostMapping("list")
    @ApiOperation("集合查询")
    public R<List<DeviceFaultLogDto.Vo>> list(@RequestBody DeviceFaultLogDto.SelectList selectList) {
        LambdaQueryWrapper<DeviceFaultLog> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceFaultLog.class).build();
        List<DeviceFaultLog> list = list(lambdaQueryWrapper);
        return R.ok(copy(DeviceFaultLogDto.Vo.class, list));
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public R<PageBean<DeviceFaultLogDto.Vo>> page(@RequestBody DeviceFaultLogDto.Page page) {
        PageBean pageBean = page.getPage();
        LambdaQueryWrapper<DeviceFaultLog> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceFaultLog.class).build();
        IPage<DeviceFaultLog> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
        return R.ok(toPageBean(DeviceFaultLogDto.Vo.class, iPage));
    }

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

    @PostMapping("update")
    @ApiOperation("修改")
    public R update(@RequestBody @Validated DeviceFaultLogDto.Update update) {
        DeviceFaultLog updateInfo = copy(DeviceFaultLog.class, update);
        updateById(updateInfo);
        return R.ok();
    }
}
