package cn.iocoder.yudao.module.inspection.service.impl;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.util.DateUtils;
import cn.iocoder.yudao.module.inspection.entity.WarnMessage;
import cn.iocoder.yudao.module.inspection.service.IWarnMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.iocoder.yudao.module.inspection.mapper.InspectionEquInfoMapper;
import cn.iocoder.yudao.module.inspection.entity.InspectionEquInfo;
import cn.iocoder.yudao.module.inspection.service.IInspectionEquInfoService;

/**
 * equInfoService业务层处理
 *
 * @author zcy
 * @date 2023-10-13
 */
@Service
public class InspectionEquInfoServiceImpl extends ServiceImpl<InspectionEquInfoMapper,InspectionEquInfo> implements IInspectionEquInfoService
{
    @Autowired
    private IWarnMessageService messageService;

    /**
     * 查询equInfo
     *
     * @param id equInfo主键
     * @return equInfo
     */
    @Override
    public InspectionEquInfo selectInspectionEquInfoById(Long id)
    {
        return baseMapper.selectInspectionEquInfoById(id);
    }

    /**
     * 查询equInfo列表
     *
     * @param inspectionEquInfo equInfo
     * @return equInfo
     */
    @Override
    public IPage<InspectionEquInfo> selectInspectionEquInfoList(Page page,InspectionEquInfo inspectionEquInfo)
    {
        return baseMapper.selectInspectionEquInfoList(page,inspectionEquInfo);
    }

    /**
     * 新增equInfo
     *
     * @param inspectionEquInfo equInfo
     * @return 结果
     */
    @Override
    public int insertInspectionEquInfo(InspectionEquInfo inspectionEquInfo)
    {
        int insert = baseMapper.insert(inspectionEquInfo);
        if (ObjectUtil.isNotEmpty(inspectionEquInfo.getNextCheckTime())){
            //同时处理提醒信息
            WarnMessage warnMessage =new WarnMessage();
            warnMessage.setTitle("设备检测即将到期");
            warnMessage.setContent("名为:"+inspectionEquInfo.getEquName()+"的设备即将到设定的检测时间,请及时处理相关事项！");
            warnMessage.setIsRead("0");
            warnMessage.setPartnerId(inspectionEquInfo.getPartnerId());
            warnMessage.setType("equ");
            warnMessage.setObjectId(inspectionEquInfo.getId());
            // 创建Calendar对象
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(inspectionEquInfo.getNextCheckTime());
            calendar.add(Calendar.MONTH, -1);
            Date newDate = calendar.getTime();
            warnMessage.setWarnTime(newDate);
            messageService.save(warnMessage);
        }
        return insert;
    }

    /**
     * 修改equInfo
     *
     * @param inspectionEquInfo equInfo
     * @return 结果
     */
    @Override
    public int updateInspectionEquInfo(InspectionEquInfo inspectionEquInfo)
    {
        LambdaQueryWrapper<WarnMessage> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(WarnMessage::getType,"equ").eq(WarnMessage::getObjectId,inspectionEquInfo.getId());
        WarnMessage one = messageService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(one)){
            //更新处理
            messageService.removeById(one.getId());
        }
        if (ObjectUtil.isNotEmpty(inspectionEquInfo.getNextCheckTime())){
            //同时处理提醒信息
            WarnMessage warnMessage =new WarnMessage();
            warnMessage.setTitle("设备检测即将到期");
            warnMessage.setContent("名为:"+inspectionEquInfo.getEquName()+"的设备即将到设定的检测时间,请及时处理相关事项！");
            warnMessage.setIsRead("0");
            warnMessage.setPartnerId(inspectionEquInfo.getPartnerId());
            warnMessage.setType("equ");
            warnMessage.setObjectId(inspectionEquInfo.getId());
            // 创建Calendar对象
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(inspectionEquInfo.getNextCheckTime());
            calendar.add(Calendar.MONTH, -1);
            Date newDate = calendar.getTime();
            warnMessage.setWarnTime(newDate);
            messageService.save(warnMessage);
        }
        return baseMapper.updateById(inspectionEquInfo);
    }

    /**
     * 批量删除equInfo
     *
     * @param ids 需要删除的equInfo主键
     * @return 结果
     */
    @Override
    public int deleteInspectionEquInfoByIds(Long[] ids)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除equInfo信息
     *
     * @param id equInfo主键
     * @return 结果
     */
    @Override
    public int deleteInspectionEquInfoById(Long id)
    {
        LambdaQueryWrapper<WarnMessage> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(WarnMessage::getType,"file").eq(WarnMessage::getObjectId,id);
        WarnMessage one = messageService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(one)){
            //更新处理
            messageService.removeById(one.getId());
        }
        return baseMapper.deleteById(id);
    }
}
