package com.cf.imes.module.system.api.setting;


import com.cf.imes.framework.common.enums.UserSettingTypeEnum;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.module.system.api.setting.dto.PackageConfigDTO;
import com.cf.imes.module.system.dal.dataobject.setting.PackageConfigDO;
import com.cf.imes.module.system.dal.mysql.setting.PackageConfigMapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.CURRENTLY_NO_CONFIGURATION_AVAILABLE;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class SettingApiImpl implements SettingApi{


    @Resource
    private PackageConfigMapper packageConfigMapper;


    @Override
    public PackageConfigDTO getUserSetting(Integer type) {

        LoginUser loginUser = getLoginUser();
        assert loginUser != null;
        PackageConfigDO packageConfigDO = packageConfigMapper.selectSetting(type, loginUser.getId(), loginUser.getOrganId());

        if(packageConfigDO ==  null){
            throw exception(CURRENTLY_NO_CONFIGURATION_AVAILABLE,type == 1 ? UserSettingTypeEnum.PREPACKAGED.getName(): UserSettingTypeEnum.PACKAGED.getName());
        }

        return BeanUtils.toBean(packageConfigDO,PackageConfigDTO.class);

    }
}
