package com.ruoyi.ibs.custmap.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.ibs.custmap.domain.vo.CompanyTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author 吴凯程
 * @Date 2025/5/23
 **/
@Api(tags = "地图拓客码值")
@RestController
@RequestMapping("/cust/map/metric")
public class CustMapMerticController extends BaseController {

    @Resource
    private RedisCache redisCache;

    @ApiOperation("币种码值表")
    @GetMapping("/cur")
    public R<List<SysDictData>> getCurDict(){
        List<SysDictData> sysDictTypeArrayList = new ArrayList<SysDictData>(redisCache.getCacheObject("sys_dict:cust_map_cur_metric"));
        return R.ok(sysDictTypeArrayList);
    }

    @ApiOperation("机构类型码值表")
    @GetMapping("/company/type")
    public R<List<CompanyTypeVO>> getCompanyTypeDict(){
        List<SysDictData> sysDictTypeArrayList = new ArrayList<SysDictData>(redisCache.getCacheObject("sys_dict:cust_map_company_type_metric"));
        List<CompanyTypeVO> collect = sysDictTypeArrayList.stream().map(CompanyTypeVO::new).collect(Collectors.toList());
        List<CompanyTypeVO> collect1 = collect.stream().filter(companyTypeVO -> companyTypeVO.getSort() == 1).collect(Collectors.toList());
        for (CompanyTypeVO companyTypeVO : collect1) {
            companyTypeVO.setChildren(companyTypeDfs(companyTypeVO.getValue(), companyTypeVO.getSort() + 1, collect));
        }
        return R.ok(collect1);
    }
    private List<CompanyTypeVO> companyTypeDfs(String value, Long sort, List<CompanyTypeVO> collect){
        List<CompanyTypeVO> collect1 = collect.stream().filter(companyTypeVO -> companyTypeVO.getValue().startsWith(value) && companyTypeVO.getSort() == sort)
                .collect(Collectors.toList());
        if (collect1.size() == 0){
            return null;
        }
        for (CompanyTypeVO companyTypeVO : collect1) {
            companyTypeVO.setChildren(companyTypeDfs(companyTypeVO.getValue(), companyTypeVO.getSort() + 1, collect));
        }
        return collect1;
    }

}
