package com.kcim.service.impl;

import com.alibaba.fastjson2.JSON;
import com.kcim.common.constants.CommonConstant;
import com.kcim.common.constants.Constant;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.exception.CostException;
import com.kcim.dao.model.dto.SysDepartment;
import com.kcim.endPoint.CenterEndPoint;
import com.kcim.service.CenterService;
import com.kcim.vo.CommonParameterVo;
import com.kcim.vo.DictDataVo;
import com.kcim.vo.KCClassVo;
import com.kcim.vo.UserInfoVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.kcim.common.constants.Constant.CENTER_SYSTEM_ID;
import static com.kcim.common.constants.Constant.LOCAL_SYSTEM_ID;

/**
 * @program: CostAccount
 * @description: 中台调用实现类
 * @author: Wang.YS
 * @create: 2023-10-17 16:24
 **/
@Service("CenterService")
@Slf4j
@AllArgsConstructor
public class CenterServiceImpl implements CenterService {
    CenterEndPoint endPoint;

    @Override
    public List<UserInfoVO> getCenterUserInfo(String filter) {
        Object userInfo = endPoint.getUserInfo(filter);
        return JSON.parseArray(JSON.toJSONString(userInfo), UserInfoVO.class);

    }

    @Override
    public DictDataVo getDict(String dictType){
        Object dict = endPoint.getDict(dictType, LOCAL_SYSTEM_ID);
        return JSON.parseObject(JSON.toJSONString(dict), DictDataVo.class);
    }

    @Override
    public DictDataVo getCenterDict(String dictType){
        Object dict = endPoint.getDict(dictType, CENTER_SYSTEM_ID);
        return JSON.parseObject(JSON.toJSONString(dict), DictDataVo.class);
    }

    @Override
    public List<DictDataVo> getAllDict(){
        Object dict = endPoint.getAllDict();
        return JSON.parseArray(JSON.toJSONString(dict), DictDataVo.class);
    }

    @Override
    public List<DictDataVo> getAllDictBySystemId(){
        Object dict = endPoint.getAllDictBySystemId(LOCAL_SYSTEM_ID);
        return JSON.parseArray(JSON.toJSONString(dict), DictDataVo.class);
    }

    @Override
    public Map<String, String> getDepartment() {
        Object departmentFilter = endPoint.getDepartmentFilter(null);
        List<SysDepartment> sysDepartments = JSON.parseArray(JSON.toJSONString(departmentFilter), SysDepartment.class);
        return sysDepartments.stream().collect(Collectors.toMap(SysDepartment::getCode, SysDepartment::getName, (a, b) -> b));

//        return  (Map<String, String>) dict;

    }

    @Override
    public List<SysDepartment> getDepartmentFilter(String departmentName) {
        Object departmentFilter = endPoint.getDepartmentFilter(departmentName);
        return JSON.parseArray(JSON.toJSONString(departmentFilter), SysDepartment.class);
    }

    @Override
    public CommonParameterVo getParameter(Long parameterCode) {
        try {
            Object dict = endPoint.getParameter(LOCAL_SYSTEM_ID, parameterCode);
            return JSON.parseObject(JSON.toJSONString(dict), CommonParameterVo.class);
        }catch (Exception e){
            throw new CostException(e.getMessage());
        }
    }


    @Override
    public String getParameterValue(Long parameterCode) {
        try {
            Object dict = endPoint.getParameter(LOCAL_SYSTEM_ID, parameterCode);
            CommonParameterVo commonParameterVo = JSON.parseObject(JSON.toJSONString(dict), CommonParameterVo.class);
            if(ObjectUtils.isEmpty(commonParameterVo)|| StringUtils.isEmpty(commonParameterVo.getValue())) {
                return Constant.EMPTY_STR;
            }
            return  commonParameterVo.getValue();
        }catch (Exception e){
            throw new CostException(e.getMessage());
        }
    }

    /**
     * 获取指定Code的参数指定序号的数值
     * @param parameterCode
     * @param index
     * @return
     */
    @Override
    public String getParameterValueByIndex(Long parameterCode, int index) {
        try {
            if(ObjectUtils.isEmpty(index)) {
                index = NumberConstant.ZERO;
            }
            String parameterValue = getParameterValue(parameterCode);
            String[] split = parameterValue.split(CommonConstant.SEPARATOR_VERTICAL);
            if(split.length>index){
                return split[index];
            }
            return Constant.EMPTY_STR;
        }catch (Exception e){
            throw new CostException(e.getMessage());
        }
    }

    @Override
    public List<KCClassVo> getKCClass() {
        Object kcClass = endPoint.getKCClass(null);
        if(Objects.nonNull(kcClass)){
          return  JSON.parseArray(JSON.toJSONString(kcClass), KCClassVo.class);
        }
        return new ArrayList<>();
    }

    @Override
    public List<SysDepartment> getDepartmentByDepartmentIds(List<Long> departmentIds) {
        Object departmentByDepartmentIds = endPoint.getDepartmentByDepartmentIds(departmentIds);
        return JSON.parseArray(JSON.toJSONString(departmentByDepartmentIds), SysDepartment.class);
    }
}
