package com.ssj.service.task;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.ssj.bean.sys.award.domain.AwardSncode;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.AdvancedUtil;
import com.ssj.framework.weixin.util.BarcodeFactory;
import com.ssj.service.sys.award.service.AwardSncodeService;
import com.ssj.taskthread.Task;

public class SyncCreateQrSnCodeTask extends Task {
	
    private List<AwardSncode> awardSncodes;

    public SyncCreateQrSnCodeTask(List<AwardSncode>  awardSncodes) {
        super();
        this.awardSncodes = awardSncodes;
    }

    @Override
    public void run() {
    	logger.info("生成活动的兑换码图片开始");
        try {
        	AwardSncodeService awardSncodeService = SystemResourceLocator.getBean(AwardSncodeService.class);
        	for (AwardSncode awardSncode : awardSncodes) {
        		String snCodeImg=createQrCode(awardSncode.getSnCode());
        		awardSncodeService.updateSnCodeImgBySnCode(awardSncode.getSnCode(), snCodeImg);
        		logger.info("生成活动的兑换码图片成功，SnCode={}",awardSncode.getSnCode());
			}
        }catch (Exception e) {
        	 logger.info("生成活动的兑换码图片异常，e={}",e.getMessage());
		}
    }
    
    public static String createQrCode(String snCode) throws Exception {
		String file_path = (String) SystemResourceLocator.getValue("file_save_path");
		String frontUrl=(String) SystemResourceLocator.getValue("frontUrl");
		String fPath = "/SNCODE/";
		fPath += DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
		if (!fPath.endsWith("/")) {
			fPath += "/";
		}
		File f = new File(file_path + fPath);
		if (!f.exists()) {
			f.mkdirs();
		}
		String url = frontUrl+"/dist/index.html?sncode="+snCode;
		String userInfoUrl=AdvancedUtil.getuserinfourl(url);
		String fPathId = fPath + snCode+".jpg";
		try {
			BarcodeFactory.bookencode(userInfoUrl, file_path + fPathId, 400, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fPathId;
	}



}
