package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.gateway.service.CsvService;
import com.sptg.pay.api.dto.PayCertificateDto;
import com.sptg.pay.api.entity.PayCertificate;
import com.sptg.pay.api.feigns.PayCertificateFeign;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xuanyan
 * @date 2020/7/10 10:56
 */
@Service
@RequiredArgsConstructor
public class PayCertificateCsvServiceImpl implements CsvService {
    @Override
    public String getFlag() {
        return "csv_pay_certificate";
    }

    @Override
    public String getTemplateName() {
        return "支付凭证导入模板";
    }

    private final PayCertificateFeign payCertificateFeign;

    @Override
    public ResultBean<?> save(Object data) {
        PayCertificate payCertificate = payCertificateFeign.addPayCertificateByImport((PayCertificateDto) data);
        if (null != payCertificate){
            return ResultBean.ok();
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    public Object arrToObj(String[] array) throws ParseException {
        PayCertificateDto payCertificateDto = new PayCertificateDto();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //原始数据id(必填)
        String oldId = array[0];
        payCertificateDto.setOldId(Long.valueOf(oldId));
        //原始数据订单id(必填)
        String oldOrderId = array[1];
        payCertificateDto.setOldOrderId(oldOrderId);
        //支付渠道(必填)
        String channelCode = array[2];
        payCertificateDto.setChannelCode(channelCode);
        //支付凭证(选填)
        String certificate = array[3];
        if (StringUtils.isNotEmpty(certificate)){
            payCertificateDto.setCertificate(certificate);
        }
        //凭证失效时间(选填)
        String expireTime = array[4];
        if (StringUtils.isNotEmpty(expireTime)){
            payCertificateDto.setExpireTime(ft.parse(expireTime));
        }
        //付款状态(必填)
        String payStatus = array[5];
        payCertificateDto.setPayStatus(Integer.valueOf(payStatus));
        //支付时间(选填)
        String payTime = array[6];
        if (StringUtils.isNotEmpty(payTime)){
            payCertificateDto.setPayTime(ft.parse(payTime));
        }
        //支付回调信息(选填)
        String notify = array[7];
        if (StringUtils.isNotEmpty(notify)){
            payCertificateDto.setNotify(notify);
        }
        //是否退款(选填)
        String refund = array[8];
        if (StringUtils.isNotEmpty(refund)){
            payCertificateDto.setRefund(Integer.valueOf(refund));
        }
        //支付金额(必填)
        String payment = array[9];
        payCertificateDto.setPayment(new BigDecimal(payment));
        //原始数据用户id(必填)
        String oldUserId = array[10];
        payCertificateDto.setOldUserId(oldUserId);
        //是否能退款(选填)
        String canRefund = array[11];
        if (StringUtils.isNotEmpty(canRefund)){
            payCertificateDto.setCanRefund(Integer.valueOf(canRefund));
        }
        //是否已发起退款申请(选填)
        String applyRefund = array[12];
        if (StringUtils.isNotEmpty(applyRefund)){
            payCertificateDto.setApplyRefund(Integer.valueOf(applyRefund));
        }
        return payCertificateDto;
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "原始数据id(必填)",
            "原始数据订单id(必填)",
            "支付渠道(必填)",
            "支付凭证(选填)",
            "凭证失效时间(选填)",
            "付款状态(必填)",
            "支付时间(选填)",
            "支付回调信息(选填)",
            "是否退款(选填)",
            "支付金额(必填)",
            "原始数据用户id(必填)",
            "是否能退款(选填)",
            "是否已发起退款申请(选填)"
    ));

    @Override
    public List<String> getHeaderList() {
        return HEADER_LIST;
    }
}
