package com.bizzan.bitrade.controller.finance;

import static com.bizzan.bitrade.constant.WithdrawStatus.FAIL;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PatchMapping;
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 com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.constant.WithdrawStatus;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.Member;
import com.bizzan.bitrade.entity.MoneyOperationRecord;
import com.bizzan.bitrade.entity.QMoneyOperationRecord;
import com.bizzan.bitrade.model.screen.TransferRecordScreen;
import com.bizzan.bitrade.service.MemberService;
import com.bizzan.bitrade.service.MoneyOperationRecordService;
import com.bizzan.bitrade.service.RedisLock;
import com.bizzan.bitrade.util.MessageResult;
import com.bizzan.bitrade.util.PredicateUtils;
import com.querydsl.core.types.dsl.BooleanExpression;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/finance/operation")
public class MoneyOperationRecordController extends BaseAdminController {

	@Autowired
	private MoneyOperationRecordService service;
	@Autowired
	private MemberService memberService;
	@Autowired
	private RedisLock redisLock;
	
	@PostMapping("page-query")
    @AccessLog(module = AdminModule.FINANCE, operation = "分页查找交易记录TransferRecord")
    public MessageResult pageQuery(
            PageModel pageModel, TransferRecordScreen screen) {
		ArrayList<BooleanExpression> predicates = new ArrayList<>();
		if(screen.getAccount()!=null) {
			Member member = memberService.findByUsername(screen.getAccount());
	        if(member == null) {
	        	try {
	        		member = memberService.findOne(Long.parseLong(screen.getAccount()));
	        	} catch (Exception e) {
	        		
	        	}
	        }
	        if(member == null) {
	        	return error("没有找到对象");
	        }
	        predicates.add(QMoneyOperationRecord.moneyOperationRecord.memberId.eq(member.getId()));
		}
		if(screen.getMemberId()!=null) {
            predicates.add((QMoneyOperationRecord.moneyOperationRecord.memberId.eq(screen.getMemberId())));
        }
		Page<MoneyOperationRecord> results = service.findAll(PredicateUtils.getPredicate(predicates), pageModel.getPageable());
		return success(results);
	}
	
	@RequiresPermissions("finance:withdraw-record:audit-pass")
    @PatchMapping("/audit-pass")
    @AccessLog(module = AdminModule.FINANCE, operation = "提现记录TransferRecord一键审核通过")
    public MessageResult auditPass(@RequestParam("ids") Long[] ids) {
		if(!redisLock.lockBySeconds("ADMIN_MONEY_ADMIT", 10))
			return this.error("操作太频繁");
		service.audit(ids, WithdrawStatus.SUCCESS);
		return MessageResult.success();
    }

    //一键审核不通过
    @RequiresPermissions("finance:withdraw-record:audit-no-pass")
    @PatchMapping("/audit-no-pass")
    @AccessLog(module = AdminModule.FINANCE, operation = "提现记录TransferRecord一键审核不通过")
    public MessageResult auditNoPass(@RequestParam("ids") Long[] ids) {
		if(!redisLock.lockBySeconds("ADMIN_MONEY_ADMIT", 10))
			return this.error("操作太频繁");
    	service.audit(ids, FAIL);
        return success();
    }
}
