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

import cn.estsh.i3plus.core.api.iservice.busi.ISysLocaleLanguageService;
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.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 13:48
 * @Modify:
 **/
@Service
public class SysLocaleLanguageService implements ISysLocaleLanguageService {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysLocaleLanguageService.class);

    @Autowired
    private SysLocaleLanguageRepository sysLocaleLanguageRDao;

    @Autowired
    private SysLocaleResourceRepository sysLocaleResourceRDao;

    @Override
    @ApiOperation(value = "添加系统语言",notes = "添加系统语言")
    public SysLocaleLanguage insertSysLocaleLanguage(SysLocaleLanguage sysLocaleLanguage) {
        LOGGER.info("系统语言 SYS_LOCALE_LANGUAGE SysLocaleLanguage:{}",sysLocaleLanguage);
        int count = sysLocaleLanguageRDao.findByPropertyCount("languageCode",sysLocaleLanguage.getLanguageCode());
        if (count > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("语言代码已存在")
                    .setErrorSolution("请重新输入语言代码")
                    .build();
        }

        return sysLocaleLanguageRDao.insert(sysLocaleLanguage);
    }

    @Override
    @ApiOperation(value = "修改系统语言",notes = "修改系统语言")
    public void updateSysLocaleLanguage(SysLocaleLanguage sysLocaleLanguage) {
        SysLocaleLanguage data = sysLocaleLanguageRDao.getById(sysLocaleLanguage.getId());

        String hqlPack = CoreHqlPack.packHqlSysLocaleLanguageCode(sysLocaleLanguage);
        int count = sysLocaleLanguageRDao.findByHqlWhereCount(hqlPack);
        if (count > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("语言代码已存在")
                    .setErrorSolution("请重新输入语言代码")
                    .build();
        }

        // 根据资源中的语言代码和名称
        sysLocaleResourceRDao.updateByProperties(
                "languageCode", data.getLanguageCode(),
                new String[]{"languageCode", "languageNameRdd"},
                new Object[]{sysLocaleLanguage.getLanguageCode(), sysLocaleLanguage.getLanguageName()}
        );

        sysLocaleLanguageRDao.update(sysLocaleLanguage);
    }

    @Override
    @ApiOperation(value = "根据id删除系统语言",notes = "根据id删除系统语言")
    public void deleteSysLocaleLanguageById(Long id) {
        SysLocaleLanguage sysLocaleLanguage = sysLocaleLanguageRDao.getById(id);
        ValidatorBean.checkNotNull(sysLocaleLanguage,"需要删除的资源不存在");

        if(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == sysLocaleLanguage.getIsDefault().intValue()){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("默认语言不允许删除")
                    .build();
        } else if(sysLocaleResourceRDao.isExitByProperty("languageCode",sysLocaleLanguage.getLanguageCode())){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("存在关联资源无法删除")
                    .build();
        }

        sysLocaleLanguageRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "查询全部系统语言",notes = "查询全部系统语言")
    public List listSysLocaleLanguage() {
        LOGGER.info("系统语言 SYS_LOCALE_LANGUAGE list");
        return sysLocaleLanguageRDao.list();
    }

    @Override
    @ApiOperation(value = "系统语言复杂查询，分页，排序",notes = "系统语言复杂查询，分页，排序")
    public ListPager querySysLocaleLanguageByPager(SysLocaleLanguage sysLocaleLanguage, Pager pager) {
        if(sysLocaleLanguage == null) {
            pager = PagerHelper.getPager(pager, sysLocaleLanguageRDao.listCount());
            return new ListPager(sysLocaleLanguageRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysLocaleLanguage(sysLocaleLanguage);
            pager = PagerHelper.getPager(pager, sysLocaleLanguageRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysLocaleLanguageRDao.findByHqlWherePage(hqlPack + sysLocaleLanguage.orderBy(),pager),pager);
        }
    }

    @Override
    @ApiOperation(value = "根据id批量删除系统语言",notes = "根据id批量删除系统语言")
    public void deleteSysLocaleLanguageByIds(Long[] ids) {
        // 判断批量数据中是否存在默认语言
        List list = sysLocaleLanguageRDao.findByHqlWhere(CoreHqlPack.packHqlSysLocaleLanguageExistDefaultByIds(ids));
        if (list.size() > 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("默认语言不允许删除")
                    .build();
        } else if(sysLocaleResourceRDao.findByHqlWhereCount(CoreHqlPack.packHqlIds("id",ids)) > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("存在关联资源无法删除")
                    .build();
        }

        sysLocaleLanguageRDao.deleteByIds(ids);
    }

    @Override
    @ApiOperation(value = "设置默认语言",notes = "设置默认语言")
    public void updateSysLocaleLanguageDefaultById(Long ids) {
        sysLocaleLanguageRDao.updateByHqlWhere("and 1=1","isDefault",CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());

        sysLocaleLanguageRDao.updateByProperties("id",ids,
                "isDefault",CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
    }

    @Override
    @ApiOperation(value = "根据code查询语言")
    public SysLocaleLanguage getSysLocaleLanguageByCode(String code) {
        return sysLocaleLanguageRDao.getByProperty("languageCode",code);
    }

    @Override
    @ApiOperation(value = "根据语言id修改语言状态")
    public void updateSysLocaleLanguageStatusById(Long id, Integer status) {
        SysLocaleLanguage sysLocaleLanguage = sysLocaleLanguageRDao.getById(id);
        ValidatorBean.checkNotNull(sysLocaleLanguage,"需要删除的资源不存在");

        // 判断是否禁用默认语言
        if (CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() == status.intValue()
                && CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == sysLocaleLanguage.getIsDefault().intValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("默认语言不允许禁用")
                    .build();
        }

        sysLocaleLanguageRDao.updateByProperties("id",id,"languageStatus",status);
    }

    @Override
    @ApiOperation(value = "根据语言id批量修改语言状态")
    public void updateSysLocaleLanguageStatusByIds(Long[] ids, Integer status) {
        // 判断批量数据禁用时是否存在默认语言
        List list = sysLocaleLanguageRDao.findByHqlWhere(CoreHqlPack.packHqlSysLocaleLanguageExistDefaultByIds(ids));
        if (CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() == status.intValue() && list.size() > 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("默认语言不允许禁用")
                    .build();
        }

        sysLocaleLanguageRDao.updateByHqlWhere(CoreHqlPack.packHqlIds("id",ids),"languageStatus",status);
    }

    @Override
    @ApiOperation(value = "根据语言状态查询语言信息")
    public List<SysLocaleLanguage> findSysLocaleLanguageByStatus(Integer status) {
        return sysLocaleLanguageRDao.findByProperty("languageStatus",status);
    }

    @Override
    public SysLocaleLanguage getSysLocaleLanguageById(Long id) {
        return sysLocaleLanguageRDao.getById(id);
    }
}
