package com.iamberry.wechat.service.task;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.member.CashLog;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.task.TaskModel;
import com.iamberry.wechat.core.entity.task.WechatTask;
import com.iamberry.wechat.core.entity.task.WechatTaskLogs;
import com.iamberry.wechat.face.member.CashLogService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.reback.RebackServices;
import com.iamberry.wechat.face.task.WechatTaskService;
import com.iamberry.wechat.service.member.dao.CashLogDao;
import com.iamberry.wechat.service.member.dao.MemberDao;
import com.iamberry.wechat.service.task.dao.WechatTaskDao;
/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年9月20日
 * @explain	
 */
@Service
public class WechatTaskServiceImpl implements WechatTaskService {
	
	@Autowired
	private WechatTaskDao wechatTaskDao;
	
	@Autowired
	private RebackServices rebackServices;
	
	@Autowired
	private MemberDao memberService;
	
	@Autowired
	private CashLogDao cashLogsService;

	@Override
	public List<WechatTask> getAll(int pageSize, int pageNO) {
		// TODO Auto-generated method stub
		PaperBean bean = new PaperBean();
		bean.setPaperData(pageNO, pageSize);
		return wechatTaskDao.getAll(bean);
	}

	@Override
	public int getCount(int pageSize) {
		// TODO Auto-generated method stub
		int count = wechatTaskDao.getCount();
		count = PaperBean.justPaperCount(count, pageSize);
		return count;
	}

	@Override
	public int insertTask(WechatTask task) {
		// TODO Auto-generated method stub
		return wechatTaskDao.insertTask(task);
	}

	@Override
	public int updateTask(WechatTask task) {
		// TODO Auto-generated method stub
		return wechatTaskDao.updateTask(task);
	}

	@Override
	public int insertLogs(WechatTaskLogs logs) {
		// TODO Auto-generated method stub
		return wechatTaskDao.insertLogs(logs);
	}

	@Override
	public List<WechatTaskLogs> getByTaskID(Integer taskID) {
		// TODO Auto-generated method stub
		return wechatTaskDao.getByTaskID(taskID);
	}

	@Override
	public Integer getByTaskIdAndOpenId(WechatTaskLogs logs) {
		// TODO Auto-generated method stub
		return wechatTaskDao.getByTaskIdAndOpenId(logs);
	}

	@Override
	public WechatTask getOne(Integer taskID) {
		// TODO Auto-generated method stub
		return wechatTaskDao.getOne(taskID);
	}

	@Override
	public List<WechatTask> getRunTask(Date statDate, Date endDate) {
		// TODO Auto-generated method stub
		WechatTask task = new WechatTask();
		task.setTaskStartDate(statDate);
		task.setTaskEndDate(endDate);
		return wechatTaskDao.getRunTask(task);
	}

	@Override
	public boolean handlerTask(String openID, int count, Integer taskID, Integer productType) throws Exception {
		// TODO Auto-generated method stub
		
		// 添加记录
		WechatTask task = this.getOne(taskID);
		WechatTaskLogs logs = new WechatTaskLogs();
		logs.setLogsTaskId(taskID);
		logs.setLogsUserOpenId(openID);
		logs.setLogsProductType(productType);
		logs.setLogsNum(task.getTaskSalesNum());
		logs.setLogsMoney(count);
		logs.setLogsStartDate(task.getTaskStartDate());
		logs.setLogsEndDate(task.getTaskEndDate());
		logs.setLogsDesc(task.getTaskDesc());
		logs.setLogsCreateTime(new Date());
		int result = wechatTaskDao.insertLogs(logs);
		if (result < 1) {
			throw new RuntimeException("添加记录报错!");
		}

		// 添加现金记录
		CashLog cashLog = new CashLog();
		cashLog.setCashLogsOpenid(openID);
		cashLog.setCashLogsNum(count);
		cashLog.setCashLogsType(1); 	// 收入
		cashLog.setCashLogsResType(6);  // 任务奖励
		cashLog.setCashLogsIntroduction("完成:" + taskID + "任务,奖励" + count + "积分");
		cashLog.setCashLogsOrderid(taskID.toString());
		cashLog.setCashLogsCreateDate(new Date());
		result = cashLogsService.addCashLog(cashLog);
//		int  i = 1 / 0;
		if (result < 1) {
			throw new RuntimeException("添加现金记录报错!");
		}
		
		// 修改用户可用金额
		Member member = new Member();
		member.setUserOpenid(openID);
		member.setUserIncome(count);
		result = memberService.updateUserIncomeByOpenId(member);
		if (result < 1) {
			throw new RuntimeException("修改用户可用金额报错!");
		}
		return true;
	}
}
