package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.entity.VipGroupCategory;
import com.ym.mec.biz.service.VipGroupCategoryService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

@Api(tags = "vip课类别")
@RequestMapping("vipGroupCategory")
@RestController
public class VipGroupCategoryController extends BaseController {

	@Autowired
	private VipGroupCategoryService vipGroupCategoryService;

	@Autowired
	private SysUserFeignService sysUserFeignService;
	
	@Autowired
	private TeacherDao teacherDao;

	@ApiOperation("单查询")
	@GetMapping(value = "/query")
	public Object query(int id) {
		return succeed(vipGroupCategoryService.get(id));
	}

	@ApiOperation("全查询")
	@GetMapping(value = "/queryAll")
	public Object queryAll(Integer organId) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(user)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		if(Objects.nonNull(organId)){
			return succeed(vipGroupCategoryService.findAllByOrgan(organId.toString()));
		}else{
			Teacher teacher = teacherDao.get(user.getId());
			if(Objects.isNull(teacher)){
				return failed("教师信息不存在");
			}
			return succeed(vipGroupCategoryService.findAllByOrgan(teacher.getTeacherOrganId().toString()));
		}
	}

	@ApiOperation("新增")
	@PostMapping(value = "/add")
	public Object add(VipGroupCategory vipGroupCategory) {
		Date date = new Date();
		vipGroupCategory.setCreateTime(date);
		vipGroupCategory.setUpdateTime(date);
		vipGroupCategory.setDelFlag("0");
		vipGroupCategoryService.insert(vipGroupCategory);
		return succeed();
	}

	@ApiOperation("修改")
	@PostMapping(value = "/update")
	public Object update(VipGroupCategory vipGroupCategory) {
		Date date = new Date();
		vipGroupCategory.setUpdateTime(date);
		vipGroupCategoryService.update(vipGroupCategory);
		return succeed();
	}

	@ApiOperation("删除")
	@PostMapping(value = "/delete")
	public Object delete(int id) {
		VipGroupCategory vipGroupCategory = vipGroupCategoryService.get(id);
		Date date = new Date();
		vipGroupCategory.setUpdateTime(date);
		vipGroupCategory.setDelFlag("1");
		vipGroupCategoryService.update(vipGroupCategory);
		return succeed();
	}

}
