package com.bizzan.bitrade.controller.game;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestAttribute;

import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.constant.SysConstant;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.Admin;
import com.bizzan.bitrade.entity.GameAdmit;
import com.bizzan.bitrade.entity.GameRecord;
import com.bizzan.bitrade.entity.QGameAdmit;
import com.bizzan.bitrade.model.screen.GameAdmitScreen;
import com.bizzan.bitrade.service.GameAdmitService;
import com.bizzan.bitrade.service.GameWalletService;
import com.bizzan.bitrade.service.LocaleMessageSourceService;
import com.bizzan.bitrade.util.MessageResult;
import com.bizzan.bitrade.util.PredicateUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(description="世界杯接口")
@RestController
@RequestMapping("/game/wc")
public class WorldCupController extends BaseAdminController {

	@Autowired
	private GameAdmitService gameAdmitService;
	@Autowired
    private LocaleMessageSourceService messageSource;
	
	@ApiOperation(value="获取下单记录", notes="获取下单记录")
	@RequiresPermissions("swap:position:page-query")
    @PostMapping("page-query")
	public MessageResult<Page<GameAdmit>> list(PageModel pageModel,
            GameAdmitScreen screen) {
		if (pageModel.getDirection() == null && pageModel.getProperty() == null) {
            ArrayList<Sort.Direction> directions = new ArrayList<>();
            directions.add(Sort.Direction.DESC);
            pageModel.setDirection(directions);
            List<String> property = new ArrayList<>();
            property.add("createTime"); // 默认金额排序
            pageModel.setProperty(property);
        }
        
        Predicate predicate = getPredicate(screen);
        
        Page<GameAdmit> positions = gameAdmitService.findAll(predicate, pageModel.getPageable());
		
		MessageResult<Page<GameAdmit>> result = MessageResult.success("success");
        result.setData(positions);
        return result;
	}
	
	private Predicate getPredicate(GameAdmitScreen screen) {
		ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
		QGameAdmit qGameAdmit = QGameAdmit.gameAdmit;
		
		if (StringUtils.isNotEmpty(screen.getBatchName())) {
            booleanExpressions.add(qGameAdmit.order.batchName.contains(screen.getBatchName()));
        }
		if (screen.getMemberId()!=null) {
            booleanExpressions.add(qGameAdmit.order.memberId.eq(screen.getMemberId()));
        }
        if(screen.getStartTime() != null) {
            booleanExpressions.add(qGameAdmit.createTime.gt(screen.getStartTime()));
        }
        if(screen.getEndTime() != null) {
            booleanExpressions.add(qGameAdmit.createTime.lt(screen.getEndTime()));
        }
        if (screen.getAdmitStatus()!=null) {
            booleanExpressions.add(qGameAdmit.admitStatus.eq(screen.getAdmitStatus()));
        }
        return PredicateUtils.getPredicate(booleanExpressions);
	}
	
	@ApiOperation(value="审核", notes="审核")
	@ApiImplicitParams(value = {
    		@ApiImplicitParam(name = "ids",value = "ID数组",required = true,paramType = "form",dataType = "array")
    })
	@RequiresPermissions("swap:position:page-query")
    @PostMapping("admit")
	public synchronized MessageResult admit(@RequestAttribute(SysConstant.SESSION_ADMIN) Admin admin,
			@RequestParam("ids") Long[] ids) {
		if (null == admin) {
			return error(messageSource.getMessage("DATA_EXPIRED_LOGIN_AGAIN"));
		}
		for(Long id: ids) {
			gameAdmitService.clear(id);
		}
		return success(messageSource.getMessage("PASS_THE_AUDIT"));
	}
}
