package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.EduOrganStudentDataDto;
import com.ym.mec.biz.dal.enums.IndexErrorType;
import com.ym.mec.biz.dal.page.IndexDataQueryInfo;
import com.ym.mec.biz.dal.page.OrganCloudStudyStudentDataQueryInfo;
import com.ym.mec.biz.service.IndexBaseMonthDataService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.tenant.TenantContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
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 java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@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 OrganizationService organizationService;
	@Autowired
	private SysUserCashAccountDetailDao sysUserCashAccountDetailDao;
	@Autowired
	private StudentPaymentOrderDao studentPaymentOrderDao;
	@Autowired
	private IndexBaseMonthDataService indexService;
	@Autowired
	private RedissonClient redissonClient;

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

		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null || sysUser.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		String organId = organizationService.getEmployeeOrgan("");

		Integer tenantId = TenantContextHolder.getTenantId();

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

		Map<String, Integer> demoDatas = demoGroupDao.queryDemoGroupNum(organId);
		
		Integer musicGroupNum = musicGroupDao.queryCurrentMonthCoursesNum(organId, tenantId);
		
		Integer vipGroupNum = vipGroupDao.queryCurrentMonthCoursesNum(organId, tenantId);
		
		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(IndexDataQueryInfo queryInfo){
		queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
		return succeed(indexService.getIndexBaseData(queryInfo));
	}

	@GetMapping("/hasIndexErrData")
	public HttpResponseResult hasIndexErrData(String organId){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("用户信息获取失败");
		}
		RBucket<Map<String, Boolean>> bucket = redissonClient.getBucket("index:hasIndexErrData " + sysUser.getId());
		Map<String, Boolean> map = bucket.get();
		if(map == null || map.size() == 0){
			organId = organizationService.getEmployeeOrgan(sysUser.getId(),organId,sysUser.getIsSuperAdmin());
			map = indexService.hasIndexErrData(organId);
			bucket.set(map,30, TimeUnit.MINUTES);
		}
		return succeed(map);
	}

	@GetMapping("/getIndexErrData")
	public HttpResponseResult getIndexErrData(String organId, IndexErrorType errorType){
		return succeed(indexService.getIndexErrData(organizationService.getEmployeeOrgan(organId), errorType));
	}

	@GetMapping("/exportIndexErrData")
	public HttpResponseResult exportIndexErrData(String organId, IndexErrorType errorType){
		return succeed(indexService.exportIndexErrData(organizationService.getEmployeeOrgan(organId), errorType));
	}

	@GetMapping("/getRemindMatterData")
	public HttpResponseResult getRemindMatterData(String organId){
		return succeed(indexService.getRemindMatterData(organizationService.getEmployeeOrgan(organId)));
	}

	@ApiOperation("查询分部云教练学员汇总数据")
	@GetMapping("/organStudentOverView")
	public HttpResponseResult<PageInfo<EduOrganStudentDataDto>> organStudentOverView(OrganCloudStudyStudentDataQueryInfo queryInfo){
		String organId = organizationService.getEmployeeOrgan("");
		List<Integer> organIds = Arrays.stream(organId.split(",")).map(id -> Integer.valueOf(id)).filter(id -> !OrganizationService.EXCLUDE_ORGAN_IDS.contains(id)).collect(Collectors.toList());
		return succeed(indexService.organStudentOverView(organIds, queryInfo));
	}
}
