package com.bohuikeji.delivery.backstage.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.bohuikeji.delivery.backstage.entity.TLogistics;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.frame.module.admin.base.SuperBase;
import com.bohuikeji.frame.module.admin.exception.WebVerifiedException;
import com.bohuikeji.frame.module.admin.utils.RegUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.Data;

import java.text.MessageFormat;

/**
 * 批量导入订单对象
 */
@Data
public class OrderDom extends SuperBase {

    /**
     * 寄件人姓名
     */
    @ExcelProperty(index = 2)
    private String mailingName;

    /**
     * 寄件人电环
     */
    @ExcelProperty(index = 3)
    private String mailingPhone;
    /**
     * 寄件人地址
     */
    @ExcelProperty(index = 5)
    private String mailingAddress;
    /**
     * 收件人姓名
     */
    @ExcelProperty(index = 7)
    private String receiptName;
    /**
     * 收件人电话
     */
    @ExcelProperty(index = 8)
    private String receiptPhone;
    /**
     * 收件人地址
     */
    @ExcelProperty(index = 10)
    private String receiptAddress;

    /**
     * 物品内容
     */
    @ExcelProperty(index = 12)
    private String itemDesc;

    /**
     * 物品类型
     */
    @ExcelProperty(index = 13)
    private String itemType;

    /**
     * 包裹数量
     */
    @ExcelProperty(index = 15)
    private Integer packageCount;

    /**
     * 业务类型
     */
    @ExcelProperty(index = 19)
    private String serviceType;

    /**
     * 支付方式
     */
    @ExcelProperty(index = 21)
    private String payWay;
    /**
     * 备注信息
     */
    @ExcelProperty(index = 30)
    private String rmk;



    /**
     * 校验并转换为订单对象
     * @return
     */
    public TOrder coverToOrder(){
        TOrder order=new TOrder();
        order.setMailingName(this.mailingName);
        order.setMailingPhone(this.mailingPhone);
        order.setReceiptName(this.receiptName);
        order.setReceiptPhone(this.receiptPhone);
        order.setOrderRemarks(this.rmk);
        order.setPlayerInfo(this.serviceType+"/"+this.itemType+"/"+this.itemDesc+"/"+this.payWay);
        order.setPackageCount(Integer.valueOf(this.packageCount));
        /*try {
            order.setPackageCount(Integer.valueOf(this.packageCount));
        }catch (Exception e){
            throw new WebVerifiedException("包裹数量读取错误，无法识别包裹数量")
        }*/
        order.setPackageCount(this.packageCount);
        return order;
    }

    /**
     * 校验并转化为物流对象
     * @return
     */
    private TLogistics coverToLogistics(){
        TLogistics logistics=new TLogistics();
        //logistics.set
        return  logistics;
    }

    /**
     * 检查订单
     * 失败返回异常信息
     * 成功返回null
     * @return
     */
    public String checkOrder(int index){
        if(StringUtils.isBlank(this.mailingName)){
            return MessageFormat.format( "寄件人姓名为空！位于第[{0}]行",index);
        }
        if(StringUtils.isBlank(this.mailingPhone)){
            return MessageFormat.format( "寄件电话为空！位于第[{0}]行",index);
        }
        if(StringUtils.isBlank(this.mailingAddress)){
            return MessageFormat.format( "寄件地址为空！位于第[{0}]行",index);
        }
        if(!RegUtils.isChinaPhoneLegal(this.mailingPhone)){
            return MessageFormat.format( "寄件电话不合法！位于第[{0}]行",index);
        }
        if(StringUtils.isBlank(this.receiptName)){
            return MessageFormat.format( "收件人姓名为空！位于第[{0}]行",index);
        }
        if(StringUtils.isBlank(this.receiptPhone)){
            return MessageFormat.format( "收件电话为空！位于第[{0}]行",index);
        }
        if(!RegUtils.isChinaPhoneLegal(this.receiptPhone)){
            return MessageFormat.format( "收件电话不合法！位于第[{0}]行",index);
        }
        if(StringUtils.isBlank(this.receiptAddress)){
            return MessageFormat.format( "收件地址为空！位于第[{0}]行",index);
        }
        return null;
    }



}
