package com.bizzan.bitrade.controller;

import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.constant.RewardRecordType;
import com.bizzan.bitrade.controller.screen.ContractRewardRecordScreen;
import com.bizzan.bitrade.entity.*;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.bizzan.bitrade.service.ContractRewardRecordService;
import com.bizzan.bitrade.service.MemberService;
import com.bizzan.bitrade.service.RewardRecordService;
import com.bizzan.bitrade.util.DateUtil;
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.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.ArrayList;
import java.util.List;

import static com.bizzan.bitrade.constant.SysConstant.SESSION_MEMBER;

/**
 * @author sulinxin
 */
@Api(value = "MemberTransactionRebateController" , tags = "返佣记录接口")
@RestController
@RequestMapping("transactionRebates")
@Slf4j
public class MemberTransactionRebateController extends BaseController{

    @Autowired
    private MemberService memberService;
    @Autowired
    private ContractRewardRecordService contractRewardRecordService;
    @Autowired
    private RewardRecordService rewardRecordService;

    @ApiOperation(value="查询返佣记录", notes="查询返佣记录")
    @PostMapping(value = "/page-query")
    @Transactional(rollbackFor = Exception.class)
    public MessageResult pageQuery(
            PageModel pageModel,
            ContractRewardRecordScreen screen,
            @SessionAttribute(SESSION_MEMBER) AuthMember user) {

        Member checkMember = memberService.findOne(user.getId());
        if(!checkMember.getSuperPartner().equals("1")) {
            return error("您不是代理商！");
        }

        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,checkMember.getId());

        Page<ContractRewardRecord> results = contractRewardRecordService.findAll(predicate, pageModel.getPageable());

        return success(results);
    }

    private Predicate getPredicate(ContractRewardRecordScreen screen,Long id) {
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        QContractRewardRecord qContractRewardRecord = QContractRewardRecord.contractRewardRecord;
        booleanExpressions.add((qContractRewardRecord.member.id.eq(id)));
        if(screen.getMemberId()!=null) {
            booleanExpressions.add((qContractRewardRecord.fromMember.id.eq(screen.getMemberId())));
        }
        if (screen.getStartTime() != null) {
            booleanExpressions.add(qContractRewardRecord.createTime.goe(screen.getStartTime()));
        }
        if (screen.getEndTime() != null){
            booleanExpressions.add(qContractRewardRecord.createTime.lt(DateUtil.dateAddDay(screen.getEndTime(),1)));
        }
        if (screen.getType() != null) {
            booleanExpressions.add(qContractRewardRecord.type.eq(screen.getType()));
        }
        if (screen.getFromUsername() != null && !screen.getFromUsername().equals("")) {
        	Member member = memberService.findByUsername(screen.getFromUsername());
        	if(member != null)
        		booleanExpressions.add(qContractRewardRecord.fromMember.id.eq(member.getId()));
        	else
        		booleanExpressions.add(qContractRewardRecord.fromMember.id.eq(0L));
        }
        return PredicateUtils.getPredicate(booleanExpressions);
    }
    
    @PostMapping(value = "/wc-page-query")
    @Transactional(rollbackFor = Exception.class)
    public MessageResult wcPageQuery(
            PageModel pageModel,
            ContractRewardRecordScreen screen,
            @SessionAttribute(SESSION_MEMBER) AuthMember user) {

        Member checkMember = memberService.findOne(user.getId());
        if(!checkMember.getSuperPartner().equals("1")) {
            return error("您不是代理商！");
        }

        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 = getWcPredicate(screen,checkMember.getId());
        log.info("pageModel={}", pageModel);
        log.info("predicate={}", predicate);

        Page<RewardRecord> results = rewardRecordService.findByPage(predicate, pageModel.getPageable());

        return success(results);
    }

    private Predicate getWcPredicate(ContractRewardRecordScreen screen,Long id) {
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        QRewardRecord qRewardRecord = QRewardRecord.rewardRecord;
        booleanExpressions.add((qRewardRecord.member.id.eq(id)));
        if(screen.getMemberId()!=null) {
            booleanExpressions.add((qRewardRecord.fromMember.id.eq(screen.getMemberId())));
        }
        if (screen.getStartTime() != null) {
            booleanExpressions.add(qRewardRecord.createTime.goe(screen.getStartTime()));
        }
        if (screen.getEndTime() != null){
            booleanExpressions.add(qRewardRecord.createTime.lt(DateUtil.dateAddDay(screen.getEndTime(),1)));
        }
        booleanExpressions.add(qRewardRecord.type.eq(RewardRecordType.GAMEAWARD));
        
        if (screen.getFromUsername() != null && !screen.getFromUsername().equals("")) {
        	Member member = memberService.findByUsername(screen.getFromUsername());
        	if(member != null)
        		booleanExpressions.add(qRewardRecord.fromMember.id.eq(member.getId()));
        	else
        		booleanExpressions.add(qRewardRecord.fromMember.id.eq(0L));
        }
        return PredicateUtils.getPredicate(booleanExpressions);
    }
}
