package com.sv.service.oms;
import com.enums.MoneyLogEnum;
import com.enums.PayTypeEnum;
import com.github.pagehelper.PageHelper;
import com.sv.mapper.MemberWithdrawApplyMapper;
import com.sv.service.common.MemberMoney;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.entity.enums.StatusEnum;
import com.ydd.framework.core.service.impl.BaseServiceImpl;

import com.sv.entity.MemberWithdrawApply;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigDecimal;

/**
 * Service - 用户提现申请
 *
 * @author yechanglin
 * @since 2018-08-03
 */
@Service("MemberWithdrawApplyServiceOms")
@Transactional(readOnly = true)
public class MemberWithdrawApplyService extends MemberMoney {

	private final Logger logger = LoggerFactory.getLogger(MemberWithdrawApplyService.class);

	@Resource
	private MemberWithdrawApplyMapper memberWithdrawApplyMapper;

	/**
	 * 创建用户提现申请
	 *
	 * @param memberWithdrawApply 用户提现申请
	 */

	@Transactional
    public void save(MemberWithdrawApply memberWithdrawApply) {
		if (memberWithdrawApply.getId() != null && memberWithdrawApply.getId() > 0) {
			// 更新
			memberWithdrawApplyMapper.update(memberWithdrawApply);
		} else {
			// 新建
			memberWithdrawApplyMapper.insert(memberWithdrawApply);
		}
	}

	/**
	 * 更新用户提现申请
 	 *
	 * @param memberWithdrawApply 用户提现申请
	 */

	@Transactional
    public void update(MemberWithdrawApply memberWithdrawApply) {
		memberWithdrawApplyMapper.update(memberWithdrawApply);
	}

	/**
	 * 删除用户提现申请
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */

	@Transactional
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return memberWithdrawApplyMapper.delete(id);
	}

	/**
	 * 删除用户提现申请
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */

	@Transactional
    public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return memberWithdrawApplyMapper.deleteByIds(ids);
	}

	/**
	 * 查询用户提现申请
	 *
	 * @param id 编号
	 * @return 用户提现申请
	 */
	public MemberWithdrawApply findById(Integer id) {
		return memberWithdrawApplyMapper.findById(id);
	}

	/**
	 * 查询用户提现申请
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */

	public Pagination findPage(Pagination pagination) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		PageHelper.orderBy("id desc");
		pagination.setQueryResult(memberWithdrawApplyMapper.findAll());
		return pagination;
	}

	/**
	 * 更新状态
	 *
	 * @param ids 编号
	 * @param status 状态
	 * @return 更新数量
	 */

	@Transactional
	public void updateStatus(Integer[] ids, Byte status, String dataTime, Integer memberId) {
		if (ids == null) {
			return ;
		}
		if (status == null) {
			status = StatusEnum.DISABLE.value;
		}

		 memberWithdrawApplyMapper.updateStatus(ids, status, dataTime,memberId);
			if (status == -1){
				Integer id = ids[0];
				MemberWithdrawApply memberWithdrawApply = memberWithdrawApplyMapper.findById(id);
				BigDecimal price = memberWithdrawApply.getMoney();
				create(memberId, 1, MoneyLogEnum.APPLY_MONEY_FAIL.value, price, PayTypeEnum.BALANCE.value, null, null, null, null);
			}else {
				create(memberId, 1, MoneyLogEnum.APPLY_MONEY_SUCCESS.value, null, PayTypeEnum.BALANCE.value, null, null, null, null);
			}
	}

}

