package com.fdkankan.ucenter.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import com.fdkankan.ucenter.common.FileUtil;
import com.fdkankan.ucenter.common.OssPath;
import com.fdkankan.ucenter.entity.CameraDetail;
import com.fdkankan.ucenter.entity.ExceedSpaceScene;
import com.fdkankan.ucenter.entity.IncrementType;
import com.fdkankan.ucenter.entity.UserIncrement;
import com.fdkankan.ucenter.mapper.IExceedSpaceSceneMapper;
import com.fdkankan.ucenter.service.IExceedSpaceSceneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IIncrementTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 超出容量场景码表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-06-30
 */
@Service
@Slf4j
public class ExceedSpaceSceneServiceImpl extends ServiceImpl<IExceedSpaceSceneMapper, ExceedSpaceScene> implements IExceedSpaceSceneService {

    @Autowired
    IIncrementTypeService incrementTypeService;
    @Autowired
    FYunFileServiceInterface fYunFileServiceInterface;

    @Override
    public List<ExceedSpaceScene> getExByCameraId(Long cameraId) {
        LambdaQueryWrapper<ExceedSpaceScene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExceedSpaceScene::getCameraId,cameraId);
        wrapper.orderByDesc(ExceedSpaceScene::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public ExceedSpaceScene getByUnicode(String unicode) {
        LambdaQueryWrapper<ExceedSpaceScene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExceedSpaceScene::getUnicode,unicode);
        List<ExceedSpaceScene> list = this.list(wrapper);
        if(list == null || list.size() <=0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public void recoveredSpaceByCameraId(Long cameraId) {

    }

    @Override
    public void recoveredSpaceByCameraId(CameraDetail cameraDetail,UserIncrement userIncrement) {
        if("SP".equals(cameraDetail.getUnit())){
            List<ExceedSpaceScene> list = this.getExByCameraId(cameraDetail.getCameraId());
            IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
            if(incrementType.getCameraSpace() == -1){
                this.recoveredSpaceAll(cameraDetail.getCameraId());
                return;
            }
            Long cameraSpace = Long.valueOf(incrementType.getCameraSpace());
            if(cameraSpace >= cameraDetail.getUsedSpace() + list.size()){
                this.recoveredSpaceAll(cameraDetail.getCameraId());
            }
            Long num = cameraSpace - cameraDetail.getUsedSpace();
            this.recoveredSpace(list,num);
        }
    }

    private void recoveredSpaceAll(Long cameraId) {
        LambdaUpdateWrapper<ExceedSpaceScene> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ExceedSpaceScene::getCameraId,cameraId);
        this.update(wrapper);
    }

    private void recoveredSpace(List<ExceedSpaceScene> list, Long num) {
        if(num <=0){
            return;
        }
        if(num >list.size()){
            num = (long) list.size();
        }
        for (int i = 0; i<num; i++){
            ExceedSpaceScene exceedSpaceScene = list.get(i);
            exceedSpaceScene.setUpdateTime(null);
            this.updateById(exceedSpaceScene);
           // this.updateOssStatus(exceedSpaceScene.getNum());
        }


    }


}
