package com.goafanti.common.task;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.mail.MessagingException;

import org.apache.log4j.LogSF;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goafanti.common.bo.EmailBo;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.NoticeMapper;
import com.goafanti.common.enums.NoticeStatus;
import com.goafanti.common.model.Notice;
import com.goafanti.common.model.User;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.common.utils.SendEmailUtil;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.customer.bo.LockingReleaseBo;
import com.goafanti.customer.service.CustomerService;
import com.goafanti.user.service.UserService;
@Component
@Controller
public class ReleaseUserTask {
	@Resource
	private CustomerService customerServiceImpl;
	@Resource
	private UserService userServiceImpl;
	@Autowired
	private NoticeMapper	noticeMapper;
	
	int pointsDataLimit=50;
	
	/**
	 * 每天凌晨一点将客户和业务转为待释放
	 */
	
	@Scheduled(cron = "0 0 1  * * ?") 
	@RequestMapping(value = "/open/updateUser", method = RequestMethod.GET)
	public void updateUser(){
		LoggerUtils.debug(getClass(), "==============客户释放开始============");
		Date releaseTime = new Date();
		try {
			List<User> userList = userServiceImpl.selectUserByRoleName("营销员","营销经理");
			List<LockingReleaseBo> lockUserList = new ArrayList<LockingReleaseBo>();
			List<LockingReleaseBo> lockBusinessList = new ArrayList<LockingReleaseBo>();
			List<LockingReleaseBo> userTmpList = null;
			List<LockingReleaseBo> businessTmpList = null;
			if (userList!=null&&!userList.isEmpty()) {
				for(User u : userList){
					if(StringUtils.isNotBlank(u.getId())) {
						userTmpList = customerServiceImpl.selectWaitReleaseCustomer(u.getId());
//					userTmpList = customerServiceImpl.selectWaitReleaseCustomer("1");
						lockUserList.addAll(userTmpList);
						businessTmpList = customerServiceImpl.selectWaitReleaseBusiness(u.getId());
//					businessTmpList = customerServiceImpl.selectWaitReleaseBusiness("1");
						lockBusinessList.addAll(businessTmpList);
					}
				}
				
			}
		List<LockingReleaseBo> newList=new ArrayList<LockingReleaseBo>();
			  if (lockUserList!=null&&lockUserList.size()>0) {
				  for(int i=0;i<lockUserList.size();i++){
					  newList.add(lockUserList.get(i));
					  if(pointsDataLimit == newList.size()||i == lockUserList.size()-1){
						  if(newList.size()>0) customerServiceImpl.updatePendingReleaseLock(newList);
						  newList.clear();
						  Thread.sleep(2000);
					  }  
				  }
			}
			  if (lockBusinessList!=null&&lockBusinessList.size()>0) {
				  for(int i=0;i<lockBusinessList.size();i++){
					  newList.add(lockBusinessList.get(i));
					  if(pointsDataLimit == newList.size()||i == lockBusinessList.size()-1){
						  if(newList.size()>0) customerServiceImpl.updatePendingReleaseLock(newList);
						  newList.clear();
						  Thread.sleep(2000);
					  }  
				  }
			}
			List<LockingReleaseBo> lockList=customerServiceImpl.selectPendinglockUserList();
			if (lockList.size()>0)customerServiceImpl.updateReleaseLock(releaseTime);
			/*if(lockUserList.size()>0) customerServiceImpl.updateReleaseLock(lockUserList,releaseTime);
			if(lockBusinessList.size()>0) customerServiceImpl.updateReleaseLock(lockBusinessList,releaseTime);
			if(lockUserList.size()>0) userServiceImpl.updateReleaseLock(lockUserList);*/

		} catch (Exception e) {
			Notice n =new Notice(UUID.randomUUID().toString(),new Date(),0,"1",NoticeStatus.TASK_PATENT_ERROR.getCode(),"==============客户释放失败================");
			 noticeMapper.insert(n);
			EmailBo emailBo = new EmailBo( "释放客户失败",  AFTConstants.ADMIN_EMAIL,  "超管", "平台",  "系统", "释放客户失败");
			try {
				SendEmailUtil.getInstance().send(emailBo);
			} catch (UnsupportedEncodingException | MessagingException e1) {
				e1.printStackTrace();
			}
			LoggerUtils.error(getClass(), "====================客户释放失败=================");
			LoggerUtils.error(getClass(), "客户释放失败", e);
		}
		LoggerUtils.debug(getClass(), "==============客户释放完成============");
	}
	
}
