package com.ssj.service.sys.award.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ssj.bean.sys.award.domain.AwardMaterial;
import com.ssj.bean.sys.award.domain.AwardSncode;
import com.ssj.dao.sys.award.dao.AwardQueryDao;
import com.ssj.dao.sys.award.dao.AwardSncodeDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.award.service.AwardSncodeService;
import com.ssj.service.task.SyncCreateQrSnCodeTask;
import com.ssj.taskthread.ThreadPool;

@Service
@Transactional
public class AwardSncodeServiceImpl extends BaseServiceImpl<AwardSncode, String> implements AwardSncodeService {
	
	@Autowired
	private AwardSncodeDao  awardSncodeDao;

	@Autowired
	private AwardQueryDao  queryDao;
	
	@Override
	public Page<Map<String, Object>> findAwardSncodeBylist(Map<String, Object> searchParams, Pageable pageable) {
		return queryDao.findAwardSncodeBylist(searchParams, pageable);
	}
	
	@Override
	public PagingAndSortingRepository<AwardSncode, String> getDao() {
		return awardSncodeDao;
	}
	
	@Override
	public synchronized void createAwardSncodeByCode(AwardMaterial awardMaterial,Integer num) {
		List<AwardSncode> awardSncodes=new ArrayList<AwardSncode>();
		AwardSncode awardSncode=null;
		for (int i = 1; i <= num; i++) {
			awardSncode=new AwardSncode();
			awardSncode.setAwardMaterialId(awardMaterial.getId());
			awardSncode.setSnCode(DateHelper.getOrderNo(5)+i);
			awardSncode.setStatus(0);
			awardSncode.setCreateTime(new Date());
			awardSncodes.add(awardSncode);
		}
		//保存兑换码
		this.save(awardSncodes);
		//生成兑换码图片
		ThreadPool.getInstance().addTask(new SyncCreateQrSnCodeTask(awardSncodes));
	}
	
	
	@Override
	public Page<Map<String, Object>> findAwardSncodeByVipList(Map<String, Object> searchParams, Pageable pageable) {
		return queryDao.findAwardSncodeByVipList(searchParams, pageable);
	}

	
	@Override
	public void updateSnCodeImgBySnCode(String snCode, String snCodeImg) {
		awardSncodeDao.updateSnCodeImgBySnCode(snCode, snCodeImg);
	}


}
