package com.ym.mec.web.controller;

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

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.entity.LuckDrawGroup;
import com.ym.mec.biz.dal.entity.LuckDrawPrize;
import com.ym.mec.biz.dal.page.LuckDrawQueryInfo;
import com.ym.mec.biz.service.LuckDrawGroupService;
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.exception.BizException;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.util.collection.MapUtil;

@Api(tags = "抽奖")
@RestController
public class LuckDrawController extends BaseController {
	
	@Autowired
	private LuckDrawGroupService luckDrawGroupService;

	@Autowired
	private LuckDrawPrizeService luckDrawPrizeService;

	@Autowired
	private LuckDrawLogService luckDrawLogService;
	
	@ApiOperation(value = "查询奖品组列表")
	@GetMapping(value = "luckDrawGroup/findAll", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawGroup/list')")
	public Object luckDrawGroupFindAll(QueryInfo queryInfo) {
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		
		return succeed(luckDrawGroupService.findAll(params));
	}
	
	@ApiOperation(value = "查询所有奖品组")
	@GetMapping(value = "luckDrawGroup/list", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawGroup/list')")
	public Object luckDrawGroupList(QueryInfo queryInfo) {
		return succeed(luckDrawGroupService.queryPage(queryInfo));
	}

	@ApiOperation(value = "新增抽奖组")
	@PostMapping(value = "luckDrawGroup/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawGroup/add')")
	public Object luckDrawGroupAdd(@RequestBody LuckDrawGroup luckDrawGroup) {
		Date date = new Date();
		luckDrawGroup.setCreateOn(date);
		return luckDrawGroupService.insert(luckDrawGroup) > 0 ? succeed() : failed();
	}

	@ApiOperation(value = "编辑抽奖组")
	@PostMapping(value = "luckDrawGroup/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawGroup/update')")
	public Object luckDrawGroupUpdate(@RequestBody LuckDrawGroup luckDrawGroup) {
		LuckDrawGroup oriGroup = luckDrawGroupService.get(luckDrawGroup.getId());
		oriGroup.setName(luckDrawGroup.getName());
		oriGroup.setConsumeType(luckDrawGroup.getConsumeType());
		oriGroup.setConsumeValue(luckDrawGroup.getConsumeValue());
		oriGroup.setEndTime(luckDrawGroup.getEndTime());
		oriGroup.setStartTime(luckDrawGroup.getStartTime());
		return luckDrawGroupService.update(oriGroup) > 0 ? succeed() : failed();
	}

	@ApiOperation(value = "查询奖品列表")
	@GetMapping(value = "luckDrawPrize/list", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawPrize/list')")
	public Object list(LuckDrawQueryInfo queryInfo) {
		return succeed(luckDrawPrizeService.queryPage(queryInfo));
	}

	@ApiOperation(value = "批量新增抽奖奖品")
	@PostMapping(value = "luckDrawPrize/batchAdd", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawPrize/batchAdd')")
	public Object batchAdd(@RequestBody List<LuckDrawPrize> luckDrawPrizeList) {
		return luckDrawPrizeService.batchInsert(luckDrawPrizeList) ? succeed() : failed();
	}

	@ApiOperation(value = "新增抽奖奖品")
	@PostMapping(value = "luckDrawPrize/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawPrize/add')")
	public Object add(@RequestBody LuckDrawPrize luckDrawPrize) {
		Date date = new Date();
		luckDrawPrize.setCreateOn(date);
		luckDrawPrize.setModifyOn(date);
		return luckDrawPrizeService.insert(luckDrawPrize) > 0 ? succeed() : failed();
	}

	@ApiOperation(value = "编辑抽奖奖品")
	@PostMapping(value = "luckDrawPrize/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawPrize/update')")
	public Object update(@RequestBody LuckDrawPrize luckDrawPrize) {
		LuckDrawPrize oriGoods = luckDrawPrizeService.get(luckDrawPrize.getId());
		oriGoods.setChances(luckDrawPrize.getChances());
		oriGoods.setEnabled(luckDrawPrize.getEnabled());
		oriGoods.setName(luckDrawPrize.getName());
		oriGoods.setStock(luckDrawPrize.getStock());
		oriGoods.setRewardType(luckDrawPrize.getRewardType());
		oriGoods.setGroupId(luckDrawPrize.getGroupId());
		oriGoods.setIsDefault(luckDrawPrize.getIsDefault());
		oriGoods.setMemo(luckDrawPrize.getMemo());
		Date date = new Date();
		oriGoods.setModifyOn(date);
		return luckDrawPrizeService.update(oriGoods) > 0 ? succeed() : failed();
	}

	@ApiOperation(value = "查询抽奖列表")
	@GetMapping(value = "luckDrawLog/rewardlist", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('luckDrawLog/rewardlist')")
	public Object rewardlist(LuckDrawQueryInfo queryInfo) {
		return succeed(luckDrawLogService.queryPage(queryInfo));
	}

}
