package com.bizzan.bitrade.job;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bizzan.bitrade.constant.BooleanEnum;
import com.bizzan.bitrade.entity.RiskControl;
import com.bizzan.bitrade.service.ContractOrderEntrustService;
import com.bizzan.bitrade.service.MemberContractPositionService;
import com.bizzan.bitrade.service.MemberDepositService;
import com.bizzan.bitrade.service.MemberService;
import com.bizzan.bitrade.service.MemberTransactionService;
import com.bizzan.bitrade.service.RiskControlService;
import com.bizzan.bitrade.service.WithdrawRecordService;
import com.bizzan.bitrade.util.DateUtil;
import com.bizzan.bitrade.vo.DepositControlVo;
import com.bizzan.bitrade.vo.DepositTransferControlVo;
import com.bizzan.bitrade.vo.MemberTransactionControlVo;
import com.bizzan.bitrade.vo.OrderFarmingVo;
import com.bizzan.bitrade.vo.PositionFarmingVo;
import com.bizzan.bitrade.vo.WithdrawAddressControlVo;
import com.bizzan.bitrade.vo.WithdrawControlVo;
import com.bizzan.bitrade.vo.WithdrawTransferControlVo;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class RiskControlJob {

	@Autowired
	private RiskControlService riskControlService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberContractPositionService memberContractPositionService;
	@Autowired
	private ContractOrderEntrustService contractOrderEntrustService;
	@Autowired
	private MemberDepositService memberDepositService;
	@Autowired
	private WithdrawRecordService withdrawRecordService;
	@Autowired
	private MemberTransactionService memberTransactionService;
	
	@Scheduled(cron = "0 5 0,8,16 * * ?")
	public void farmingControl() {
		long endTime = DateUtil.getTimeMillis() / 1000;
		long startTime = endTime - 8 * 60 * 60;
		List<PositionFarmingVo> positionFarmingList = memberContractPositionService.findFarmingList(startTime, endTime);
		for(PositionFarmingVo positionFarmingVo : positionFarmingList) {
			memberService.updateTransactionStatus(positionFarmingVo.getMemberId(), BooleanEnum.IS_FALSE);
			
			this.saveRiskControl(positionFarmingVo.getMemberId(), 1, String.format("总开仓次数：%d，持仓少于3分钟次数：%d, 比例：%.2f", 
					positionFarmingVo.getTotalCount(), positionFarmingVo.getShortDurationCount(), positionFarmingVo.getRatio()));
			
		}
	}
	
	@Scheduled(cron = "0 5 0 * * ?")
	public void oneDayControl() {
		String[] dates = DateUtil.getTodayRealSartAndEndDateStr();
		List<OrderFarmingVo> orderFarmingList = contractOrderEntrustService.findFarmingList(dates[0], dates[1]);
		for(OrderFarmingVo orderFarmingVo : orderFarmingList) {
			memberService.updateTransactionStatus(orderFarmingVo.getMemberId(), BooleanEnum.IS_FALSE);
			
			this.saveRiskControl(orderFarmingVo.getMemberId(), 2, String.format("总开单次数：%d", 
					orderFarmingVo.getTotalCount()));
		}
		
		List<MemberTransactionControlVo> memberTransactionControlVolist = memberTransactionService.statFarmingControl(dates[0], dates[1]);
		for (MemberTransactionControlVo memberTransactionControlVo : memberTransactionControlVolist) {
			//memberService.updateDisableReward(memberTransactionControlVo.getMemberId(), 1);
			
			this.saveRiskControl(memberTransactionControlVo.getMemberId(), 4, String.format("总收益：%.2f，合约返佣：%.2f", 
					memberTransactionControlVo.getTotalAmount(), memberTransactionControlVo.getAwardAmount()));
		}
		
		List<DepositControlVo> depositControllist = memberDepositService.statControl(dates[0], dates[1]);
		for (DepositControlVo depositControlVo : depositControllist) {
			memberService.updateIsMoneyLock(depositControlVo.getMemberId(), 1);
			
			this.saveRiskControl(depositControlVo.getMemberId(), 4, String.format("小额充值次数：%d，金额：%.2f", 
					depositControlVo.getSmallCount(), depositControlVo.getSmallAmount()));
		}
		
		List<DepositTransferControlVo> depositTransferControllist = memberDepositService.statTransferControl(dates[0], dates[1]);
		for (DepositTransferControlVo depositTransferControlVo : depositTransferControllist) {
			memberService.updateIsMoneyLock(depositTransferControlVo.getMemberId(), 1);
			memberService.updateIsMoneyLock(depositTransferControlVo.getTargetMemberId(), 1);
			
			this.saveRiskControl(depositTransferControlVo.getMemberId(), 5, String.format("充值后%d分钟划转金额%.2f到账号%s", 
					depositTransferControlVo.getInterval(), depositTransferControlVo.getAmount(), depositTransferControlVo.getTargetMemberId()));
		}
		
		List<WithdrawControlVo> withdrawControllist = withdrawRecordService.statControl(dates[0], dates[1]);
		for (WithdrawControlVo withdrawControlVo : withdrawControllist) {
			memberService.updateIsMoneyLock(withdrawControlVo.getMemberId(), 1);
			
			this.saveRiskControl(withdrawControlVo.getMemberId(), 6, String.format("小额提现次数：%d，金额：%.2f", 
					withdrawControlVo.getSmallCount(), withdrawControlVo.getSmallAmount()));
		}
		
		List<WithdrawAddressControlVo> withdrawAddressControllist = withdrawRecordService.statAddressControl(dates[0], dates[1]);
		for (WithdrawAddressControlVo withdrawAddressControlVo : withdrawAddressControllist) {
			memberService.updateIsMoneyLock(withdrawAddressControlVo.getMemberId(), 1);
			
			this.saveRiskControl(withdrawAddressControlVo.getMemberId(), 7, String.format("转出不同地址次数：%d", 
					withdrawAddressControlVo.getAddressCount()));
		}
		
		List<WithdrawTransferControlVo> withdrawTransferControllist = withdrawRecordService.statTransferControl(dates[0], dates[1]);
		for (WithdrawTransferControlVo withdrawTransferControlVo : withdrawTransferControllist) {
			memberService.updateIsMoneyLock(withdrawTransferControlVo.getMemberId(), 1);
			
			this.saveRiskControl(withdrawTransferControlVo.getMemberId(), 7, String.format("平台内转账次数：%d", 
					withdrawTransferControlVo.getAddressCount()));
		}
	}
	
	private void saveRiskControl(Long memberId, int type, String content) {
		RiskControl riskControl = new RiskControl();
		riskControl.setMemberId(memberId);
		riskControl.setType(type);
		riskControl.setContent(content);
		riskControl.setCreateTime(new Date());
		riskControlService.save(riskControl);
	}
}
