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.DeviceSimPickRecordsDto;
import com.xy.entity.DeviceSimPickRecords;
import com.xy.mapper.DeviceSimPickRecordsMapper;
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 hechunping
* @since 2024-08-22
*/
@Service
@AllArgsConstructor
@Api(tags = "物联网卡加油包记录")
public class DeviceSimPickRecordsServiceImpl extends ServiceImpl<DeviceSimPickRecordsMapper, DeviceSimPickRecords> implements DeviceSimPickRecordsService {

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

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

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

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

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