/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.service.impl.patrol;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codvision.auth.entity.vo.SysUserVO;
import com.codvision.auth.support.UserContext;
import com.codvision.basic.entity.patrol.PatrolPoint;
import com.codvision.basic.entity.vo.patrol.PatrolPointVO;
import com.codvision.basic.mapper.patrol.PatrolPointMapper;
import com.codvision.basic.mapper.patrol.PatrolPointRouteRelationMapper;
import com.codvision.basic.service.patrol.PatrolPointService;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.QueryPageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author codvision
 */
@Service
@RequiredArgsConstructor
public class PatrolPointServiceImpl extends ServiceImpl<PatrolPointMapper, PatrolPoint> implements PatrolPointService {

    private final PatrolPointRouteRelationMapper routeRelationMapper;

    @Override
    public IPage<PatrolPoint> getPageResult(QueryPage<Map<String, Object>> queryPage) {
        Map<String, Object> paraMap = QueryPageUtil.queryMap(queryPage);
        Integer oid = (Integer) paraMap.get("oid");
        if (oid == null) {
            SysUserVO sysUserVO = UserContext.get();
            paraMap.put("oid", sysUserVO.getActiveOid());
        }
        return getBaseMapper().getPageResult(queryPage.getPage(), paraMap);
    }

    @Override
    public List<PatrolPointVO> getPointByRecordId(Integer recordId, Integer routeId) {
        if (routeId != null) {
            return getBaseMapper().getPointByRecordIdAndRouteId(recordId, routeId);
        } else {
            return getBaseMapper().getPointByRecordId(recordId, UserContext.get().getActiveOid());
        }
    }

    @Override
    public void batchDel(List<Integer> pointIds) {
        if (routeRelationMapper.countByPointIds(pointIds) > 0) {
            throw new BusinessException("存在点位在路线中，无法删除！");
        }

        getBaseMapper().deleteBatchIds(pointIds);
    }
}
