package com.tuoheng.admin.service.camera;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.common.ServiceExceptionEnum;
import com.tuoheng.admin.entity.domain.Camera;
import com.tuoheng.admin.entity.request.camera.QueryCameraListRequest;
import com.tuoheng.admin.enums.MarkTypeEnum;
import com.tuoheng.admin.mapper.CameraMapper;
import com.tuoheng.admin.service.camera.query.QueryCameraListService;
import com.tuoheng.admin.utils.GaodeUtil;
import com.tuoheng.common.common.BaseServiceImpl;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.mapper.UserMapper;
import com.tuoheng.system.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 摄像头Service业务层处理
 *
 * @team tuoheng
 * @author wanjing
 * @date 2023-02-06
 */
@Service
public class CameraServiceImpl extends BaseServiceImpl<CameraMapper, Camera> implements ICameraService {

    @Autowired
    private QueryCameraListService queryCameraListService;

    @Autowired
    private CameraMapper cameraMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询摄像头列表
     *
     * @param request 摄像头
     * @return 摄像头
     */
    @Override
    public JsonResult getList(QueryCameraListRequest request) {
        return queryCameraListService.getList(request);
    }

    /**
     * 查询摄像头
     * 
     * @param id 摄像头主键
     * @return 摄像头
     */
    @Override
    public JsonResult getOneById(Integer id) {
        return null;
    }

    @Override
    public JsonResult editInfo(Camera entity) {
        //新增
        if (StringUtils.isNull(entity.getId())) {
            if (StringUtils.isEmpty(entity.getCameraName())) {
                throw new ServiceException("摄像头名称不能为空");
            }
            if (ObjectUtil.isEmpty(entity.getCameraType())) {
                throw new ServiceException("设备类型不能为空");
            }
            if (StringUtils.isEmpty(entity.getLongitude())) {
                throw new ServiceException("经度不能为空");
            }
            if (StringUtils.isEmpty(entity.getLatitude())) {
                throw new ServiceException("纬度不能为空");
            }
            if (StringUtils.isEmpty(entity.getFlvUrl())) {
                throw new ServiceException("摄像头地址不能为空");
            }
            Integer tenantId = ShiroUtils.getTenantId();
            entity.setTenantId(tenantId);
            entity.setCreateUser(ShiroUtils.getUserId());
        }
        if(StringUtils.isNotEmpty(entity.getLongitude()) && StringUtils.isNotEmpty(entity.getLatitude())){
            String gaodeAddress = GaodeUtil.getGaodeAddress(entity.getLongitude(), entity.getLatitude());
            entity.setLocation(gaodeAddress);
        }
        entity.setUpdateUser(ShiroUtils.getUserId());
        super.edit(entity);
        return JsonResult.success();

    }

    @Override
    public JsonResult getPage(QueryCameraListRequest request) {
        if (null == request.getPage() || null == request.getLimit()) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        // 获取分页数据
        IPage<Camera> page = new Page<>(request.getPage(), request.getLimit());
        IPage<Camera> pageData = cameraMapper.selectPage(page, new LambdaQueryWrapper<Camera>()
                .eq(Camera::getMark, MarkTypeEnum.VALID.getCode())
                .eq(Camera::getTenantId, ShiroUtils.getTenantId())
                .like(StringUtils.isNotEmpty(request.getCameraName()), Camera::getCameraName, request.getCameraName())
                .orderByDesc(Camera::getCreateTime));
        for (Camera record : pageData.getRecords()) {
            User user = userMapper.selectById(record.getUpdateUser());
            if(ObjectUtil.isNotNull(user)){
                record.setUpdateUserName(user.getRealname());
            }

        }

        return JsonResult.success(pageData);
    }

}
