package com.willalp.control.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.control.domain.HsControlTiming;
import com.willalp.control.mapper.HsControlTimingMapper;
import com.willalp.control.service.IHsControlTimingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.List;

/**
 * 设备定时任务Service业务层处理
 *
 * @author willalp
 * @date 2022-02-16
 */
@Service
public class HsControlTimingServiceImpl extends ServiceImpl<HsControlTimingMapper, HsControlTiming> implements IHsControlTimingService {
    @Autowired
    private HsControlTimingMapper hsControlTimingMapper;

    /**
     * 查询设备定时任务列表
     *
     * @param hsControlTiming 设备定时任务
     * @return 设备定时任务
     */
    @Override
    public List<HsControlTiming> selectHsControlTimingList(HsControlTiming hsControlTiming) {
        return hsControlTimingMapper.selectHsControlTimingList(hsControlTiming);
    }

    @Override
    public HsControlTiming getTimingInfo(String useNoId) {
        if (StringUtils.isBlank(useNoId)) {
            throw new BaseException("control.useNoId.null", new Object[]{useNoId});
        }
        HsControlTiming timing = hsControlTimingMapper.selectOne(
                new QueryWrapper<HsControlTiming>()
                        .eq("use_no_id", useNoId));
        if (ObjectUtil.isEmpty(timing)) {
            throw new BaseException("control.selectTiming.to.useNoId.null", new Object[]{useNoId});
        }
        return timing;
    }

    @Override
    public int saveOrUpdateTiming(HsControlTiming timing) {
        if (ObjectUtil.isNull(timing)) {
            throw new BaseException("数据为空");
        }
        Long useNoId = timing.getUseNoId();
        Time startTime = timing.getStartTime();
        Time endTime = timing.getEndTime();
        if (0 == useNoId) {
            throw new BaseException("control.useNoId.null", new Object[]{useNoId});
        }
        if (null == startTime) {
            throw new BaseException("开始时间不能为空");
        }
        if (null == endTime) {
            throw new BaseException("结束时间不能为空");
        }
        QueryWrapper<HsControlTiming> qwEqUseNoId = new QueryWrapper<HsControlTiming>()
                .eq("use_no_id", useNoId);
        HsControlTiming timingDb = this.baseMapper.selectOne(qwEqUseNoId);
        if (ObjectUtil.isNull(timingDb)) {
            for (; ; ) {
                String idStr = IdWorker.getIdStr();
                HsControlTiming byId = this.baseMapper.selectById(idStr);
                if (null == byId) {
                    timing.setTimingId(Long.valueOf(idStr));
                    break;
                }
            }
            return this.baseMapper.insert(timing);
        } else {
            return this.baseMapper.update(timing, qwEqUseNoId);
        }
    }
}
