package cn.ey.biz.module.system.api.dict;

import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.util.i18n.I18nContextHolder;
import cn.ey.biz.module.system.api.dict.dto.CategoryDataDTO;
import cn.ey.biz.module.system.api.dict.dto.DictDataRespDTO;
import cn.ey.biz.module.system.convert.dict.DictDataConvert;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryDO;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryDataDO;
import cn.ey.biz.module.system.dal.dataobject.dict.DictDataDO;
import cn.ey.biz.module.system.dal.mapper.category.CategoryDataMapper;
import cn.ey.biz.module.system.dal.mapper.category.CategoryMapper;
import cn.ey.biz.module.system.enums.i18n.I18nEnum;
import cn.ey.biz.module.system.service.dict.DictDataService;
import cn.ey.biz.module.system.service.i18n.I18nService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.module.system.enums.ApiConstants.VERSION;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.ACCEPT_LANGUAGE_NOT_NULL;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class DictDataApiImpl implements DictDataApi {

    @Resource
    private DictDataService dictDataService;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryDataMapper categoryDataMapper;

    @Resource
    private I18nService i18nService;

    @Override
    public R<Boolean> validDictDatas(String dictType, Collection<String> values) {
        dictDataService.validDictDatas(dictType, values);
        return success(true);
    }

    @Override
    public R<DictDataRespDTO> getDictData(String dictType, String value) {
        DictDataDO dictData = dictDataService.getDictData(dictType, value);
        return success(DictDataConvert.INSTANCE.convert02(dictData));
    }

    @Override
    public R<DictDataRespDTO> parseDictData(String dictType, String label) {
        DictDataDO dictData = dictDataService.parseDictData(dictType, label);
        return success(DictDataConvert.INSTANCE.convert02(dictData));
    }

    @Override
    public R<List<CategoryDataDTO>> getDictDataByType(String dictType) {
        String acceptLanguage = I18nContextHolder.getLangId();
        if (StringUtils.isEmpty(acceptLanguage)) {
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }

        CategoryDO categoryDO = categoryMapper.selectOne(new LambdaQueryWrapper<CategoryDO>()
                .eq(CategoryDO::getCode, dictType));

        List<CategoryDataDO> categoryDataDOs = categoryDataMapper.selectList(new LambdaUpdateWrapper<CategoryDataDO>()
                .eq(CategoryDataDO::getCategoryCode, categoryDO.getCode()));

        return success(categoryDataDOs.stream().map(category -> {
            CategoryDataDTO dto = DictDataConvert.INSTANCE.categoryDo2Dto(category);
            dto.setName(i18nService.getLabelFromDB(Long.toString(category.getId()),
                    acceptLanguage, I18nEnum.DICT.getType()));
            return dto;
        }).collect(Collectors.toList()));
    }


}
