package com.jsowell.pile.service.impl;

import com.google.common.collect.Lists;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.ClearingWithdrawInfo;
import com.jsowell.pile.mapper.ClearingWithdrawInfoMapper;
import com.jsowell.pile.service.ClearingWithdrawInfoService;
import com.jsowell.pile.service.IPileMerchantInfoService;
import com.jsowell.pile.vo.web.ClearingBillVO;
import com.jsowell.pile.vo.web.WithdrawInfoVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ClearingWithdrawInfoServiceImpl implements ClearingWithdrawInfoService{

    @Autowired
    private AdapayService adapayService;

    @Autowired
    private IPileMerchantInfoService pileMerchantInfoService;

    @Resource
    private ClearingWithdrawInfoMapper clearingWithdrawInfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return clearingWithdrawInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ClearingWithdrawInfo record) {
        return clearingWithdrawInfoMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(ClearingWithdrawInfo record) {
        return clearingWithdrawInfoMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(ClearingWithdrawInfo record) {
        return clearingWithdrawInfoMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(ClearingWithdrawInfo record) {
        return clearingWithdrawInfoMapper.insertSelective(record);
    }

    @Override
    public ClearingWithdrawInfo selectByPrimaryKey(Integer id) {
        return clearingWithdrawInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ClearingWithdrawInfo record) {
        return clearingWithdrawInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ClearingWithdrawInfo record) {
        return clearingWithdrawInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<ClearingWithdrawInfo> list) {
        return clearingWithdrawInfoMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<ClearingWithdrawInfo> list) {
        return clearingWithdrawInfoMapper.batchInsert(list);
    }

    @Override
    public ClearingWithdrawInfo selectByWithdrawCode(String withdrawCode) {
        return clearingWithdrawInfoMapper.selectByWithdrawCode(withdrawCode);
    }

    /**
     * 根据订单号查询
     */
    @Override
    public ClearingBillVO selectWithdrawInfoByOrderCode(String orderCode) {
        return clearingWithdrawInfoMapper.selectWithdrawInfoByOrderCode(orderCode);
    }

    @Override
    public List<ClearingBillVO> selectWithdrawInfoByOrderCodeList(List<String> orderCodeList) {
        return clearingWithdrawInfoMapper.selectWithdrawInfoByOrderCodeList(orderCodeList);
    }

    /**
     * 查询运营商提现记录
     */
    @Override
    public List<WithdrawInfoVO> selectByMerchantId(String merchantId) throws BaseAdaPayException {
        List<ClearingWithdrawInfo> infoList = clearingWithdrawInfoMapper.selectByMerchantId(merchantId);
        List<WithdrawInfoVO> resultList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(infoList)) {
            return resultList;
        }
        String wechatAppId = pileMerchantInfoService.queryAppIdByMerchantId(merchantId);
        if (StringUtils.isBlank(wechatAppId)) {
            return resultList;
        }
        for (ClearingWithdrawInfo clearingWithdrawInfo : infoList) {
            String withdrawCode = clearingWithdrawInfo.getWithdrawCode();
            WithdrawInfoVO vo = new WithdrawInfoVO();
            vo.setMerchantId(merchantId);
            vo.setWithdrawCode(withdrawCode);
            vo.setApplicationTime(DateUtils.formatDateTime(clearingWithdrawInfo.getApplicationTime()));
            vo.setStatusDesc(clearingWithdrawInfo.getWithdrawStatus());
            vo.setCashAmt(clearingWithdrawInfo.getCreditedAmt());
            resultList.add(vo);
        }
        return resultList;
    }

}
