package org.dromara.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.app.domain.AppSchedulingDevice;
import org.dromara.app.domain.bo.AppSchedulingDeviceBo;
import org.dromara.app.domain.vo.AppSchedulingDeviceVo;
import org.dromara.app.mapper.AppSchedulingDeviceMapper;
import org.dromara.app.service.IAppSchedulingDeviceService;
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.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class AppSchedulingDeviceServiceImpl implements IAppSchedulingDeviceService {

    private final AppSchedulingDeviceMapper baseMapper;

    @Override
    public AppSchedulingDeviceVo queryById(Long schedulingId) {
        return baseMapper.selectVoById(schedulingId);
    }

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

    @Override
    public List<AppSchedulingDeviceVo> queryList(AppSchedulingDeviceBo bo) {
        LambdaQueryWrapper<AppSchedulingDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppSchedulingDevice> buildQueryWrapper(AppSchedulingDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppSchedulingDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getScheduleId() != null, AppSchedulingDevice::getScheduleId, bo.getScheduleId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), AppSchedulingDevice::getDeviceNo, bo.getDeviceNo());
        return lqw;
    }

    @Override
    public Boolean insertByBo(AppSchedulingDeviceBo bo) {
        if (queryByScheduleIdAndDeviceNo(bo.getScheduleId(), bo.getDeviceNo()) != null) {
            return true;
        }
        AppSchedulingDevice add = MapstructUtils.convert(bo, AppSchedulingDevice.class);
        validEntityBeforeSave(add);
        try {
            return baseMapper.insert(add) > 0;
        } catch (DuplicateKeyException ignored) {
            return true;
        }
    }

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

    private void validEntityBeforeSave(AppSchedulingDevice entity) {
        // TODO 业务校验
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 业务校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public AppSchedulingDeviceVo queryByScheduleIdAndDeviceNo(Long scheduleId, String deviceNo) {
        return baseMapper.selectVoOne(new QueryWrapper<AppSchedulingDevice>()
            .eq("schedule_id", scheduleId)
            .eq("device_no", deviceNo));
    }

    @Override
    public Boolean deleteWithValidByScheduleIdAndDeviceNo(Long scheduleId, String deviceNo) {
        return baseMapper.delete(new QueryWrapper<AppSchedulingDevice>()
            .eq("schedule_id", scheduleId)
            .eq("device_no", deviceNo)) > 0;
    }

    @Override
    public List<AppSchedulingDeviceVo> findByDeviceNo(String deviceNo) {
        return baseMapper.selectVoList(new QueryWrapper<AppSchedulingDevice>().eq("device_no", deviceNo));
    }

    @Override
    public Set<String> findBoundDeviceNos(Collection<String> deviceNos) {
        if (deviceNos == null || deviceNos.isEmpty()) {
            return Set.of();
        }
        return baseMapper.selectList(
                new QueryWrapper<AppSchedulingDevice>()
                    .select("device_no")
                    .in("device_no", deviceNos))
            .stream()
            .map(AppSchedulingDevice::getDeviceNo)
            .filter(StringUtils::isNotBlank)
            .collect(Collectors.toSet());
    }
}
