package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.StudentVipGroupShowListDto;
import com.ym.mec.biz.dal.page.StudentVipGroupQueryInfo;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dao.SysUserCashAccountDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.SysUserCashAccount;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.common.controller.BaseController;

import java.util.List;

@RequestMapping("api")
@Api(tags = "对外接口")
@RestController
public class APIController extends BaseController {

	@Autowired
	private SysUserCashAccountDao sysUserCashAccountDao;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private PracticeLessonApplyService practiceLessonApplyService;
	@Autowired
	private PracticeGroupService practiceGroupService;
	@Autowired
	private ImGroupService imGroupService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private SysTenantConfigService sysTenantConfigService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private VipGroupService vipGroupService;

	@GetMapping("/createCashAccount")
	public Boolean createCashAccount(Integer userId,Integer tenantId) {
		// 添加用户现金账户
		sysUserCashAccountDao.insert(new SysUserCashAccount(userId,tenantId, "CNY"));

		return true;
	}

	@GetMapping("/queryTeacherOrganId")
	public Integer queryTeacherOrganId(Integer userId) {
		Teacher teacher = teacherDao.get(userId);
		if (teacher != null) {
			return teacher.getTeacherOrganId();
		}
		return null;
	}

	@GetMapping("/practiceSum")
	public Object practiceSum() {
		return succeed(practiceLessonApplyService.practiceSum());
	}

	@GetMapping("/isPurchasedPracticeCourse")
	public Boolean isPurchasedPracticeCourse(Integer userId) {
		return practiceGroupService.isPurchasedPracticeCourse(userId);
	}

	@GetMapping("/updateNickName")
	public int updateNickName(Integer userId,String nickName) {
		return imGroupService.updateNickName(userId,nickName);
	}

	@GetMapping("/getTenantConfigValue")
	public String getTenantConfigValue(Integer tenantId,String paramName) {
		return sysTenantConfigService.getTenantConfigValue(paramName,tenantId);
	}

	@GetMapping("/queryOrganIdByUserId")
	public String queryOrganIdByUserId(Integer userId, String clientId) {
		if (StringUtils.equalsIgnoreCase("system", clientId) || StringUtils.equalsIgnoreCase("education", clientId)) {
			Employee employee = employeeDao.get(userId);
			if (employee != null) {
				return employee.getOrganIdList();
			}

		} else if (StringUtils.equalsIgnoreCase("teacher", clientId)) {
			Teacher teacher = teacherDao.get(userId);
			if (teacher != null) {
				return teacher.getOrganId() + "";
			}
		} else {
			SysUser user = sysUserFeignService.queryUserInfo();
			if (user != null) {
				return user.getOrganId() + "";
			}
		}
		return null;
	}

	@ApiOperation(value = "获取可购买vip、网管课列表")
	@RequestMapping(value = "/queryVipPracticeGroups")
	public Boolean queryVipPracticeGroups(){
		SysUser sysUser = sysUserService.getUser();
		StudentVipGroupQueryInfo queryInfo = new StudentVipGroupQueryInfo();
		queryInfo.setOrganId(sysUser.getOrganId());
		queryInfo.setUserId(sysUser.getId());
		List<StudentVipGroupShowListDto> listDtos = vipGroupService.queryVipPracticeGroups(queryInfo);
		return CollectionUtils.isEmpty(listDtos)?false:true;
	}

}
