package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.ucenter.entity.CameraSpace;
import com.fdkankan.ucenter.entity.ExpansionOrder;
import com.fdkankan.ucenter.mapper.ICameraSpaceMapper;
import com.fdkankan.ucenter.service.ICameraSpaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.fdkankan.ucenter.vo.response.CameraSpaceVo;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 相机容量表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-04
 */
@Service
public class CameraSpaceServiceImpl extends ServiceImpl<ICameraSpaceMapper, CameraSpace> implements ICameraSpaceService {

    @Override
    public List<CameraSpaceVo> getVoListByCameraId(Long cameraId) {
        return this.getBaseMapper().getVoListByCameraId(cameraId);
    }

    @Override
    public HashMap<Long, CameraSpaceVo> getVoListByCameraIds(List<Long> cameraIdList) {
        HashMap<Long, CameraSpaceVo> map= new HashMap<>();
        List<CameraSpaceVo> voListByCameraIds = this.getBaseMapper().getVoListByCameraIds(cameraIdList);
        voListByCameraIds.forEach(entity ->map.put(entity.getCameraId(),entity));
        return map;
    }

    @Override
    public void removeByCameraId(Long cameraId) {
        LambdaQueryWrapper<CameraSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CameraSpace::getCameraId,cameraId);
        this.remove(wrapper);
    }

    @Override
    public CameraSpace saveByExpansionOrder(ExpansionOrder expansionOrder) {
        CameraSpace cameraSpaceEntity = new CameraSpace();
        DateTime date = null;
        if(expansionOrder.getMonth() == 12){
            date = new DateTime().plusYears(1).plusDays(expansionOrder.getDelay());
        }else {
            date = new DateTime().plusDays(expansionOrder.getMonth() * 31).plusDays(expansionOrder.getDelay());
        }
        cameraSpaceEntity.setUserId(expansionOrder.getUserId());
        cameraSpaceEntity.setOrderSn(expansionOrder.getOrderSn());
        cameraSpaceEntity.setCameraId(expansionOrder.getCameraId());
        cameraSpaceEntity.setSpace(0L);
        cameraSpaceEntity.setSpaceType(2);
        cameraSpaceEntity.setSpaceStartTime(new Date());
        cameraSpaceEntity.setSpaceEndTime(date.toDate());
        cameraSpaceEntity.setRecStatus("A");
        cameraSpaceEntity.setCreateTime(DateUserUtil.getDate(new Date()));
        cameraSpaceEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
        this.save(cameraSpaceEntity);
        return cameraSpaceEntity;
    }
}
