package com.chinacreator.process.job;

import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.dao.RedeemUserDao;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.process.util.JsonUtil;
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;
import java.util.Map;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class RedeemUserJob {

    @Autowired
    private DictionaryDao dictionaryDao;
    @Autowired
    private RedeemUserDao redeemUserDao;
    private Logger logger = Logger.getLogger(RedeemUserJob.class);

    public void doProcess() throws Exception {
        logger.info("挽回用户短信开始执行");
        long startTime = System.currentTimeMillis();
        List<HashMap> redeemUserList = redeemUserDao.getRedeemUserList();
        String userid = "";
        String cpid = "";
        String spid = "";
        String busiType = "";
        String id = "";
        int retry=0;
        if (redeemUserList!=null && redeemUserList.size()>0) {
            for (HashMap hashMap : redeemUserList) {
                try {
                    userid = String.valueOf(hashMap.get("USERID"));
                    cpid = String.valueOf(hashMap.get("CPID"));
                    spid = String.valueOf(hashMap.get("SPID"));
                    busiType = String.valueOf(hashMap.get("SMSCOF"));
                    id = String.valueOf(hashMap.get("ID"));
                    retry = Integer.parseInt(String.valueOf(hashMap.get("RETRYCNT")));
                    inserSmstMq(userid, busiType, cpid, spid);
                    redeemUserDao.updateType(id);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.info("挽回用户发送短信异常====="+userid+"===="+spid+"====="+e.getMessage());
                    redeemUserDao.updateRetry(id,String.valueOf(retry+1));
                }
                logger.info("挽回用户条数====="+redeemUserList.size()+"====执行时间==="+(System.currentTimeMillis()-startTime));
            }
        }
    }


    public void inserSmstMq(String userid, String busiType, String cpid, String spid) throws BusinessException {
        try {

            Map<String, String> map = new HashMap<String, String>();
            map.put("userid", userid);
            map.put("cpid", cpid);
            map.put("spid", spid);
            map.put("result", "0");
            map.put("channel", "");
            map.put("style", "0000");
            map.put("times", "");
            map.put("orderType", "");
            map.put("type", "cssms");
            map.put("busiType", busiType);
            //生产环境
            URLUtil.post(dictionaryDao.getValue("mqReciveUrl"), JsonUtil.objectToJson(map));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("9001","插入队列出现问题");
        }
    }
}
