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

import com.zcloud.personnel.positioning.persistence.dataobject.TerminalDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalCardCompanyOptionDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalCardCompanyStatDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalCardDetailDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalCardPersonOptionDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalAppCompanyStatDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalAppDetailDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalPersonSnapshotDO;
import com.zcloud.personnel.positioning.persistence.dataobject.CorpInfoSnapshotDO;
import com.zcloud.personnel.positioning.persistence.mapper.TerminalMapper;
import com.zcloud.personnel.positioning.persistence.repository.TerminalRepository;
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.List;
import java.util.Map;
import java.util.HashMap;

/**
 * web-infrastructure
 *
 * @Author fangjiakai
 * @Date 2026-07-07
 */
@Service
@RequiredArgsConstructor
public class TerminalRepositoryImpl extends BaseRepositoryImpl<TerminalMapper, TerminalDO> implements TerminalRepository {
    private final TerminalMapper terminalMapper;

    @Override
    public PageResponse<TerminalDO> listPage(Map<String, Object> params) {
        IPage<TerminalDO> iPage = new Query<TerminalDO>().getPage(params);
        QueryWrapper<TerminalDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        IPage<TerminalDO> result = terminalMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

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

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

    @Override
    public TerminalCardDetailDO getCardDetail(Map<String, Object> params) {
        return terminalMapper.getCardDetail(params);
    }

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

    @Override
    public List<TerminalCardCompanyOptionDO> listCardCompanyOptions(Map<String, Object> params) {
        return terminalMapper.listCardCompanyOptions(params);
    }

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

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

    @Override
    public TerminalAppDetailDO getAppDetail(Map<String, Object> params) {
        return terminalMapper.getAppDetail(params);
    }

    @Override
    public TerminalPersonSnapshotDO findPerson(Map<String, Object> params) {
        return terminalMapper.findPerson(params);
    }

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

    @Override
    public boolean updateBindIdCardNo(Long id, String idCardNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("idCardNo", idCardNo);
        return terminalMapper.updateBindIdCardNo(params) == 1;
    }
}
