package com.project.zcustom.service.unit.impl;

import com.project.zcustom.domain.addional.LargeCamera;
import com.project.zcustom.mapper.unit.LargeCameraMapper;
import com.project.zcustom.service.unit.ILargeCameraService;
import com.project.common.core.domain.search.PageParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;

import java.util.List;

/**
 * 摄像头Service业务层处理
 *
 * @author change
 * @date 2024-12-30
 */
@Service
public class LargeCameraServiceImpl extends ServiceImpl<LargeCameraMapper, LargeCamera> implements ILargeCameraService {

    @Autowired
    private LargeCameraMapper largeCameraMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeCamera> getLambdaQueryWrapper(LargeCamera largeCamera) {
        LambdaQueryWrapper<LargeCamera> lqw = new LambdaQueryWrapper<LargeCamera>();
                        if (StringUtils.isNotBlank(largeCamera.getCameraName())){
                        lqw.like(LargeCamera::getCameraName ,largeCamera.getCameraName());
                    }
                        if (StringUtils.isNotNull(largeCamera.getProjectId())){
                        lqw.eq(LargeCamera::getProjectId ,largeCamera.getProjectId());
                    }
                        if (StringUtils.isNotBlank(largeCamera.getType())){
                        lqw.eq(LargeCamera::getType ,largeCamera.getType());
                    }
                        if (StringUtils.isNotBlank(largeCamera.getStatus())){
                        lqw.eq(LargeCamera::getStatus ,largeCamera.getStatus());
                    }
                        if (StringUtils.isNotBlank(largeCamera.getOnlineFlag())){
                        lqw.eq(LargeCamera::getOnlineFlag ,largeCamera.getOnlineFlag());
                    }
                        if (StringUtils.isNotBlank(largeCamera.getConnectFlag())){
                        lqw.eq(LargeCamera::getConnectFlag ,largeCamera.getConnectFlag());
                    }
        return lqw;
    }

    /**
     * 分页查询摄像头列表
     *
     * @param largeCamera 摄像头
     * @return 摄像头集合
     */
    @Override
    public IPage<LargeCamera> pagingLargeCameraList(LargeCamera largeCamera) {
        IPage<LargeCamera> page = new Page<>(largeCamera.getPageNum(), largeCamera.getPageSize());
        LambdaQueryWrapper<LargeCamera> lqw = getLambdaQueryWrapper(largeCamera);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCamera::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询摄像头列表
     *
     * @param lqw
     * @return 摄像头集合
     */
    @Override
    public IPage<LargeCamera> pagingLargeCameraList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeCamera> lqw) {
        IPage<LargeCamera> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCamera::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询摄像头列表
     *
     * @param largeCamera 摄像头
     * @return 摄像头
     */
    @Override
    public List<LargeCamera> selectLargeCameraList(LargeCamera largeCamera) {
        LambdaQueryWrapper<LargeCamera> lqw = getLambdaQueryWrapper(largeCamera);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCamera::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询摄像头列表
     *
     * @param largeCamera 摄像头
     * @return 摄像头
     */
    @Override
    public List<LargeCamera> selectLargeCameraList(LargeCamera largeCamera,Long limitNum) {
        LambdaQueryWrapper<LargeCamera> lqw = getLambdaQueryWrapper(largeCamera);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCamera::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 摄像头
     */
    @Override
    public LargeCamera getLargeCameraByField(String field, String filedValue) {
        QueryWrapper<LargeCamera> lqw = new QueryWrapper<LargeCamera>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 摄像头
     */
    @Override
    public List<LargeCamera> selectLargeCameraListByField(String field, String filedValue,Long limitNum) {
        QueryWrapper<LargeCamera> lqw = new QueryWrapper<LargeCamera>();
        lqw.eq(field, filedValue);
        lqw.last("limit "+limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 摄像头
     */
    @Override
    public List<LargeCamera> selectLargeCameraListByField(String field, String filedValue) {
        QueryWrapper<LargeCamera> lqw = new QueryWrapper<LargeCamera>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field 字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue,String tips, Long id){
        QueryWrapper<LargeCamera> lqw = new QueryWrapper<LargeCamera>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }
    /**
     *  真删接口
     */
    @Override
    public void reallyDeleteLargeCamera(LambdaQueryWrapper<LargeCamera> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    public List<LargeCamera> getCameraList(Long projectId){
        return largeCameraMapper.getCameraList(projectId);
    }

    public int getCameraNum(){
        return largeCameraMapper.getCameraNum();
    }
    public List<LargeCamera> getConnectCameraNumByProject(Long projectId){
        return largeCameraMapper.getConnectCameraNumByProject(projectId);
    }
    public int getConnectOnlineCameraNum(){
        return largeCameraMapper.getConnectOnlineCameraNum();
    }
}
