package com.chinacreator.process.job;

import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.dao.OrderKsPreDao;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class OrderKsPreJob {
	
	private Logger log = Logger.getLogger("ksPre");

    @Autowired
    private DictionaryDao dictionaryDao;

    @Autowired
    private OrderKsPreDao orderKsPreDao;

	public void doProcess() throws Exception {
        System.out.println("快手退订开始处理");
        List<HashMap> dataList = orderKsPreDao.queryKsPreList();
        log.info("需要退订数据========="+dataList);
        if (dataList!=null&&dataList.size()>0){
            for (HashMap map : dataList) {
                String resultCode="0";
                String resultInfo="处理成功";
                String userid = (String) map.get("USERID");
                String cpid = (String) map.get("CPID");
                String spid = (String) map.get("SPID");
                //修改状态为执行中
                orderKsPreDao.updateStatus("1",userid,spid,cpid);
                try {
                    this.cancel(userid,cpid,spid);
                    orderKsPreDao.updateKsPre("2",resultCode,resultInfo,userid,spid,cpid);
                    log.info(userid+"===退订成功");
                    orderKsPreDao.addOrderLog(userid,cpid,spid,resultCode,resultInfo,"kuaishou_h5","1");
                } catch (BusinessException e) {
                    e.printStackTrace();
                    resultCode=e.getCode();
                    resultInfo=e.getMessage();
                    boolean bool = orderKsPreDao.findRetryNub(userid, spid, cpid);
                    if (bool){
                        orderKsPreDao.updateErrorKsPre("0",resultCode,resultInfo,userid,spid,cpid,1);
                    }else{
                        orderKsPreDao.updateErrorKsPre("4",resultCode,resultInfo,userid,spid,cpid,0);
                        orderKsPreDao.addOrderLog(userid,cpid,spid,resultCode,resultInfo,"kuaishou_h5","1");
                        log.info(userid+"==退订出现异常=="+e.getMessage());
                    }
                }
            }
        }

    }
    
    /**
     * 退订
     * @param userid
     * @param cpid
     * @param spid
     * @throws BusinessException
     */
    public void cancel(String userid,String cpid,String spid) throws BusinessException{
        String result ="";
        String url="";
        try{
            url = dictionaryDao.getValue("shareCancelUrl");
            result = URLUtil.get(url+"?cpid="+cpid+"&spid="+spid+"&userid="+userid,30000);
        }catch (Exception e) {
            throw new BusinessException("9170", "调用能力共享平台退订接口异常");
        }
        JSONObject obj = JSONObject.fromObject(result);
        System.out.println("能力平台返回参数========="+obj);
        if(!obj.getString("resultCode").equals("0")){
            throw new BusinessException(obj.getString("resultCode"), obj.getString("resultInfo"));
        }
    }
}
