package com.ruoyi.voluntary.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.voluntary.domain.BusiSchools;
import com.ruoyi.voluntary.domain.SysLocations;
import com.ruoyi.voluntary.mapper.SysLocationsMapper;
import com.ruoyi.voluntary.service.ISysLocationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 位置编码Service业务层处理
 *
 * @author mingxue
 * @date 2021-06-12
 */
@Service
public class SysLocationsServiceImpl implements ISysLocationsService
{
    @Autowired
    private SysLocationsMapper sysLocationsMapper;
    private static ConcurrentHashMap<Long, SysLocations> dbCache = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<Long, SysLocations> getDbCache() {
        return dbCache;
    }

    @PostConstruct
    public void reloadData() {
        List<SysLocations> utcConfigList = sysLocationsMapper.selectSysLocationsList(new SysLocations());
        dbCache.clear();
        for (SysLocations uc : utcConfigList) {
            dbCache.put(uc.getId(), uc);
        }
    }

    @Override
    public Map<Long, SysLocations> getAllConfig() {
        Map<Long, SysLocations> results = new HashMap<>();
        results.putAll(dbCache);
        return results;
    }

    /**
     * 查询位置编码
     *
     * @param id 位置编码ID
     * @return 位置编码
     */
    @Override
    public SysLocations selectSysLocationsById(Long id)
    {
        return sysLocationsMapper.selectSysLocationsById(id);
    }

    @Override
    public List<SysLocations> selectSysLocationsByCode(String code) {
        return sysLocationsMapper.selectSysLocationsByCode(code);
    }

    @Override
    public SysLocations selectSysLocationsBySname(String sname) {
        return sysLocationsMapper.selectSysLocationsBySname(sname);
    }

    /**
     * 查询位置编码列表
     *
     * @param sysLocations 位置编码
     * @return 位置编码
     */
    @Override
    public List<SysLocations> selectSysLocationsList(SysLocations sysLocations)
    {
        return sysLocationsMapper.selectSysLocationsList(sysLocations);
    }

    /**
     * 新增位置编码
     *
     * @param sysLocations 位置编码
     * @return 结果
     */
    @Override
    public int insertSysLocations(SysLocations sysLocations)
    {

        int result = sysLocationsMapper.insertSysLocations(sysLocations);
        reloadData();
        return result;
    }

    /**
     * 修改位置编码
     *
     * @param sysLocations 位置编码
     * @return 结果
     */
    @Override
    public int updateSysLocations(SysLocations sysLocations)
    {
        int result =sysLocationsMapper.updateSysLocations(sysLocations);
        reloadData();
        return result;
    }

    /**
     * 批量删除位置编码
     *
     * @param ids 需要删除的位置编码ID
     * @return 结果
     */
    @Override
    public int deleteSysLocationsByIds(Long[] ids)
    {
        return sysLocationsMapper.deleteSysLocationsByIds(ids);
    }

    /**
     * 删除位置编码信息
     *
     * @param id 位置编码ID
     * @return 结果
     */
    @Override
    public int deleteSysLocationsById(Long id)
    {
        return sysLocationsMapper.deleteSysLocationsById(id);
    }

    @Override
    public String getNameByLocationId(Long locationId)
    {
        SysLocations locations = dbCache.get(locationId);
        if(StringUtils.isNotNull(locations)){
            return locations.getSname();
        }
        return null;
    }

    @Override
    public String getProCityArea(BusiSchools school)
    {
        return this.getNameByLocationId(school.getPro())
                + this.getNameByLocationId(school.getCity())
                + this.getNameByLocationId(school.getArea());
    }
}
