package org.dromara.inspection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.inspection.domain.ArDevice;
import org.dromara.inspection.domain.bo.ArDeviceBo;
import org.dromara.inspection.domain.vo.ArDeviceVo;
import org.dromara.inspection.mapper.ArDeviceMapper;
import org.dromara.inspection.service.IArDeviceService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * AR设备Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@RequiredArgsConstructor
@Service
public class ArDeviceServiceImpl implements IArDeviceService {

    private final ArDeviceMapper baseMapper;

    @Override
    public ArDeviceVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public TableDataInfo<ArDeviceVo> queryPageList(ArDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArDevice> lqw = buildQueryWrapper(bo);
        Page<ArDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<ArDeviceVo> queryList(ArDeviceBo bo) {
        return baseMapper.selectVoList(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<ArDevice> buildQueryWrapper(ArDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArDevice> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), ArDevice::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), ArDevice::getDeviceNo, bo.getDeviceNo());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceModel()), ArDevice::getDeviceModel, bo.getDeviceModel());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ArDevice::getStatus, bo.getStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ArDevice::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(ArDevice::getCreateTime);
        return lqw;
    }

    @Override
    public Boolean insertByBo(ArDeviceBo bo) {
        ArDevice add = MapstructUtils.convert(bo, ArDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ArDeviceBo bo) {
        ArDevice update = MapstructUtils.convert(bo, ArDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ArDevice entity) {
        // 校验设备编号唯一性
        if (StringUtils.isNotBlank(entity.getDeviceNo())) {
            LambdaQueryWrapper<ArDevice> lqw = Wrappers.lambdaQuery();
            lqw.eq(ArDevice::getDeviceNo, entity.getDeviceNo());
            if (entity.getId() != null) {
                lqw.ne(ArDevice::getId, entity.getId());
            }
            long count = baseMapper.selectCount(lqw);
            if (count > 0) {
                throw new ServiceException("设备编号已存在!");
            }
        }
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 校验是否有权限删除
            List<ArDevice> list = baseMapper.selectByIds(ids);
            if (list.size() != ids.size()) {
                throw new ServiceException("您没有删除权限!");
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
