package com.ruoyi.voluntary.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.voluntary.dto.SchoolDto;
import com.ruoyi.voluntary.service.ISysLocationsService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.voluntary.mapper.BusiSchoolsMapper;
import com.ruoyi.voluntary.domain.BusiSchools;
import com.ruoyi.voluntary.service.IBusiSchoolsService;

/**
 * 学校Service业务层处理
 *
 * @author voluntary
 * @date 2022-11-20
 */
@Service
public class BusiSchoolsServiceImpl implements IBusiSchoolsService
{
    @Autowired
    private BusiSchoolsMapper busiSchoolsMapper;
    @Autowired
    private ISysLocationsService locationsService;

    /**
     * 查询学校
     *
     * @param id 学校主键
     * @return 学校
     */
    @Override
    public BusiSchools selectBusiSchoolsById(Long id)
    {
        return busiSchoolsMapper.selectBusiSchoolsById(id);
    }

    @Override
    public List<BusiSchools> selectBusiSchoolsByIds(List<Long> ids) {
        return busiSchoolsMapper.selectBusiSchoolsByIds(ids);
    }

    /**
     * 查询学校列表
     *
     * @param busiSchools 学校
     * @return 学校
     */
    @Override
    public List<BusiSchools> selectBusiSchoolsList(BusiSchools busiSchools)
    {
        return busiSchoolsMapper.selectBusiSchoolsList(busiSchools);
    }

    /**
     * 新增学校
     *
     * @param busiSchools 学校
     * @return 结果
     */
    @Override
    public int insertBusiSchools(BusiSchools busiSchools)
    {
        busiSchools.setCreateTime(DateUtils.getNowDate());
        return busiSchoolsMapper.insertBusiSchools(busiSchools);
    }

    /**
     * 修改学校
     *
     * @param busiSchools 学校
     * @return 结果
     */
    @Override
    public int updateBusiSchools(BusiSchools busiSchools)
    {
        busiSchools.setUpdateTime(DateUtils.getNowDate());
        return busiSchoolsMapper.updateBusiSchools(busiSchools);
    }

    /**
     * 批量删除学校
     *
     * @param ids 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deleteBusiSchoolsByIds(Long[] ids)
    {
        return busiSchoolsMapper.deleteBusiSchoolsByIds(ids);
    }

    /**
     * 删除学校信息
     *
     * @param id 学校主键
     * @return 结果
     */
    @Override
    public int deleteBusiSchoolsById(Long id)
    {
        return busiSchoolsMapper.deleteBusiSchoolsById(id);
    }

    @Override
    public List<SchoolDto> getAreaSchoolsByAgentId(Long agentId, Boolean isEcard)
    {
        if(isEcard){
            //电子卡返回所有学校
            return getAreaSchoolsByAgentId2(new BusiSchools());
        }else {
            return getAreaSchoolsByAgentId(agentId);
        }
    }

    @Override
    public List<SchoolDto> getAreaSchoolsByAgentId(Long agentId)
    {
        BusiSchools busiSchoolsCond = new BusiSchools();
        if(null==agentId){
            return Lists.newArrayList();
        }
        busiSchoolsCond.setAgentId(agentId);
        return getAreaSchoolsByAgentId2(busiSchoolsCond);
    }

    private List<SchoolDto> getAreaSchoolsByAgentId2(BusiSchools busiSchoolsCond){
        return getAreaSchoolsByAgentId3(selectBusiSchoolsList(busiSchoolsCond)) ;
    }

    public List<SchoolDto> getAreaSchoolsByAgentId3(List<BusiSchools> list){
        List<SchoolDto> schoolList = Lists.newArrayList();
        for (BusiSchools school : list) {
            if ( null == school) {
                continue;
            }
            SchoolDto schoolDto = new SchoolDto();
            schoolDto.setSchoolId(school.getId());
            schoolDto.setSchoolName(school.getName());
            String area = locationsService.getProCityArea(school);
            schoolDto.setArea( area);
            schoolDto.setPro( school.getPro());
            schoolList.add(schoolDto);
        }

        return schoolList;
    }

    /**
     * 卡只能看到分配学校
     * @param distributeSchoolId
     * @return
     */
    @Override
    public List<SchoolDto> getAreaSchoolsByDistributeSchool(Long distributeSchoolId){
        List<BusiSchools> schoolList = com.google.common.collect.Lists.newArrayList();
        BusiSchools school = busiSchoolsMapper.selectBusiSchoolsById(distributeSchoolId);
        schoolList.add(school);
        return getAreaSchoolsByAgentId3(schoolList);
    }
}
