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.UserOrderDto;
import com.sptg.pay.api.entity.UserOrder;
import com.sptg.pay.api.feigns.UserOrderFeign;
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/9 14:54
 */
@Service
@RequiredArgsConstructor
public class UserOrderCsvServiceImpl implements CsvService {
    @Override
    public String getFlag() {
        return "csv_user_order";
    }

    @Override
    public String getTemplateName() {
        return "订单导入模板";
    }

    private final UserOrderFeign userOrderFeign;

    @Override
    public ResultBean<?> save(Object data) {
        UserOrder userOrder = userOrderFeign.addUserOrderByImport((UserOrderDto) data);
        if (null != userOrder){
            return ResultBean.ok();
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    public Object arrToObj(String[] array) throws ParseException {
        UserOrderDto userOrderDto = new UserOrderDto();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //订单号(必填)
        String orderNo = array[0];
        if (StringUtils.isNotEmpty(orderNo)){
            userOrderDto.setBizOrderNo(orderNo);
        }

        //订单类型(必填)
        String orderType = array[1];
        if (StringUtils.isNotEmpty(orderType)){
            userOrderDto.setOrderType(orderType);
        }

        //应付款(必填)
        String shouldPayAmount = array[3];
        if (StringUtils.isNotEmpty(shouldPayAmount)){
            userOrderDto.setShouldPayAmount(new BigDecimal(shouldPayAmount));
        }

        //实际付款(必填)
        String actualPayAmount = array[4];
        if (StringUtils.isNotEmpty(actualPayAmount)){
            userOrderDto.setActualPayAmount(new BigDecimal(actualPayAmount));
        }

        //支付状态(必填)1 未支付 2 已支付
        String payStatus = array[6];
        if (StringUtils.isNotEmpty(payStatus)){
            userOrderDto.setPayStatus(Integer.valueOf(payStatus));
        }

        //支付方式(必填)
        String payType = array[7];
        if (StringUtils.isNotEmpty(payType)){
            userOrderDto.setPayType(payType);
        }

        //支付时间(必填)
        String payTime = array[8];
        if (StringUtils.isNotEmpty(payTime)){
            userOrderDto.setPayTime(ft.parse(payTime));
        }

        //订单原始数据id(必填)
        String oldId = array[18];
        userOrderDto.setOldId(oldId);

        //用户原始数据id(必填) 通过id找user表得用户name等
        String oldUserId = array[19];
        userOrderDto.setOldUserId(oldUserId);

        //商品原始数据id(必填)
        String oldGoodsId = array[20];
        userOrderDto.setOldGoodsId(oldGoodsId);

        //小区原始数据id(必填) 通过id找name
        String oldResidentialId = array[21];
        if (StringUtils.isNotEmpty(oldResidentialId)){
            userOrderDto.setOldResidentialId(oldResidentialId);
        }

        //单元原始数据id(必填) 通过id找name
        String oldUnitId = array[22];
        if (StringUtils.isNotEmpty(oldUnitId)){
            userOrderDto.setOldUnitId(oldUnitId);
        }

        //房间原始数据id(必填) 通过id找name
        String oldRoomId = array[23];
        if (StringUtils.isNotEmpty(oldRoomId)){
            userOrderDto.setOldRoomId(oldRoomId);
        }

        //订单描述(选填)
        String description = array[2];
        if (StringUtils.isNotEmpty(description)){
            userOrderDto.setDescription(description);
        }
        //抵扣金额(选填)
        String offsetAmount = array[5];
        if (StringUtils.isNotEmpty(offsetAmount)){
            userOrderDto.setOffsetAmount(new BigDecimal(offsetAmount));
        }
        //是否收费(选填)
        String isCharge = array[9];
        if (StringUtils.isNotEmpty(isCharge)){
            userOrderDto.setIsCharge(Integer.valueOf(isCharge));
        }
        //第三方订单号(选填)
        String transactionId = array[10];
        if (StringUtils.isNotEmpty(transactionId)){
            userOrderDto.setTransactionId(transactionId);
        }
        //是否退款(选填)
        String refund = array[11];
        if (StringUtils.isNotEmpty(refund)){
            userOrderDto.setRefund(Integer.valueOf(refund));
        }
        //退款金额(选填)
        String refundFee = array[12];
        if (StringUtils.isNotEmpty(refundFee)){
            userOrderDto.setRefundFee(new BigDecimal(refundFee));
        }
        //退款时间(选填)
        String refundDate = array[13];
        if (StringUtils.isNotEmpty(refundDate)){
            userOrderDto.setRefundDate(ft.parse(refundDate));
        }
        //手机型号(选填)
        String phoneType = array[14];
        if (StringUtils.isNotEmpty(phoneType)){
            userOrderDto.setPhoneType(phoneType);
        }
        //费用类型(选填)
        String feeType = array[15];
        if (StringUtils.isNotEmpty(feeType)){
            userOrderDto.setFeeType(feeType);
        }
        //费用规则(选填)
        String feeRule = array[16];
        if (StringUtils.isNotEmpty(feeRule)){
            userOrderDto.setFeeRule(feeRule);
        }
        //地点(选填)
        String address = array[17];
        if (StringUtils.isNotEmpty(address)){
            userOrderDto.setAddress(address);
        }
        return userOrderDto;
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "订单号(必填)",
            "订单类型(必填)",
            "订单描述(选填)",
            "应付款(必填)",
            "实际付款(必填)",
            "抵扣金额(选填)",
            "支付状态(必填)",
            "支付方式(必填)",
            "支付时间(必填)",
            "是否收费(选填)",
            "第三方订单号(选填)",
            "是否退款(选填)",
            "退款金额(选填)",
            "退款时间(选填)",
            "手机型号(选填)",
            "费用类型(选填)",
            "费用规则(选填)",
            "地点(选填)",
            "订单原始数据id(必填)",
            "用户原始数据id(必填)",
            "人脸商品原始数据id(必填)",
            "小区原始数据id(必填)",
            "单元原始数据id(必填)",
            "房间原始数据id(必填)"
    ));

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