package com.zcloud.domain.persistence.repository.impl;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.domain.domain.enums.CommonFlagEnum;
import com.zcloud.domain.persistence.dataobject.CorpInfoDO;
import com.zcloud.domain.persistence.mapper.CorpInfoMapper;
import com.zcloud.domain.persistence.repository.CorpInfoRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:17
 */
@Service
@RequiredArgsConstructor
public class CorpInfoRepositoryImpl extends BaseRepositoryImpl<CorpInfoMapper, CorpInfoDO> implements CorpInfoRepository {
    private final CorpInfoMapper corpInfoMapper;

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

    @Override
    public Long getCorpCountByCorpName(String corpName) {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corp_name", corpName);
        return corpInfoMapper.selectCount(queryWrapper);
    }
    @Override
    public Long getTotalCount() {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("use_flag", CommonFlagEnum.YES.getCode());
        return corpInfoMapper.selectCount(queryWrapper);
    }

    @Override
    public boolean isSupper() {
        //判断当前企业是否是监管端
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", AuthContext.getTenantId());
        CorpInfoDO corpInfoDO = corpInfoMapper.selectById(AuthContext.getTenantId());
        if(corpInfoDO == null)
            throw new BizException("请登录后查看");
        if(corpInfoDO.getType() == 2)
            return true;
        return false;
    }
    @Override
    public Boolean checkCorp() {
        return AuthContext.getUserId().equals(AuthContext.getTenantId());
    }

    @Override
    public List<CorpInfoDO> getGufen() {
        QueryWrapper<CorpInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 2);
        List<CorpInfoDO> corpInfoDOS = corpInfoMapper.selectList(queryWrapper);
        return corpInfoDOS;
    }
}

