package com.ym.mec.student.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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 com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.LuckDrawLog;
import com.ym.mec.biz.dal.page.LuckDrawQueryInfo;
import com.ym.mec.biz.service.LuckDrawCountService;
import com.ym.mec.biz.service.LuckDrawLogService;
import com.ym.mec.biz.service.LuckDrawPrizeService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.string.ValueUtil;

@Api(tags = "抽奖")
@RestController
@RequestMapping(value = "luckDraw")
public class LuckDrawController extends BaseController {

	@Autowired
	private LuckDrawPrizeService luckDrawPrizeService;

	@Autowired
	private LuckDrawLogService luckDrawLogService;

	@Autowired
	private LuckDrawCountService luckDrawCountService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "查询抽奖记录")
	@GetMapping(value = "/list", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object list(LuckDrawQueryInfo queryInfo) {
		PageInfo<LuckDrawLog> pageInfo = luckDrawLogService.queryPage(queryInfo);
		for (LuckDrawLog log : pageInfo.getRows()) {
			log.getUser().setPhone(ValueUtil.fuzzyMobile(log.getUser().getPhone()));
		}
		return succeed(pageInfo);
	}

	@ApiOperation(value = "查询我的抽奖记录")
	@GetMapping(value = "/myList", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object myList(LuckDrawQueryInfo queryInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		queryInfo.setUserId(sysUser.getId());
		return succeed(luckDrawLogService.queryPage(queryInfo));
	}

	@ApiOperation(value = "抽奖")
	@PostMapping(value = "/draw", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object draw(int group) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		return succeed(luckDrawPrizeService.draw((long) sysUser.getId(), group));
	}

	@ApiOperation(value = "查询个人抽奖机会")
	@GetMapping(value = "/queryCount", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object queryCount() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		return succeed(luckDrawCountService.get((long) sysUser.getId()));
	}

}
