package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.BaseAlarm;
import com.usky.iot.domain.BaseAlarmNotice;
import com.usky.iot.domain.BaseAlarmType;
import com.usky.iot.mapper.BaseAlarmNoticeMapper;
import com.usky.iot.service.BaseAlarmNoticeService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.iot.service.vo.BaseAlarmListVO;
import com.usky.iot.service.vo.BaseAlarmTypeRequestVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 告警通知配置 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-09-19
 */
@Service
public class BaseAlarmNoticeServiceImpl extends AbstractCrudService<BaseAlarmNoticeMapper, BaseAlarmNotice> implements BaseAlarmNoticeService {


    @Override
    public void add(BaseAlarmNotice baseAlarmNotice){
        if(checkTypeUnique(baseAlarmNotice)){
            throw new BusinessException("新增告警通知失败，该告警类型已经存在通知配置");
        }
        baseAlarmNotice.setCreateBy(SecurityUtils.getUsername());
        baseAlarmNotice.setCreateTime(LocalDateTime.now());
        if (Objects.nonNull(SecurityUtils.getLoginUser().getSysUser().getDeptId())){
            baseAlarmNotice.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId().intValue());
        }
        baseAlarmNotice.setTenantId(SecurityUtils.getTenantId());
        baseAlarmNotice.setDeleteFlag(0);
        this.save(baseAlarmNotice);
    }

    @Override
    public void update(BaseAlarmNotice baseAlarmNotice){
        if(checkTypeUnique(baseAlarmNotice)){
            throw new BusinessException("修改告警通知失败，该通知配置已经存在");
        }
        baseAlarmNotice.setUpdateBy(SecurityUtils.getUsername());
        baseAlarmNotice.setUpdateTime(LocalDateTime.now());
        this.updateById(baseAlarmNotice);
    }

    @Override
    public void remove(Integer id){
        BaseAlarmNotice baseAlarmNotice = this.getById(id);
        Optional.ofNullable(baseAlarmNotice).orElseThrow(() -> new BusinessException("通知配置不存在"));
        baseAlarmNotice.setDeleteFlag(1);
        this.updateById(baseAlarmNotice);
    }

    @Override
    public CommonPage<BaseAlarmNotice> page(BaseAlarmListVO baseAlarmListVO){
        IPage<BaseAlarmNotice> page = new Page<>(baseAlarmListVO.getCurrent(),baseAlarmListVO.getSize());
        LambdaQueryWrapper<BaseAlarmNotice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(baseAlarmListVO.getAlarmType()),BaseAlarmNotice::getTypeCode,
                baseAlarmListVO.getAlarmType())
                .eq(StringUtils.isNotBlank(baseAlarmListVO.getProductCode()),BaseAlarmNotice::getProductCode,
                        baseAlarmListVO.getProductCode())
                .eq(BaseAlarmNotice::getDeleteFlag,0)
                .eq(BaseAlarmNotice::getTenantId,SecurityUtils.getTenantId())
                .orderByDesc(BaseAlarmNotice::getId);
        page  = this.page(page,queryWrapper);
        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getSize(),page.getCurrent());

    }

    @Override
    public boolean checkTypeUnique(BaseAlarmNotice baseAlarmNotice) { //根据告警类型判断唯一
        Integer id = null == baseAlarmNotice.getId() ? -1 : baseAlarmNotice.getId();
        LambdaQueryWrapper<BaseAlarmNotice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseAlarmNotice::getTypeCode,baseAlarmNotice.getTypeCode())
                .eq(BaseAlarmNotice::getDeleteFlag,0)
                .eq(BaseAlarmNotice::getTenantId,SecurityUtils.getTenantId());
        BaseAlarmNotice one = this.getOne(queryWrapper);
        return null != one && !Objects.equals(one.getId(), id);
    }
}
