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

import cn.estsh.i3plus.core.api.iservice.busi.ISysLocaleResourceService;
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.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysLocaleLanguage;
import cn.estsh.i3plus.pojo.platform.bean.SysLocaleResource;
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.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description : 系统资源
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2018-11-12 14:46
 * @Modify:
 **/
@Service
public class SysLocaleResourceService implements ISysLocaleResourceService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysLocaleLanguageService.class);

    @Autowired
    private SysLocaleResourceRepository sysLocaleResourceRDao;

    @Autowired
    private SysLocaleLanguageRepository sysLocaleLanguageRDao;

    @Override
    @ApiOperation(value = "新增系统资源")
    public SysLocaleResource insertSysLocaleResource(SysLocaleResource sysLocaleResource) {
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE SysLocaleResource：{}",sysLocaleResource);
        int count = sysLocaleResourceRDao.findByPropertyCount(new String[]{"languageCode","resourceKey"},
                        new String[]{sysLocaleResource.getLanguageCode(),sysLocaleResource.getResourceKey()});
        if (count > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("语言资源已存在")
                    .setErrorSolution("请重新输入语言代码与资源键")
                    .build();
        }

        // 设置语言名称冗余
        SysLocaleLanguage sysLocaleLanguage = sysLocaleLanguageRDao.getByProperty("languageCode",
                                sysLocaleResource.getLanguageCode());
        ValidatorBean.checkNotNull(sysLocaleLanguage,"选中的语言不存在");
        sysLocaleResource.setLanguageNameRdd(sysLocaleLanguage.getLanguageName());

        return sysLocaleResourceRDao.insert(sysLocaleResource);
    }

    @Override
    @ApiOperation(value = "修改系统资源",notes = "修改系统资源")
    public void updateSysLocaleResource(SysLocaleResource sysLocaleResource) {
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE SysLocaleResource：{}",sysLocaleResource);
            String hqlPack = CoreHqlPack.packHqlSysLocaleResourceKey(sysLocaleResource);
            int count = sysLocaleResourceRDao.findByHqlWhereCount(hqlPack);
            if (count > 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("语言资源已存在")
                        .setErrorSolution("请重新输入语言代码与资源键")
                        .build();
        }

        // 设置语言名称冗余
        SysLocaleLanguage sysLocaleLanguage = sysLocaleLanguageRDao.getByProperty("languageCode",
                sysLocaleResource.getLanguageCode());
        ValidatorBean.checkNotNull(sysLocaleLanguage,"选中的语言不存在");
        sysLocaleResource.setLanguageNameRdd(sysLocaleLanguage.getLanguageName());

        sysLocaleResourceRDao.update(sysLocaleResource);
    }

    @Override
    @ApiOperation(value = "根据id删除系统资源",notes = "根据id删除系统资源")
    public void deleteSysLocaleResourceById(Long id) {
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE id：{}",id);
        sysLocaleResourceRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "查询全部系统资源",notes = "查询全部系统资源")
    public List listSysLocaleResource() {
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE list");
        return sysLocaleResourceRDao.list();
    }

    @Override
    @ApiOperation(value = "系统资源复杂查询，分页，排序",notes = "系统资源复杂查询，分页，排序")
    public ListPager querySysLocaleResourceByPager(SysLocaleResource sysLocaleResource, Pager pager) {
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE SysLocaleResource：{}，Pager：{}",sysLocaleResource,pager);
        if(sysLocaleResourceRDao == null) {
            pager = PagerHelper.getPager(pager, sysLocaleResourceRDao.listCount());
            return new ListPager(sysLocaleResourceRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysLocaleResource(sysLocaleResource);
            pager = PagerHelper.getPager(pager, sysLocaleResourceRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysLocaleResourceRDao.findByHqlWherePage(hqlPack + sysLocaleResource.orderBy(),pager),pager);
        }
    }

    @Override
    @ApiOperation(value = "根据id批量删除",notes = "根据id批量删除")
    public void deleteSysLocaleResourceByIds(Long[] ids) {
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE ids：{}",ids);
        sysLocaleResourceRDao.deleteByIds(ids);
    }

    @Override
    @ApiOperation(value = "批量添加")
    public void insertSysLocaleResourceList(List<SysLocaleResource> sysLocaleResourcesList) {
        // 添加校验
        SysLocaleResource sysLocaleResource = sysLocaleResourcesList.get(0);
        LOGGER.info("系统资源 SYS_LOCALE_RESOURCE SysLocaleResource：{}",sysLocaleResource);
        int count = sysLocaleResourceRDao.findByPropertyCount(new String[]{"languageCode","resourceKey"},
                new String[]{sysLocaleResource.getLanguageCode(),sysLocaleResource.getResourceKey()});
        if (count > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("语言资源已存在")
                    .setErrorSolution("请重新输入语言代码与资源键")
                    .build();
        }

        // 设置语言名称冗余
        SysLocaleLanguage sysLocaleLanguage = sysLocaleLanguageRDao.getByProperty("languageCode",
                sysLocaleResource.getLanguageCode());
        ValidatorBean.checkNotNull(sysLocaleLanguage,"选中的语言不存在");
        sysLocaleResource.setLanguageNameRdd(sysLocaleLanguage.getLanguageName());

        sysLocaleResourceRDao.saveAll(sysLocaleResourcesList);
    }

    @Override
    @ApiOperation(value = "根据语言code和资源key查询资源")
    public SysLocaleResource getSysLocaleResourceByLanguageCodeAndResKey(String languageCode,String resKey) {
        return sysLocaleResourceRDao.getByProperty(new String[]{"languageCode","resourceKey"},new String[]{languageCode,resKey});
    }

    @Override
    public SysLocaleResource getSysLocaleResourceById(Long id) {
        return sysLocaleResourceRDao.getById(id);
    }
}
