package com.ruoyi.dz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dz.domain.*;
import com.ruoyi.dz.mapper.DzAgentMapper;
import com.ruoyi.dz.mapper.DzSchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dz.mapper.DzCardsOpenMapper;
import com.ruoyi.dz.service.IDzCardsOpenService;

/**
 * 开卡申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class DzCardsOpenServiceImpl implements IDzCardsOpenService 
{
    @Autowired
    private DzCardsOpenMapper dzCardsOpenMapper;
    @Autowired
    private DzAgentMapper dzAgentMapper;
    @Autowired
    private DzSchoolMapper dzSchoolMapper;

    /**
     * 查询开卡申请
     * 
     * @param id 开卡申请主键
     * @return 开卡申请
     */
    @Override
    public DzCardsOpen selectDzCardsOpenById(Long id)
    {
        return dzCardsOpenMapper.selectDzCardsOpenById(id);
    }

    /**
     * 查询开卡申请列表
     * 
     * @param dzCardsOpen 开卡申请
     * @return 开卡申请
     */
    @Override
    public List<DzCardsOpen> selectDzCardsOpenList(DzCardsOpen dzCardsOpen)
    {
        return fillNames(dzCardsOpenMapper.selectDzCardsOpenList(dzCardsOpen));
    }

    private List<DzCardsOpen> fillNames(List<DzCardsOpen> list) {
        Set<Long> schoolIdSet = Sets.newHashSet();
        Set<Long> agentIdSet = Sets.newHashSet();
        for(DzCardsOpen o : list) {
            if(null != o.getSchoolId()) {
                schoolIdSet.add(o.getSchoolId());
            }
            if(null != o.getAgentId()) {
                agentIdSet.add(o.getAgentId());
            }
        }
        Map<Long, String> schoolMap = !schoolIdSet.isEmpty() ? dzSchoolMapper.selectDzSchoolListByIds(schoolIdSet).stream().collect(Collectors.toMap(DzSchool::getId, DzSchool::getName)) : Maps.newHashMap();;
        Map<Long, String> agentMap = !agentIdSet.isEmpty() ? dzAgentMapper.selectDzAgentByAgentIds(agentIdSet).stream().collect(Collectors.toMap(DzAgent::getAgentId, DzAgent::getName)) : Maps.newHashMap();
        for(DzCardsOpen o : list) {
            o.setSchoolName(schoolMap.get(o.getSchoolId()));
            o.setAgentName(agentMap.get(o.getAgentId()));
        }
        return list;
    }

    /**
     * 新增开卡申请
     * 
     * @param dzCardsOpen 开卡申请
     * @return 结果
     */
    @Override
    public int insertDzCardsOpen(DzCardsOpen dzCardsOpen)
    {
        dzCardsOpen.setCreateTime(DateUtils.getNowDate());
        return dzCardsOpenMapper.insertDzCardsOpen(dzCardsOpen);
    }

    /**
     * 修改开卡申请
     * 
     * @param dzCardsOpen 开卡申请
     * @return 结果
     */
    @Override
    public int updateDzCardsOpen(DzCardsOpen dzCardsOpen)
    {
        return dzCardsOpenMapper.updateDzCardsOpen(dzCardsOpen);
    }

    /**
     * 批量删除开卡申请
     * 
     * @param ids 需要删除的开卡申请主键
     * @return 结果
     */
    @Override
    public int deleteDzCardsOpenByIds(Long[] ids)
    {
        return dzCardsOpenMapper.deleteDzCardsOpenByIds(ids);
    }

    /**
     * 删除开卡申请信息
     * 
     * @param id 开卡申请主键
     * @return 结果
     */
    @Override
    public int deleteDzCardsOpenById(Long id)
    {
        return dzCardsOpenMapper.deleteDzCardsOpenById(id);
    }
}
