package org.springblade.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.DictCodeConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.enterprise.entity.Enterprise;
import org.springblade.enterprise.service.IEnterpriseService;
import org.springblade.enterprise.vo.ChartDataVO;
import org.springblade.enterprise.vo.EnterpriseBizTypePieDataVO;
import org.springblade.system.entity.DictBiz;
import org.springblade.system.feign.IDictBizClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author cy-computer
 */
@RestController
@AllArgsConstructor
@RequestMapping("/enterprisestatistics")
@Api(value = "", tags = "接口")
public class EnterpriseStatisticsController extends BladeController {

	private final IEnterpriseService enterpriseService;
	private final IDictBizClient dictBizClient;
	/**
	 * 入驻企业数量【web端智能运营中心】
	 */
	@GetMapping("/count")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "/**", notes = "传入energyStatistics")
	public R count() {
		return R.data(enterpriseService.count());
	}

	/**
	 * 企业业务类型统计【web端智能运营中心】
	 */
	@GetMapping("/bizType")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "/**")
	public R bizType() {
		List<DictBiz> dictList = dictBizClient.getList(DictCodeConstant.ENTERPRISE_BIZ_TYPE).getData();
		Assert.isTrue(CollUtil.isNotEmpty(dictList),DictCodeConstant.DICT_LIST_IS_EMPTY);
		//获取饼状图的legendData
		List<String> legendData = dictList.stream().map(DictBiz::getDictValue).collect(Collectors.toList());
		//获取饼状图的data
		List<ChartDataVO> chartData = this.getChartData(legendData);

		return R.data(EnterpriseBizTypePieDataVO.builder()
			.legendData(legendData)
			.data(chartData)
			.build());
	}

	private List<ChartDataVO> getChartData(List<String> bizTypes){
		String OTHER_BIZ_TYPE = "其他";

		List<ChartDataVO> list = new ArrayList<>();
		//获取所有的企业
		List<Enterprise> enterpriseList = enterpriseService.list();
		for (String bizType : bizTypes) {
			long count=0;
			if (CollUtil.isNotEmpty(enterpriseList)) {
				count = enterpriseList.stream().filter(item -> {
					//企业未设置业务类型也归置为其他分类
					if (StrUtil.isBlank(item.getBizType())) {
						item.setBizType(OTHER_BIZ_TYPE);
					}
					return bizType.equals(item.getBizType());
				}).count();
			}
			ChartDataVO chartDataVO = ChartDataVO.builder().name(bizType).value(count).build();
			list.add(chartDataVO);
		}
		return list;
	}

}
