package com.palmnest.webapp.controller.third;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.palmnest.model.third.result.ScreenDataResult;
import com.palmnest.model.third.vo.ScreenDataVO;
import com.palmnest.service.third.ThirdCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author huangxt
 * @Description 提供第三方接口控制器
 * @Date 2019/5/22 14:21
 **/
@Slf4j
@Controller
@RequestMapping("/third/*")
public class ThirdCenterController {


	@Autowired
	private ThirdCenterService thirdCenterService;

	/**
	 * 航电大屏数据展示接口
	 * 获取项目覆盖数,活跃用户数,交易金额
	 *
	 * @param orgIds
	 * @return
	 */
	@RequestMapping(value = "/hd/screenData", method = RequestMethod.POST)
	@ResponseBody
	public ScreenDataResult screenData(@RequestParam("orgIds") String[] orgIds) {

		log.info("screenData param:\n{}", JSONObject.toJSONString(orgIds));
		ScreenDataVO vo = thirdCenterService.getScreenData(orgIds);

		// 数据封装
		List<ScreenDataVO> list = new ArrayList<>();
		list.add(vo);
		return new ScreenDataResult("0", "成功", convert(vo));
	}

	private JSONArray convert(ScreenDataVO vo) {
		JSONArray  array1 = new JSONArray();
		JSONObject result = new JSONObject();

		JSONArray array2 = new JSONArray();

		JSONObject orgNum = new JSONObject();
		orgNum.put("name", "项目覆盖数量");
		JSONArray  orgNumValue = new JSONArray();
		JSONObject subOrgNum   = new JSONObject();
		subOrgNum.put("name", "数量");
		subOrgNum.put("value", vo.getOrgNum());
		orgNumValue.add(subOrgNum);
		orgNum.put("value", orgNumValue);

		JSONObject userNum = new JSONObject();
		userNum.put("name", "用户活跃数量");
		JSONArray  userNumValue = new JSONArray();
		JSONObject subUserNum   = new JSONObject();
		subUserNum.put("name", "数量");
		subUserNum.put("value", vo.getUserNum());
		userNumValue.add(subUserNum);
		userNum.put("value", userNumValue);

		JSONObject money = new JSONObject();
		money.put("name", "交易金额");
		JSONArray  moneyValue    = new JSONArray();
		JSONObject submoneyValue = new JSONObject();
		submoneyValue.put("name", "数量");
		submoneyValue.put("value", vo.getMoney());
		moneyValue.add(submoneyValue);
		money.put("value", moneyValue);

		array2.add(orgNum);
		array2.add(userNum);
		array2.add(money);
		result.put("dataValue", array2);
		array1.add(result);
		return array1;
	}
}
