package com.sz.admin.monitor.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.monitor.mapper.BlurDetectionMapper;
import com.sz.admin.monitor.mapper.CameraSnapshotMapper;
import com.sz.admin.monitor.pojo.dto.blurDetection.BlurDetectionDTO;
import com.sz.admin.monitor.pojo.po.BlurDetection;
import com.sz.admin.monitor.service.BlurDetectionService;
import com.sz.admin.monitor.service.CameraSnapshotService;
import com.sz.core.common.exception.common.BusinessException;
import com.sz.core.util.BeanCopyUtils;
import com.sz.platform.enums.AdminResponseEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * ClassName: BlurDetectionServiceImpl
 * Package: com.sz.admin.monitor.service.impl
 * Description:
 */
@Service
public class BlurDetectionServiceImpl extends ServiceImpl<BlurDetectionMapper, BlurDetection> implements BlurDetectionService {
    @Resource
    private BlurDetectionMapper blurDetectionMapper;
    @Resource
    private CameraSnapshotService cameraSnapshotService;
    @Override
    @Transactional
    public Boolean saveOrUpdateBlur(BlurDetectionDTO blurDetectionDTO) {
        Long id = blurDetectionDTO.getId();
        Integer enable = blurDetectionDTO.getEnable();
        if(ObjUtil.isNotNull(id))
        {
            // 修改
            if(enable==0)
            {
                // 关闭，直接将enable状态修改成0
                boolean update = UpdateChain.of(BlurDetection.class)
                    .set(BlurDetection::getEnable, 0)
                    .where(BlurDetection::getId)
                    .eq(id)
                    .update();
            }else {
                //  删除之前的基准图。抓拍一张最新的基准图保存到数据库
              //  cameraSnapshotService.deleteByCameraIdAndType(cameraId,4);
              //  cameraSnapshotService.captureAndSave(cameraId, 4);
                // 保存数据库
                blurDetectionMapper.update(BeanCopyUtils.copy(blurDetectionDTO, BlurDetection.class));
            }
        }else {
            // 新增
           // cameraSnapshotService.captureAndSave(cameraId, 4);
            BlurDetection detection = BeanCopyUtils.copy(blurDetectionDTO, BlurDetection.class);
            detection.setCreateTime(LocalDateTime.now());
            detection.setUpdateTime(LocalDateTime.now());
            blurDetectionMapper.insert(detection);
        }
        return true;
    }
}
