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.DeviceSetRecordsDto;
import com.xy.entity.DeviceSetRecords;
import com.xy.mapper.DeviceSetRecordsMapper;
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.ArrayList;
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-04-11
 */
@Service
@AllArgsConstructor
@Api(tags = "设备设置记录")
public class DeviceSetRecordsServiceImpl extends ServiceImpl<DeviceSetRecordsMapper, DeviceSetRecords> implements DeviceSetRecordsService {

    @PostMapping("lastOne")
    @ApiOperation("返显最后一条记录")
    public R<List<DeviceSetRecordsDto.LastOneVo>> lastOne(@RequestBody DeviceSetRecordsDto.LastOneDto dto) {
        List<DeviceSetRecordsDto.LastOneVo> voList = new ArrayList<>();
        dto.getTypeList().forEach(type -> {
            LambdaQueryWrapper<DeviceSetRecords> lqw = new LambdaQueryWrapper<DeviceSetRecords>()
                    .eq(DeviceSetRecords::getDeviceId, dto.getDeviceId())
                    .eq(DeviceSetRecords::getType, type)
                    .orderByDesc(DeviceSetRecords::getCreateTime)
                    .last("limit 1");
            DeviceSetRecords one = getOne(lqw);
            if (Emptys.check(one)) {
                DeviceSetRecordsDto.LastOneVo vo = new DeviceSetRecordsDto.LastOneVo()
                        .setType(type)
                        .setContent(one.getContent());
                voList.add(vo);
            }
        });
        return R.ok(voList);
    }


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

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

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

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

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