package com.fdkankan.platform.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.platform.goods.entity.CameraSpace;
import com.fdkankan.platform.goods.mapper.ICameraSpaceMapper;
import com.fdkankan.platform.goods.service.ICameraSpaceService;
import com.fdkankan.platform.order.entity.ExpansionOrder;
import com.fdkankan.platform.user.vo.ResponseCameraSpace;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

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

    @Resource
    private ICameraSpaceMapper cameraSpaceMapper;

    @Override
    public List<CameraSpace> getListByCameraId(Long cameraId) {
        QueryWrapper<CameraSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CameraSpace::getTbStatus, TbStatus.VALID.code());
        queryWrapper.lambda().eq(CameraSpace::getCameraId,cameraId);
        return this.list(queryWrapper);
    }

    @Override
    public List<ResponseCameraSpace> getVoListByCameraId(Long cameraId) {
        return cameraSpaceMapper.getVoListByCameraId(cameraId);
    }

    @Override
    public CameraSpace getByUserIdAndCameraId(Long userId, Long cameraId) {
        QueryWrapper<CameraSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CameraSpace::getTbStatus,TbStatus.VALID.code());
        queryWrapper.lambda().eq(CameraSpace::getUserId,userId)
                .eq(CameraSpace::getCameraId,cameraId)
                .eq(CameraSpace::getSpaceType,1);
        List<CameraSpace> list = this.list(queryWrapper);
        if(list == null || list.size() <= 0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public void removeByCameraId(Long cameraId) {
        UpdateWrapper<CameraSpace> update = new UpdateWrapper<>();
        update.lambda().eq(CameraSpace::getTbStatus,TbStatus.VALID.code());
        update.lambda().eq(CameraSpace::getCameraId,cameraId);
        update.lambda().set(CameraSpace::getTbStatus,TbStatus.DELETE.code());
        this.update(update);
    }

    @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());
        this.save(cameraSpaceEntity);
        return cameraSpaceEntity;
    }

    @Override
    public List<CameraSpace> findExpiredList() {
        return cameraSpaceMapper.findExpiredList();
    }
}
