package com.iformall.controller;

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

import com.iformall.domain.po.WxTagsGroup;
import com.iformall.domain.po.WxTagsType;
import com.iformall.domain.vo.WxTagsGroupVo;
import com.iformall.domain.vo.WxTagsTypeVo;
import com.iformall.service.WxTagsGroupService;
import com.iformall.service.WxTagsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxTags;
import com.iformall.service.WxCUserTagsService;
import com.iformall.service.WxTagsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("wxTags")
@Api(description="标签弹窗接口")
public class WxTagsController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxTagsService wxTagsService;

	@Autowired
	private WxTagsGroupService wxTagsGroupService;

	@Autowired
	private WxTagsTypeService wxTagsTypeService;

	@Autowired
	private WxCUserTagsService wxCUserTagsService;

    @GetMapping("getAllList")
    @ApiOperation("标签弹窗接口")
    public ResultData getAllList() {
		logger.debug("[" + getIpAddr() + "] WxTagsController::getAllList");
    	List<WxTagsGroupVo> groupVos = new ArrayList<>();
    	List<WxTagsGroup> groups = wxTagsGroupService.findList(null);
    	for(WxTagsGroup tg:groups) {
			WxTagsGroupVo tgvo =new WxTagsGroupVo();
			tgvo.setId(tg.getId());
			tgvo.setName(tg.getName());

			WxTagsType type = new WxTagsType();
			type.setGroupId(tg.getId());
			List<WxTagsTypeVo> typeVos =new ArrayList<>();
			List<WxTagsType> types = wxTagsTypeService.findList(type);
    		for(WxTagsType tt:types) {
				WxTagsTypeVo ttvo = new WxTagsTypeVo();
				ttvo.setId(tt.getId());
				ttvo.setName(tt.getName());
				ttvo.setFlag(tt.getFlag());
				ttvo.setGroupId(tg.getId());
				ttvo.setGroupName(tg.getName());

    			WxTags t = new WxTags();
    			t.setTypeId(tt.getId());
    			ttvo.setTags(wxTagsService.findList(t));
    			typeVos.add(ttvo);
    		}
    		tgvo.setTypes(typeVos);
			groupVos.add(tgvo);
    	}
    	return new ResultData(groupVos);
    }

    @ApiOperation("查询微信用户人群")
    @GetMapping("findCUserCountByTag")
    public Result findCUserCountByTag(Long[] tagIds) {
		logger.debug("[" + getIpAddr() + "] WxTagsController::findCUserCountByTag");
    	return new ResultData(wxCUserTagsService.findTagListFromCUser(getTenantId(), Arrays.asList(tagIds)));
    }

	@ApiOperation("查询会员用户人群")
	@GetMapping("findCountByTag")
	public Result findCountByTag(Long[] tagIds) {
		logger.debug("[" + getIpAddr() + "] WxTagsController::findCountByTag");
		return new ResultData(wxCUserTagsService.findTagList(getTenantId(), Arrays.asList(tagIds)));
	}


}
