package com.huijy.management.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.thread.ThreadUtil;
import com.huijy.common.utils.DateUtils;
import com.huijy.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huijy.management.mapper.EarlyWarningMapper;
import com.huijy.management.domain.EarlyWarning;
import com.huijy.management.service.IEarlyWarningService;

/**
 * 预警信息Service业务层处理
 *
 * @author luobo
 * @date 2021-09-12
 */
@Service
public class EarlyWarningServiceImpl implements IEarlyWarningService
{
    @Autowired
    private EarlyWarningMapper earlyWarningMapper;


    /**
     * 查询预警信息
     *
     * @param earlyWarningId 预警信息主键
     * @return 预警信息
     */
    @Override
    public EarlyWarning selectEarlyWarningByEarlyWarningId(Long earlyWarningId)
    {
        return earlyWarningMapper.selectEarlyWarningByEarlyWarningId(earlyWarningId);
    }

    /**
     * 查询预警信息列表
     *
     * @param earlyWarning 预警信息
     * @return 预警信息
     */
    @Override
    public List<EarlyWarning> selectEarlyWarningList(EarlyWarning earlyWarning)
    {
        return earlyWarningMapper.selectEarlyWarningList(earlyWarning);
    }

    /**
     * 新增预警信息
     *
     * @param earlyWarning 预警信息
     * @return 结果
     */
    @Override
    public int insertEarlyWarning(EarlyWarning earlyWarning)
    {
        earlyWarning.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isEmpty(earlyWarning.getSendFlag())){
            earlyWarning.setSendFlag("0");
        }

        return earlyWarningMapper.insertEarlyWarning(earlyWarning);
    }

    /**
     * 修改预警信息
     *
     * @param earlyWarning 预警信息
     * @return 结果
     */
    @Override
    public int updateEarlyWarning(EarlyWarning earlyWarning)
    {
        return earlyWarningMapper.updateEarlyWarning(earlyWarning);
    }

    /**
     * 批量删除预警信息
     *
     * @param earlyWarningIds 需要删除的预警信息主键
     * @return 结果
     */
    @Override
    public int deleteEarlyWarningByEarlyWarningIds(Long[] earlyWarningIds)
    {
        return earlyWarningMapper.deleteEarlyWarningByEarlyWarningIds(earlyWarningIds);
    }

    /**
     * 删除预警信息信息
     *
     * @param earlyWarningId 预警信息主键
     * @return 结果
     */
    @Override
    public int deleteEarlyWarningByEarlyWarningId(Long earlyWarningId)
    {
        return earlyWarningMapper.deleteEarlyWarningByEarlyWarningId(earlyWarningId);
    }


    //发送预警信息

    @Override
    public Integer updateSendFlag(Long earlyWarningId, String sendFlag,Long adminId) {
        EarlyWarning entity = earlyWarningMapper.selectEarlyWarningByEarlyWarningId(earlyWarningId);
        if (entity!=null){
            if (entity.getSendFlag().equalsIgnoreCase(sendFlag)){
                return 0;
            }
            entity.setSendFlag(sendFlag);
            entity.setSendAt(adminId);
            entity.setSendTime(new Date());
            earlyWarningMapper.updateEarlyWarning(entity);
            return 1;
        }

        return 0;

    }



}
