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.DeviceFaultMonitorConfigDto;
import com.xy.entity.DeviceFaultMonitorConfig;
import com.xy.mapper.DeviceFaultMonitorConfigMapper;
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 DeviceFaultMonitorConfigServiceImpl extends ServiceImpl<DeviceFaultMonitorConfigMapper, DeviceFaultMonitorConfig> implements DeviceFaultMonitorConfigService {

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


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

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

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

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