package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.service.EmployeeService;
import com.ym.mec.biz.service.IndexBaseMonthDataService;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.common.controller.BaseController;

@Api(tags = "首页")
@RequestMapping()
@RestController
public class IndexController extends BaseController {

	@Autowired
	private MusicGroupDao musicGroupDao;
	
	@Autowired
	private TeacherDao teacherDao;
	
	@Autowired
	private StudentRegistrationDao studentRegistrationDao;
	
	@Autowired
	private VipGroupDao vipGroupDao;
	
	@Autowired
	private DemoGroupDao demoGroupDao;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Autowired
	private EmployeeDao employeeDao;

	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private SysUserCashAccountDetailDao sysUserCashAccountDetailDao;
	
	@Autowired
	private StudentPaymentOrderDao studentPaymentOrderDao;

	@Autowired
	private IndexBaseMonthDataService indexService;

	@ApiOperation(value = "获取首页数据")
	@GetMapping("/index")
	public Object index() {

		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null || sysUser.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		Integer userId = sysUser.getId();
		Employee employee = employeeDao.get(userId);
		String organId = employee.getOrganIdList();
		if (sysUser.getIsSuperAdmin()) {
			organId = null;
		}else if(StringUtils.isEmpty(organId)){
			return failed(HttpStatus.INTERNAL_SERVER_ERROR, "分部信息错误");
		}

		Map<String, Integer> musicDatas = musicGroupDao.queryOrganMusicGroupNum(organId);
		
		Map<String, Integer> teacherDatas = teacherDao.queryOrganTeacherNum(organId);
		
		Map<String, Integer> studentDatas = studentRegistrationDao.queryStudentNum(organId);
		
		Map<String, Integer> vipDatas = vipGroupDao.queryVipGroupNum(organId);

		Map<String, Integer> demoDatas = demoGroupDao.queryDemoGroupNum(organId);
		
		Integer musicGroupNum = musicGroupDao.queryCurrentMonthCoursesNum(organId);
		
		Integer vipGroupNum = vipGroupDao.queryCurrentMonthCoursesNum(organId);
		
		Integer demoGroupNum = demoGroupDao.queryCurrentMonthCoursesNum(organId);
		
		Map<String, Object> incomeExpendMap = sysUserCashAccountDetailDao.queryStatisticsWithCurrentMonth(organId);
		
		Map<String, Object> incomeMap = studentPaymentOrderDao.queryIncomeStatisticsWithCurrentMonth(organId);
		
		ModelMap model = new ModelMap();
		model.put("musicDatas", musicDatas);
		model.put("teacherDatas", teacherDatas);
		model.put("studentDatas", studentDatas);
		model.put("vipDatas", vipDatas);
		model.put("demoDatas", demoDatas);
		model.put("musicGroupNum", musicGroupNum);
		model.put("vipGroupNum", vipGroupNum);
		model.put("demoGroupNum", demoGroupNum);
		model.put("incomeExpendMap", incomeExpendMap);
		model.put("incomeMap", incomeMap);

		return succeed(model);
	}

	@GetMapping("/newIndex")
	public HttpResponseResult newIndex(String dataTypes, String organIds, Integer year){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		if (!sysUser.getIsSuperAdmin()) {
			Employee employee = employeeService.get(sysUser.getId());
			if (StringUtils.isBlank(organIds)) {
				organIds = employee.getOrganIdList();
			}else if(StringUtils.isEmpty(employee.getOrganIdList())){
				return failed("用户所在分部异常");
			}else {
				List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
				if(!list.containsAll(Arrays.asList(organIds.split(",")))){
					return failed("非法请求");
				}
			}
		}
		return succeed(indexService.getIndexBaseData(dataTypes , organIds, year));
	}
}
