策略模式的应用

1 if 太多,为了解决这个用策略模式


@Service("erpNanruiInvoiceDataTaskServiceBiz")
public class ErpNanruiInvoiceDataTaskServiceBizImpl extends BaseService implements ErpNanruiInvoiceDataTaskServiceBiz {
	private Logger logger = LoggerFactory.getLogger(ErpNanruiInvoiceDataTaskServiceBizImpl.class);

	@Autowired
	private ErpNanruiInvoiceDataTaskService erpNanruiInvoiceDataTaskService;


	@Autowired
	private ErpNanruiInvoiceDataMapper erpNanruiInvoiceDataMapper;

	@Override
	public ServiceResult add(ErpInvoiceWrapper erpInvoiceWrapper) {
		// 单位报销总金额 long?
		//
		try {
			ErpNanruiInvoiceDataTask erpNanruiInvoiceDataTask=null;
			List<ErpInvoice> invoiceList = erpInvoiceWrapper.getInvoiceList();
			for (ErpInvoice erpInvoiceReq : invoiceList) {
				String invoiceType = erpInvoiceReq.getInvoiceType();
				ErpInvoiceTypeEnum instance = ErpInvoiceTypeEnum.getInstance(invoiceType);
				InvoiceHandler handler = (InvoiceHandler) SpringUtil.getBean(instance.getClazz());
				ErpNanruiInvoiceDataTask resolve = handler.resolve(erpInvoiceReq);

				erpNanruiInvoiceDataTaskService.save(erpNanruiInvoiceDataTask);

				if (("10506").equals(invoiceType)) {//飞机票
					AirLineTicketVo airLineTicketVo = eprInvoiceToAir(erpInvoiceReq);
					ServiceResult serviceResult = checkInvoiceData(airLineTicketVo, erpInvoiceReq.getInvoiceCode());//todo 将InvoiceCode修改成InvoiceId
					if (serviceResult!=null){
						return serviceResult;
					}
					 erpNanruiInvoiceDataTask = erpNanruiInvoiceDataMapper.AirLineTicketVoToErpNanruiInvoiceData(airLineTicketVo);

				}else if(("10503").equals(invoiceType)){//火车票
					TrainTicketVo trainTicketVo = eprInvoiceToTrain(erpInvoiceReq);
					ServiceResult serviceResult = checkInvoiceData(trainTicketVo, erpInvoiceReq.getInvoiceCode());//todo 将InvoiceCode修改成InvoiceId
					if (serviceResult!=null){
						return serviceResult;
					}
					erpNanruiInvoiceDataTask = erpNanruiInvoiceDataMapper.TrainTicketVoToErpNanruiInvoiceData(trainTicketVo);

				}else if(("10505a").equals(invoiceType)){//轮船
					SteamerTicketVo steamerTicketVo = eprInvoiceToSteamer(erpInvoiceReq);
					ServiceResult serviceResult = checkInvoiceData(steamerTicketVo, erpInvoiceReq.getInvoiceCode());
					if (serviceResult!=null){
						return serviceResult;
					}
					erpNanruiInvoiceDataTask = erpNanruiInvoiceDataMapper.SteamerTicketVoToErpNanruiInvoiceData(steamerTicketVo);

				}else if(("10505").equals(invoiceType)){//汽车
					BusTicketVo busTicketVo = erpInvoiceToBus(erpInvoiceReq);
					ServiceResult serviceResult = checkInvoiceData(busTicketVo, erpInvoiceReq.getInvoiceCode());
					if (serviceResult!=null){
						return serviceResult;
					}
					erpNanruiInvoiceDataTask = erpNanruiInvoiceDataMapper.BusTicketVoToErpNanruiInvoiceData(busTicketVo);


				}else if(("10101").equals(invoiceType)){
					String key = "inv";
					Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
					List<Map<String,Object>> invoiceItems =(List)((Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType())).get("invoiceItems");
                    if(CollectionUtils.isNotEmpty(invoiceItems)){
						for (Map<String, Object> invoiceItem : invoiceItems) {
							if(String.valueOf(invoiceItem.get("name")).contains("酒店")){//酒店
								HotelTicketVo hotelTicketVo = erpInvoiceToHotel(erpInvoiceReq);
								ServiceResult serviceResult = checkInvoiceData(hotelTicketVo, erpInvoiceReq.getInvoiceCode());
								if (serviceResult!=null){
									return serviceResult;
								}
								erpNanruiInvoiceDataTask = erpNanruiInvoiceDataMapper.HotelTicketVoToErpNanruiInvoiceData(hotelTicketVo);


							}
						}
					}

				}else if(("10500").equals(invoiceType)||("20105").equals(invoiceType)){//出租车发票,滴滴出行行程单
					TrafficTicketVo trafficTicketVo = erpInvoiceToTraffic(erpInvoiceReq, invoiceType);
					ServiceResult serviceResult = checkInvoiceData(trafficTicketVo, erpInvoiceReq.getInvoiceCode());if (serviceResult!=null){
						return serviceResult;
					}
					erpNanruiInvoiceDataTask = erpNanruiInvoiceDataMapper.TrafficTicketVoToErpNanruiInvoiceData(trafficTicketVo);
				}else{


				}

				erpNanruiInvoiceDataTaskService.save(erpNanruiInvoiceDataTask);

			}
			return buildServiceResult(1);
		} catch (Exception e) {
			//LogUtil.encError(logger, "查询失败: ", e);
			return buildErrorResult("查询失败,系统出现异常");
		}

	}

	public InvoiceTicketVo erpInvoiceToInvoice(ErpInvoice erpInvoiceReq) {
		InvoiceTicketVo invoiceTicketVo=new InvoiceTicketVo();
		//steamerTicketVo.setInvoiceId();发票id
		invoiceTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		invoiceTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		invoiceTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		invoiceTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
		invoiceTicketVo.setInvoiceDate(erpInvoiceReq.getInvoiceDate());
		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		invoiceTicketVo.setCity(String.valueOf(map.get("city")));//未给
		invoiceTicketVo.setCheckCodeLastSex(String.valueOf(map.get("checkCode")));
		invoiceTicketVo.setStartPlace(String.valueOf(map.get("startPlace")));//? 其他类型的开始地点给的字段是否一致?
		invoiceTicketVo.setEndPlace(String.valueOf(map.get("endPlace")));//?
		invoiceTicketVo.setCheckCodeLastSex(String.valueOf(map.get("checkCode")));//?
		return invoiceTicketVo;

	}

	public TrafficTicketVo erpInvoiceToTraffic(ErpInvoice erpInvoiceReq,String invoiceType) {
		TrafficTicketVo trafficTicketVo = new TrafficTicketVo();
		//steamerTicketVo.setInvoiceId();发票id
		trafficTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		trafficTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		trafficTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		trafficTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		trafficTicketVo.setCity(String.valueOf(map.get("city")));//未给
		if (("10500").equals(invoiceType)) {
			trafficTicketVo.setDateStart(erpInvoiceReq.getInvoiceDate() + "" + String.valueOf(map.get("timeGeton")));
			trafficTicketVo.setDateEnd(erpInvoiceReq.getInvoiceDate() + "" + String.valueOf(map.get("timeGetoff")));
			trafficTicketVo.setStartPlace(String.valueOf(map.get("place")));//?开始地点
			trafficTicketVo.setStartPlace(String.valueOf(map.get("place")));//?结束地点
		}
		if (("20105").equals(invoiceType)) {
			trafficTicketVo.setDateStart(erpInvoiceReq.getInvoiceDate() + "" + String.valueOf(map.get("dateStart")));
			trafficTicketVo.setDateEnd(erpInvoiceReq.getInvoiceDate() + "" + String.valueOf(map.get("dateEnd")));
			trafficTicketVo.setStartPlace(String.valueOf(map.get("stationGeton")));//?开始地点
			trafficTicketVo.setStartPlace(String.valueOf(map.get("stationGetoff")));//?结束地点
		}

		return trafficTicketVo;
	}

	public HotelTicketVo erpInvoiceToHotel(ErpInvoice erpInvoiceReq) {
		HotelTicketVo hotelTicketVo=new HotelTicketVo();
		//steamerTicketVo.setInvoiceId();发票id
		hotelTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		hotelTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		hotelTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		hotelTicketVo.setInvoiceDate(erpInvoiceReq.getInvoiceDate());
		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		hotelTicketVo.setCity(String.valueOf(map.get("city")));//未给
		hotelTicketVo.setCheckCodeLastSex(String.valueOf(map.get("checkCode")));
		hotelTicketVo.setSeller(String.valueOf(map.get("seller")));
		hotelTicketVo.setTaxAmount(String.valueOf(map.get("tax")));
		hotelTicketVo.setTaxRate(String.valueOf(map.get("taxRate")));
		return hotelTicketVo;

	}

	public BusTicketVo erpInvoiceToBus(ErpInvoice erpInvoiceReq) {
		BusTicketVo busTicketVo=new BusTicketVo();
		//steamerTicketVo.setInvoiceId();发票id
		busTicketVo.setUserId(erpInvoiceReq.getApplicant());
		busTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		busTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		busTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		busTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
		busTicketVo.setTicketAmount(erpInvoiceReq.getInvoiceAmount());
		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		busTicketVo.setStartPlace(String.valueOf(map.get("stationGeton")));
		busTicketVo.setEndPlace(String.valueOf(map.get("stationGetoff")));
		return busTicketVo;

	}

	public SteamerTicketVo eprInvoiceToSteamer(ErpInvoice erpInvoiceReq) {
		SteamerTicketVo steamerTicketVo=new SteamerTicketVo();
		//steamerTicketVo.setInvoiceId();发票id
		steamerTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		steamerTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		steamerTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		steamerTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
		steamerTicketVo.setTicketAmount(erpInvoiceReq.getInvoiceAmount());
		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		steamerTicketVo.setName(String.valueOf(map.get("name")));
        steamerTicketVo.setStartPlace(String.valueOf(map.get("stationGeton")));
        steamerTicketVo.setEndPlace(String.valueOf(map.get("stationGetoff")));
        steamerTicketVo.setCity(String.valueOf(map.get("city")));//未给
		return steamerTicketVo;

	}


	public TrainTicketVo eprInvoiceToTrain(ErpInvoice erpInvoiceReq) {
		TrainTicketVo trainTicketVo=new TrainTicketVo();
		//trainTicketVo.setName(erpInvoiceReq.getInvoiceId());发票id
		trainTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		trainTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		trainTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		trainTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
		trainTicketVo.setTicketAmount(erpInvoiceReq.getInvoiceAmount());
		trainTicketVo.setInvoiceDate(erpInvoiceReq.getInvoiceDate());
		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		trainTicketVo.setName(String.valueOf(map.get("name")));
		trainTicketVo.setStartPlace(String.valueOf(map.get("stationGeton")));
		trainTicketVo.setEndPlace(String.valueOf(map.get("stationGetoff")));
		return trainTicketVo;
	}


	public AirLineTicketVo eprInvoiceToAir(ErpInvoice erpInvoiceReq) {
		AirLineTicketVo airLineTicketVo = new AirLineTicketVo();
		//airLineTicketVo.setInvoiceId(erpInvoiceReq.geti);发票id

		airLineTicketVo.setUserId(erpInvoiceReq.getApplicant());
		airLineTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
		airLineTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
		airLineTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
		airLineTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
		airLineTicketVo.setInvoiceDate(erpInvoiceReq.getInvoiceDate());

		String key = "inv";
		Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
		Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
		airLineTicketVo.setName(String.valueOf(map.get("userName")));
		airLineTicketVo.setCheckCodeLastSex(String.valueOf(map.get("checkCode")));
		airLineTicketVo.setTicketAmount(map.get("fare")==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(map.get("fare"))));
		airLineTicketVo.setCaacDevelopmentFund(map.get("caacDevelopmentFund")==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(map.get("caacDevelopmentFund"))));
		airLineTicketVo.setFuelSurcharge(map.get("fuelSurcharge")==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(map.get("fuelSurcharge"))));
		airLineTicketVo.setTaxAmount(map.get("tax")==null&&map.get("tax")==""?BigDecimal.ZERO:new BigDecimal(String.valueOf(map.get("tax"))));


		List<Map<String, Object>> invoiceItems = (List) map.get("invoiceItems");
		if (invoiceItems.size() > 0) {
			for (Map<String, Object> invoiceItem : invoiceItems) {
				airLineTicketVo.setDateStart(String.valueOf(invoiceItem.get("date")));
				airLineTicketVo.setStartPlace(String.valueOf(invoiceItem.get("from")));
				airLineTicketVo.setEndPlace(String.valueOf(invoiceItem.get("to")));
			}

		}
		return   airLineTicketVo;

	}


		public static <T> String validBean(T t) {

		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

		Validator validator = factory.getValidator();

		Set<ConstraintViolation<T>> validate =
				validator.validate(t);

		Iterator<ConstraintViolation<T>> iterator = validate.iterator();

		while (iterator.hasNext()) {
			ConstraintViolation<T> error = iterator.next();
			String errorMessage = new StringBuilder()
					.append("[")
					.append(error.getPropertyPath().toString())
					.append("]")
					.append(error.getMessage())
					.toString();
			return errorMessage;
		}
		return null;
	}



	public ServiceResult checkInvoiceData(Object object,String invoiceId) {
		String s = validBean(object);
		if (!StringUtils.isEmpty(s)) {
			Map resultMap = new HashMap();
			resultMap.put("invoiceId", invoiceId);
			resultMap.put("message", s);
			return buildServiceResult("推送失败", false, resultMap);

		}
		return null;
	}

}

2 策略模式

2.1定义一个枚举

package cn.com.esgcc.gwtrip.erp.enums;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@ToString
public enum ErpInvoiceTypeEnum {

    /**
     * 飞机—航空行程单、增值税发票且名称包含“退票”的?(10506:航空运输电子客票行程单)
     * 火车—火车票(10503:火车票)
     * 轮船—船票(10505a:船票)
     * 住宿(酒店)—增值税发票且名称包含“住宿服务”的 (10103:增值税普通发票(卷票))
     * 汽车—汽车客运票(10505:客运汽车)
     * 市内交通—出租车、滴滴行程单 (20105:滴滴出行行程单,10500:出租车发票)
     * 其他费用—机打发票、定额发票、小票、过路费、增值税卷票、可报销其他发票(例:火车退票)、其他增值税发票
     */

    INVOICE_TYPE_AIR(1,"10506", "airHandler"),
    INVOICE_TYPE_TRAIN(2,"10503","trainTicketHandler"),
    INVOICE_TYPE_SHIP(3,"10505a","steamerTicketHandler"),
    INVOICE_TYPE_HOTEL(4,"10103","hotelTicketHandler"),
    INVOICE_TYPE_CAR(5,"10505","busTicketHandler"),
    INVOICE_TYPE_TAXI(6,"20105","trafficTicketHandler"),
    INVOICE_TYPE_DIDI(6,"10500","TrainTicketHandler"),
    ;

    private Integer status;
    private String value;
    private String name;


    ErpInvoiceTypeEnum(Integer status,String value,String name){
        this.status=status;
        this.value=value;
        this.name = name;
    }

    public static ErpInvoiceTypeEnum getInstance(String value) {

        for (ErpInvoiceTypeEnum erpInvoiceTypeEnum : values()) {
            if (erpInvoiceTypeEnum.getValue().equals(value)) {
                return erpInvoiceTypeEnum;
            }
        }
        return null;
    }
}

2.2 定义公共接口

public interface InvoiceHandler {

    /**
     *
     * @param erpInvoiceReq
     * @return
     */
    Object resolve(ErpInvoice erpInvoiceReq);

    /**
     * 复制到新的bean
     * @param result
     * @return
     */
    ErpNanruiInvoiceDataTask generatorTask(Object result);

}

2.3各个实现类

package cn.com.esgcc.gwtrip.erp.invoice.handler;

import cn.com.esgcc.gwtrip.common.service.ServiceResult;
import cn.com.esgcc.gwtrip.erp.invoice.entity.mapper.ErpNanruiInvoiceDataMapper;
import cn.com.esgcc.gwtrip.erp.invoice.entity.po.ErpNanruiInvoiceDataTask;
import cn.com.esgcc.gwtrip.erp.invoice.entity.vo.AirLineTicketVo;
import cn.com.esgcc.gwtrip.erp.invoice.entity.vo.ErpInvoice;
import cn.com.esgcc.gwtrip.erp.invoice.entity.vo.TrainTicketVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 火车票处理
 */
public class TrainTicketHandler implements InvoiceHandler{
    @Autowired
    private ErpNanruiInvoiceDataMapper erpNanruiInvoiceDataMapper;

    @Override
    public Object resolve(ErpInvoice erpInvoiceReq) {
        TrainTicketVo trainTicketVo = eprInvoiceToTrain(erpInvoiceReq);
        return trainTicketVo;
    }

    public TrainTicketVo eprInvoiceToTrain(ErpInvoice erpInvoiceReq) {
        TrainTicketVo trainTicketVo=new TrainTicketVo();
        //trainTicketVo.setName(erpInvoiceReq.getInvoiceId());发票id
        trainTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
        trainTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
        trainTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
        trainTicketVo.setInvoiceNumber(erpInvoiceReq.getInvoiceNumber());
        trainTicketVo.setTicketAmount(erpInvoiceReq.getInvoiceAmount());
        trainTicketVo.setInvoiceDate(erpInvoiceReq.getInvoiceDate());
        String key = "inv";
        Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
        Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
        trainTicketVo.setName(String.valueOf(map.get("name")));
        trainTicketVo.setStartPlace(String.valueOf(map.get("stationGeton")));
        trainTicketVo.setEndPlace(String.valueOf(map.get("stationGetoff")));
        return trainTicketVo;
    }


    @Override
    public ErpNanruiInvoiceDataTask generatorTask(Object result) {
        return erpNanruiInvoiceDataMapper.TrainTicketVoToErpNanruiInvoiceData((TrainTicketVo) result);
    }
}

package cn.com.esgcc.gwtrip.erp.invoice.handler;

import cn.com.esgcc.gwtrip.common.service.ServiceResult;
import cn.com.esgcc.gwtrip.erp.invoice.entity.mapper.ErpNanruiInvoiceDataMapper;
import cn.com.esgcc.gwtrip.erp.invoice.entity.po.ErpNanruiInvoiceDataTask;
import cn.com.esgcc.gwtrip.erp.invoice.entity.vo.ErpInvoice;
import cn.com.esgcc.gwtrip.erp.invoice.entity.vo.HotelTicketVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

public class HotelTicketHandler implements InvoiceHandler{

    @Autowired
    private ErpNanruiInvoiceDataMapper erpNanruiInvoiceDataMapper;

    @Override
    public Object resolve(ErpInvoice erpInvoiceReq) {
        HotelTicketVo hotelTicketVo=null;
        String key = "inv";
        Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
        List<Map<String,Object>> invoiceItems =(List)((Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType())).get("invoiceItems");
        if(CollectionUtils.isNotEmpty(invoiceItems)){
            for (Map<String, Object> invoiceItem : invoiceItems) {
                if(String.valueOf(invoiceItem.get("name")).contains("酒店")){//酒店
                     hotelTicketVo = erpInvoiceToHotel(erpInvoiceReq);
                }
            }
        }

        return hotelTicketVo;
    }

    public HotelTicketVo erpInvoiceToHotel(ErpInvoice erpInvoiceReq) {
        HotelTicketVo hotelTicketVo=new HotelTicketVo();
        //steamerTicketVo.setInvoiceId();发票id
        hotelTicketVo.setTotalAmount(erpInvoiceReq.getInvoiceAmount());
        hotelTicketVo.setInvoiceType(erpInvoiceReq.getInvoiceType());
        hotelTicketVo.setInvoiceCode(erpInvoiceReq.getInvoiceCode());
        hotelTicketVo.setInvoiceDate(erpInvoiceReq.getInvoiceDate());
        String key = "inv";
        Map<String, Object> invoiceExtension = erpInvoiceReq.getInvoiceExtension();
        Map map = (Map) invoiceExtension.get(key + erpInvoiceReq.getInvoiceType());
        hotelTicketVo.setCity(String.valueOf(map.get("city")));//未给
        hotelTicketVo.setCheckCodeLastSex(String.valueOf(map.get("checkCode")));
        hotelTicketVo.setSeller(String.valueOf(map.get("seller")));
        hotelTicketVo.setTaxAmount(String.valueOf(map.get("tax")));
        hotelTicketVo.setTaxRate(String.valueOf(map.get("taxRate")));
        return hotelTicketVo;

    }

    @Override
    public ErpNanruiInvoiceDataTask generatorTask(Object result) {
        return 	erpNanruiInvoiceDataMapper.HotelTicketVoToErpNanruiInvoiceData((HotelTicketVo) result);

    }
}

2.4 使用

2.4.1 springboot注入bean(SpringBeanUtil)

package cn.com.esgcc.gwtrip.erp.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanUtil implements ApplicationContextAware {
    private static Logger springBeanUtilLog = LoggerFactory.getLogger(SpringBeanUtil.class);

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringBeanUtil.applicationContext == null) {
            SpringBeanUtil.applicationContext = applicationContext;
        }
        springBeanUtilLog.info("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext={}",SpringBeanUtil.applicationContext);
    }

    /**
     * 获取applicationContext
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

2.4.2使用

@Override
	public ServiceResult add(ErpInvoiceWrapper erpInvoiceWrapper) {
		try {
			ErpNanruiInvoiceDataTask erpNanruiInvoiceDataTask=null;
			List<ErpInvoice> invoiceList = erpInvoiceWrapper.getInvoiceList();
			for (ErpInvoice erpInvoiceReq : invoiceList) {
				try{
					String invoiceType = erpInvoiceReq.getInvoiceType();
					ErpInvoiceTypeEnum instance = ErpInvoiceTypeEnum.getInstance(invoiceType);
					InvoiceHandler handler = (InvoiceHandler)SpringBeanUtil.getBean(instance.getName());
					Object result = handler.resolve(erpInvoiceReq);
					ServiceResult serviceResult = checkInvoiceData(result, erpInvoiceReq.getInvoiceCode());//todo 将InvoiceCode修改成InvoiceId
					if (serviceResult!=null){
						return serviceResult;
					}
					erpNanruiInvoiceDataTask = handler.generatorTask(result);
					erpNanruiInvoiceDataTaskService.save(erpNanruiInvoiceDataTask);
				}catch (Exception e){
					//LogUtil.encError(logger, "解析失败: {}", JSON.toJSONString(erpInvoiceReq));
					LogUtil.encError(logger, "解析失败: ", e);
				}

			}
			return buildServiceResult(1);
		} catch (Exception e) {
			LogUtil.encError(logger, "查询失败: ", e);
			return buildErrorResult("查询失败,系统出现异常");
		}

	}
上一篇:Object.prototype.valueOf()和toString()方法


下一篇:数据类型之间的转换: - 如何将字符串转换为基本数据类型? - 如何将基本数据类型转换为字符串?