package com.zcloud.personnel.positioning.persistence.repository.impl;

import com.zcloud.personnel.positioning.persistence.dataobject.FenceDO;
import com.zcloud.personnel.positioning.persistence.dataobject.CompanyOptionDO;
import com.zcloud.personnel.positioning.persistence.dataobject.FenceCompanyStatDO;
import com.zcloud.personnel.positioning.persistence.dataobject.CorpInfoSnapshotDO;
import com.zcloud.personnel.positioning.persistence.mapper.FenceMapper;
import com.zcloud.personnel.positioning.persistence.repository.FenceRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author fangjiakai
 * @Date 2026-07-07
 */
@Service
@RequiredArgsConstructor
public class FenceRepositoryImpl extends BaseRepositoryImpl<FenceMapper, FenceDO> implements FenceRepository {
    private final FenceMapper fenceMapper;

    @Override
    public PageResponse<FenceDO> listPage(Map<String, Object> params) {
        IPage<FenceDO> iPage = new Query<FenceDO>().getPage(params);
        QueryWrapper<FenceDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        if (Boolean.TRUE.equals(params.get("electronicFenceOnly"))) {
            queryWrapper.in("fence_scope", "ELECTRONIC", "WORK_AREA");
        }
        queryWrapper.orderByDesc("create_time");
        IPage<FenceDO> result = fenceMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public PageResponse<FenceCompanyStatDO> listCompanyStats(Map<String, Object> params) {
        IPage<FenceCompanyStatDO> page = new Query<FenceCompanyStatDO>().getPage(params);
        IPage<FenceCompanyStatDO> result = fenceMapper.listCompanyStats(page, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public List<CompanyOptionDO> listCompanyOptions(Map<String, Object> params) {
        return fenceMapper.listCompanyOptions(params);
    }

    @Override
    public boolean clearAreaLink(Long areaId) {
        return fenceMapper.clearAreaLink(areaId) >= 0;
    }

    @Override
    public CorpInfoSnapshotDO findCorpInfo(Map<String, Object> params) {
        return fenceMapper.findCorpInfo(params);
    }
}
