package com.xhpc.pp.logic;

import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.TxException;
import com.xhpc.pp.tx.logic.ServiceLogic;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component("ServiceMainLogic")
public class ServiceMainLogic {

    protected Log log = LogFactory.getLog(getClass());

    // 结构：Map<serviceType,ServiceCommandLogic>
    @Resource
    private Map<String, ServiceLogic> serviceLogics;

    public ServiceResult process(ServiceParameter sp) {
        ServiceResult result;
        try {
            ServiceLogic logic = getServiceLogic(sp.getServiceName());

//            startTransaction(sp);
            if (sp.getParameters() == null) {
                throw TxException.INVALID_PARAMETER;
            }
            result = logic.service(sp);
//            commitTransaction(sp);
        } catch (TxException e) {
            log.error("服务请求失败：错误码[" + e.getReturnCode() + "],错误：{" + e.getMessage() + "}");
//			saveErrorLog(sp, e);

            try {
                terminateTransaction(sp);
                result = new ServiceResult(e.getReturnCode(), e.getMessage());
            } catch (Exception e1) {
                result = new ServiceResult(e1.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ServiceResult(e.getMessage());
        }
        return result;
    }

//	private void saveErrorLog(ServiceParameter param, TxException e) {
//		Map<String, String> params = param.getParameters();
//		String customerId = params.get("customerId");
//		if (customerId == null || e.getMessage() == null)
//			return;
//
//		SysLog log = new SysLog();
//		log.setSystemlogid(sequenceService.nextLogId());
//		log.setCustomerid(Integer.parseInt(customerId));
//		log.setLogtime(new Date());
//		log.setOperationdescription(e.getMessage());
//		log.setCreatetime(new Date());
//		log.setUpdatetime(new Date());
//
//		logService.save(log);
//	}

    /**
     * 创建事务对象，设置事务状态(已开始) 记录日志
     *
     * @param param
     */
    private void startTransaction(ServiceParameter param) throws Exception {

        //
    }

    /**
     * 设置事务状态（已完成），保存到数据库
     *
     * @param param
     */
    private void commitTransaction(ServiceParameter param) throws Exception {

    }

    /**
     * 设置事务状态（已失败），保存到数据库
     *
     * @param param
     */
    private void terminateTransaction(ServiceParameter param) throws Exception {

    }

    public ServiceLogic getServiceLogic(String serviceName) throws Exception {

        if (serviceName == null || "".equals(serviceName))
            throw new TxException("0101", "请求参数错误");
        ServiceLogic logic = serviceLogics.get(serviceName);
        if (logic == null)
            throw new TxException("0102", "请求的业务类型错误," + serviceName);
        return logic;
    }

}
