package com.fdkankan.ucenter.service.impl;
import java.util.Date;
import java.util.List;

import com.fdkankan.ucenter.entity.CameraIncrementLog;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.entity.UserIncrement;
import com.fdkankan.ucenter.mapper.ICameraIncrementLogMapper;
import com.fdkankan.ucenter.service.ICameraIncrementLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IUserIncrementService;
import com.fdkankan.ucenter.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-02-01
 */
@Service
public class CameraIncrementLogServiceImpl extends ServiceImpl<ICameraIncrementLogMapper, CameraIncrementLog> implements ICameraIncrementLogService {

    @Autowired
    IUserService userService;
    @Autowired
    IUserIncrementService userIncrementService;

    @Override
    public void saveLog(Long cameraId, Long incrementId, Long userId, Integer operationType) {
        CameraIncrementLog cameraIncrementLog = new CameraIncrementLog();
        cameraIncrementLog.setCameraId(cameraId);
        cameraIncrementLog.setOperationType(operationType);
        cameraIncrementLog.setOperationUser(userId);
        cameraIncrementLog.setIncrementId(incrementId);
        this.save(cameraIncrementLog);

    }

    @Override
    public void saveLog(List<Long> cameraIds, Long incrementId, Long userId, Integer operationType) {
        for (Long cameraId : cameraIds) {
            this.saveLog(cameraId,incrementId,userId,operationType);
        }
    }

    @Override
    public void saveLog(List<Long> cameraIds,String userName) {
        User user = userService.getByUserName(userName);
        if(user == null){
            return;
        }
        for (Long cameraId : cameraIds) {
            UserIncrement byCameraId = userIncrementService.getByCameraId(cameraId);
            if(byCameraId != null ){
                this.saveLog(cameraId,byCameraId.getId(),user.getId(),1);
            }
            this.saveLog(cameraId,null,user.getId(),3);
        }
    }
}
