package com.aegis.camera.service.impl;

import com.aegis.camera.domain.Camera;
import com.aegis.camera.mapper.CameraMapper;
import com.aegis.camera.service.ICameraService;
import com.aegis.common.exception.ServiceException;
import com.aegis.common.utils.SecurityUtils;
import com.aegis.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 摄像头Service业务层处理
 * 
 * @author aegis
 */
@Service
public class CameraServiceImpl extends ServiceImpl<CameraMapper, Camera> implements ICameraService {

    @Override
    public List<Camera> listByCondition(Camera condition) {
        LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
        if (condition == null) {
            wrapper.orderByDesc(Camera::getCreateTime);
        } else {
            wrapper.like(StringUtils.isNotBlank(condition.getCameraName()), Camera::getCameraName, condition.getCameraName());
            wrapper.like(StringUtils.isNotBlank(condition.getCameraCode()), Camera::getCameraCode, condition.getCameraCode());
            wrapper.eq(StringUtils.isNotBlank(condition.getProtocol()), Camera::getProtocol, condition.getProtocol());
            wrapper.eq(StringUtils.isNotBlank(condition.getOrgName()), Camera::getOrgName, condition.getOrgName());
            wrapper.eq(StringUtils.isNotBlank(condition.getStatus()), Camera::getStatus, condition.getStatus());
            Object beginTime = condition.getParams().get("beginTime");
            if (beginTime != null) {
                wrapper.ge(Camera::getCreateTime, beginTime);
            }
            Object endTime = condition.getParams().get("endTime");
            if (endTime != null) {
                wrapper.le(Camera::getCreateTime, endTime);
            }
            wrapper.orderByDesc(Camera::getCreateTime);
        }
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Camera camera) {
        if (camera == null) {
            throw new ServiceException("摄像头信息不能为空");
        }
        if (StringUtils.isNotBlank(camera.getCameraCode())) {
            checkCameraCodeUnique(camera.getCameraCode(), null);
        }
        applyDefaultValues(camera);
        String operator = SecurityUtils.getUsername();
        Date now = new Date();
        camera.setCreateBy(operator);
        camera.setCreateTime(now);
        return super.save(camera);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Camera camera) {
        if (camera == null) {
            throw new ServiceException("摄像头信息不能为空");
        }
        if (StringUtils.isNotBlank(camera.getCameraCode())) {
            checkCameraCodeUnique(camera.getCameraCode(), camera.getId());
        }
        String operator = SecurityUtils.getUsername();
        Date now = new Date();
        camera.setUpdateBy(operator);
        camera.setUpdateTime(now);
        return super.updateById(camera);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<?> idList) {
        return super.removeByIds(idList);
    }

    /**
     * 校验摄像头编码是否唯一
     */
    private void checkCameraCodeUnique(String cameraCode, Long excludeId) {
        LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Camera::getCameraCode, cameraCode);
        // 只校验未删除的记录，允许与已逻辑删除的数据重复
        wrapper.eq(Camera::getDelFlag, "0");
        if (excludeId != null) {
            wrapper.ne(Camera::getId, excludeId);
        }
        long count = count(wrapper);
        if (count > 0) {
            throw new ServiceException("摄像头编码已存在");
        }
    }

    /**
     * 应用默认值
     */
    private void applyDefaultValues(Camera camera) {
        if (StringUtils.isBlank(camera.getStatus())) {
            camera.setStatus("offline");
        }
    }
}