package com.xy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.dto.DeviceQualityValueDto;
import com.xy.entity.DeviceQualityValue;
import com.xy.mapper.DeviceQualityValueMapper;
import com.xy.utils.Emptys;
import com.xy.utils.MybatisPlusQuery;
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.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;


/**
 * <p>
 * 设备-质检值 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-01-30
 */
@Service
@AllArgsConstructor
@Api(tags = "设备-质检值")
public class DeviceQualityValueServiceImpl extends ServiceImpl<DeviceQualityValueMapper, DeviceQualityValue> implements DeviceQualityValueService {

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

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

    @PostMapping("saveOrUpdate")
    @ApiOperation("添加/修改")
    public R saveOrUpdate(@RequestBody DeviceQualityValueDto.Vo vo) {
        DeviceQualityValue deviceQualityValue = getOne(new LambdaQueryWrapper<DeviceQualityValue>()
                .eq(DeviceQualityValue::getQualityId, vo.getQualityId())
                .eq(DeviceQualityValue::getQualityType, vo.getQualityType())
        );
        //修改
        if (deviceQualityValue != null) {
            copy(deviceQualityValue, vo);
            updateById(deviceQualityValue);
            return R.ok();
        }
        //添加
        LocalDateTime now = LocalDateTime.now();
        DeviceQualityValue deviceQualityValue2 = copy(DeviceQualityValue.class, vo)
                .setCreateTime(now)
                .setUpdateTime(now);
        save(deviceQualityValue2);
        return R.ok();
    }

    @PostMapping("del")
    @ApiOperation("删除")
    public R del(@RequestBody DeviceQualityValueDto.Vo vo) {
        DeviceQualityValue deviceQualityValue = null;
        if (Emptys.check(vo.getId())) {
            deviceQualityValue = getById(vo.getId());
        }
        if (Emptys.check(vo.getQualityId()) && Emptys.check(vo.getQualityType())) {
            deviceQualityValue = getOne(new LambdaQueryWrapper<DeviceQualityValue>()
                    .eq(DeviceQualityValue::getQualityId, vo.getQualityId())
                    .eq(DeviceQualityValue::getQualityType, vo.getQualityType())
            );
        }
        if (deviceQualityValue != null) {
            removeById(deviceQualityValue.getId());
        }
        return R.ok();
    }
}