package com.bizzan.bitrade.controller.finance;

import com.alibaba.fastjson.JSONObject;
import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.constant.TransactionType;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.Member;
import com.bizzan.bitrade.entity.MemberTransaction;
import com.bizzan.bitrade.entity.QMember;
import com.bizzan.bitrade.entity.QMemberTransaction;
import com.bizzan.bitrade.entity.QWalletTransRecord;
import com.bizzan.bitrade.entity.WalletTransRecord;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.bizzan.bitrade.es.ESUtils;
import com.bizzan.bitrade.model.ContractFeeStatVo;
import com.bizzan.bitrade.model.MemberTransactionStatVo;
import com.bizzan.bitrade.model.RechargeAndWithdrawStatVo;
import com.bizzan.bitrade.model.screen.MemberTransactionScreen;
import com.bizzan.bitrade.model.screen.WalletTransRecordScreen;
import com.bizzan.bitrade.model.vo.MemberTransaction2ESVO;
import com.bizzan.bitrade.service.LocaleMessageSourceService;
import com.bizzan.bitrade.service.MemberContractWalletService;
import com.bizzan.bitrade.service.MemberTransactionService;
import com.bizzan.bitrade.service.MemberWalletService;
import com.bizzan.bitrade.service.UserPurseService;
import com.bizzan.bitrade.service.WalletTransRecordService;
import com.bizzan.bitrade.util.DateUtil;
import com.bizzan.bitrade.util.FileUtil;
import com.bizzan.bitrade.util.MessageResult;
import com.bizzan.bitrade.util.PredicateUtils;
import com.bizzan.bitrade.vo.MemberTransactionVO;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.bizzan.bitrade.constant.SysConstant.SESSION_MEMBER;
import static org.springframework.util.Assert.notNull;

/**
 * @author
 * @description 划转记录
 * @date 2019/1/17 17:07
 */
@RestController
@Slf4j
@RequestMapping("/finance/wallet-trans")
public class WalletTransRecordController extends BaseAdminController {
    @Autowired
    private WalletTransRecordService walletTransRecordService;

    @RequiresPermissions(value = {"finance:member-transaction:page-query", "finance:member-transaction:page-query:recharge",
            "finance:member-transaction:page-query:check", "finance:member-transaction:page-query:fee"}, logical = Logical.OR)
    @PostMapping("page-query")
    @AccessLog(module = AdminModule.FINANCE, operation = "分页查找交易记录WalletTransRecord")
    public MessageResult pageQuery(
            PageModel pageModel,
            WalletTransRecordScreen screen) {
        ArrayList<BooleanExpression> predicates = new ArrayList<>();

        if(screen.getMemberId()!=null) {
            predicates.add((QMember.member.id.eq(screen.getMemberId())));
        }
        if (!StringUtils.isEmpty(screen.getAccount())) {
            predicates.add(QMember.member.username.like("%"+screen.getAccount()+"%")
                        .or(QMember.member.realName.like("%"+screen.getAccount()+"%")));
        }
        if (screen.getStartTime() != null) {
            predicates.add(QWalletTransRecord.walletTransRecord.createTime.goe(screen.getStartTime()));
        }
        if (screen.getEndTime() != null){
            predicates.add(QWalletTransRecord.walletTransRecord.createTime.lt(DateUtil.dateAddDay(screen.getEndTime(),1)));
        }
        if (screen.getSource() != null) {
            predicates.add(QWalletTransRecord.walletTransRecord.source.eq(screen.getSource()));
        }
        if (screen.getTarget() != null) {
            predicates.add(QWalletTransRecord.walletTransRecord.target.eq(screen.getTarget()));
        }

        if(screen.getMinMoney()!=null) {
            predicates.add(QWalletTransRecord.walletTransRecord.amount.goe(screen.getMinMoney()));
        }

        if(screen.getMaxMoney()!=null) {
            predicates.add(QWalletTransRecord.walletTransRecord.amount.loe(screen.getMaxMoney()));
        }

        Page<WalletTransRecord> results = walletTransRecordService.findAll(PredicateUtils.getPredicate(predicates), pageModel);

        return success(results);
    }
}
