package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISystemResourceService;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.platform.bean.SysLocaleLanguage;
import cn.estsh.i3plus.pojo.platform.bean.SysLocaleResource;
import cn.estsh.i3plus.pojo.platform.platrepositorymongo.SysLogOperateRepository;
import cn.estsh.i3plus.pojo.platform.platrepositorymongo.SysLogSystemRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysLocaleLanguageRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysLocaleResourceRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description : 系统资源服务
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-10-11 17:00
 * @Modify:
 **/
@Service
public class SystemResourceService implements ISystemResourceService {

    @Resource(name="redisRes")
    private ImppRedis redisRes;

    @Autowired
    private SysLocaleLanguageRepository sysLocaleLanguageRDao;

    @Autowired
    private SysLocaleResourceRepository sysLocaleResourceRDao;

    @Autowired
    private SysLogOperateRepository logOperateRDao;

    @Autowired
    private SysLogSystemRepository logSystemRDao;

    @Override
    public SysLocaleLanguage insertSysLocaleLanguage(SysLocaleLanguage lang) {
        return sysLocaleLanguageRDao.insert(lang);
    }

    @Override
    public SysLocaleLanguage updateSysLocaleLanguage(SysLocaleLanguage lang) {
        return sysLocaleLanguageRDao.save(lang);
    }

    @Override
    public void deleteSysLocaleLanguageById(long id) {
        sysLocaleLanguageRDao.deleteById(id);
    }

    @Override
    public List<SysLocaleLanguage> listSysLocaleLanguage(SysLocaleLanguage lang) {
        if(lang == null){
            return sysLocaleLanguageRDao.list();
        }else {
            return sysLocaleLanguageRDao.findByProperty(
                    new String[]{"languageCode", "languageName", "isDefault"},
                    new Object[]{lang.getLanguageCode(), lang.getLanguageName(), lang.getIsDefault()});
        }
    }

    @Override
    public ListPager<SysLocaleLanguage> listSysLocaleLanguageByPager(SysLocaleLanguage lang, Pager pager) {
        if(lang == null) {
            pager = PagerHelper.getPager(pager, sysLocaleLanguageRDao.listCount());
            return new ListPager(sysLocaleLanguageRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysLocaleLanguage(lang);
            pager = PagerHelper.getPager(pager, sysLocaleLanguageRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysLocaleLanguageRDao.findByHqlWherePage(hqlPack + lang.orderBy(),pager),pager);
        }
    }

    @Override
    public SysLocaleResource insertSysLocaleResource(SysLocaleResource res) {
        res = sysLocaleResourceRDao.insert(res);
        putResourceToCache(res);    //更新缓存

        return res;
    }

    @Override
    public SysLocaleResource updateSysLocaleResource(SysLocaleResource res) {
        res = sysLocaleResourceRDao.save(res);
        putResourceToCache(res);    //更新缓存

        return res;
    }

    private void putResourceToCache(SysLocaleResource res){
        //通过编码获取语言资源
        Map resMap = redisRes.getHashMap(res.getResourceKey());
        if(resMap != null ) {
            // 语言，国际化内容
            resMap.put(res.getLanguageCode(),res.getResourceValue());
        }else{
            resMap = new HashMap<String,String>();
            resMap.put(res.getLanguageCode(),res.getResourceValue());
            redisRes.putHashMap(res.getResourceKey(),resMap,0);
        }
    }

    @Override
    public void deleteSysLocaleResourceById(long id) {
        sysLocaleResourceRDao.deleteById(id);
    }

    @Override
    public void deleteSysLocaleResourceByIds(Long[] ids){
        sysLocaleLanguageRDao.deleteByIds(ids);
    }

    @Override
    public ListPager<SysLocaleResource> listSysLocaleResourceByPager(SysLocaleResource res, Pager pager) {
        if(res == null){
            //不传入实体对象，查询所有
            int count = (int)sysLocaleResourceRDao.listCount();
            pager = PagerHelper.getPager(pager,count);
            return new ListPager(sysLocaleResourceRDao.listPager(pager),pager);
        }else {
            //生成hql查询语句
            String hqlPack = CoreHqlPack.packHqlSysLocaleResource(res);
            pager = PagerHelper.getPager(pager,sysLocaleResourceRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysLocaleResourceRDao.findByHqlWherePage(hqlPack + res.orderBy(), pager),pager);
        }
    }

    @Override
    public List<SysLocaleResource> listSysLocaleResource(SysLocaleResource res) {
        if(res == null){
            return sysLocaleResourceRDao.list();
        }else {
            return sysLocaleResourceRDao.findByProperty(
                    new String[]{"resourceType", "languageCode", "resourceKey", "resourceValue", "isSystem"},
                    new Object[]{res.getResourceType(), res.getLanguageCode(), res.getResourceKey(), res.getResourceValue(), res.getIsSystem()});
        }
    }

    @Override
    public SysLocaleResource getSysLocaleResourceByLanguage(int resourceType,String langCode,String resKey) {
        List<SysLocaleResource> resList = sysLocaleResourceRDao.findByProperty(
                new String[]{"resourceType", "languageCode", "resourceKey"},
                new Object[]{resourceType,langCode, resKey});

        if(CollectionUtils.isEmpty(resList)){
            //不存在，则使用默认的
            resList = sysLocaleResourceRDao.findByProperty(
                    new String[]{"resourceType", "resourceKey"},
                    new Object[]{resourceType,resKey});
        }
        if(resList.size() > 0) {
            return resList.iterator().next();
        }else{
            return null;
        }
    }
}
