package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.chenyanlogin.service.impl.ZhwlCaller;
import com.ruoyi.web.domain.entity.ZhwlDeviceListEntity;
import com.ruoyi.web.mapper.ZhwlDeviceListMapper;
import com.ruoyi.web.service.ZhwlDeviceListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@Slf4j
@Service
public class ZhwlDeviceListServiceImpl extends ServiceImpl<ZhwlDeviceListMapper, ZhwlDeviceListEntity>  implements ZhwlDeviceListService {

    @Transactional
    @Override
    public AjaxResult add(ZhwlDeviceListEntity request) {
        //入参检测
        if(ObjectUtils.isEmpty(request.getDeviceId())) {
            return error("请传入【设备编号】！");
        }
        //防重检测，
        Integer count = lambdaQuery().eq(ZhwlDeviceListEntity::getDeviceId, request.getDeviceId()).count();
        if (count != 0) {
            return error("请勿重复添加！");
        }
        return success("添加成功", baseMapper.insert(request));
    }

    @Transactional
    @Override
    public AjaxResult edit(ZhwlDeviceListEntity request) {
        //数据格式校验
        if(ObjectUtils.isEmpty(request.getDeviceId())){
            return error("待更新数据未传入有效ID，请设置数据的唯一标识ID！");
        }
        //根据数据的id更新所有不为空的字段
        return updateById(request) ? success("修改成功！") : error("修改失败，请重试！");
    }

    @Transactional
    @Override
    public AjaxResult delete(String ids) {
        String[] idList = ids.split(",");
        return success("删除成功", removeByIds(Arrays.asList(idList)));
    }

    @Override
    public AjaxResult page(ZhwlCaller.DeviceListQueryVO request) {
        //构造查询条件（检索条件未定时，先按不为空的字段进行等值查询）
        LambdaQueryWrapper<ZhwlDeviceListEntity> wrapper = new LambdaQueryWrapper<>();
        if(!ObjectUtils.isEmpty(request.getDeviceModelName())) {
            wrapper.eq(ZhwlDeviceListEntity::getDeviceModelName, request.getDeviceModelName());
        }
        if(!ObjectUtils.isEmpty(request.getState())) {
            wrapper.eq(ZhwlDeviceListEntity::getState, request.getState());
        }
        if(!ObjectUtils.isEmpty(request.getRegisterSign())) {
            wrapper.eq(ZhwlDeviceListEntity::getRegisterSign, request.getRegisterSign());
        }
        return success(baseMapper.selectPage(new Page<>(request.getPageNum(),request.getPageSize()), wrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(lambdaQuery().list());
    }

    @Transactional
    @Override
    public AjaxResult insertOrUpdate(ZhwlDeviceListEntity request) {
        Boolean result = saveOrUpdate(request);
        return success(result);
    }
}
