/**  
* Title: CrmRuleController.java 
* Description: 
* @author dsc  
* @date 2020年6月11日  
* @version 1.0  
*/
package cn.timer.api.controller.crm;

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.timer.api.bean.crm.CrmClientAssociate;
import cn.timer.api.bean.crm.CrmClientData;
import cn.timer.api.bean.crm.CrmSeaRule;

/**
 * Title: CrmRuleController.java
 * 
 * @Description: 公海规则
 * @author dsc
 * @date 2020年6月11日
 * @version 1.0
 */

@Component
public class CrmRuleController {

	@Scheduled(cron = "0 0 10 * * ? ") // 每小时扫描
	@Transactional
	public void getRules() {
		// 全部业务组规则
		List<CrmSeaRule> crmSeaRules = CrmSeaRule.builder().build().selectAll();
		for (CrmSeaRule crmSeaRule : crmSeaRules) {
			// 单个业务组规则信息
			Integer one = crmSeaRule.getOne();
			Integer two = crmSeaRule.getTwo();
			Integer three = crmSeaRule.getThree();
			Integer four = crmSeaRule.getFour(); // app提醒
//			Integer five = crmSeaRule.getFive();  // 客户添加限制(暂不做)
			Integer gid = crmSeaRule.getGid();
			// 单个业务组对应的全部客户
			List<CrmClientData> crmClientDatas = CrmClientData.builder().build()
					.selectList(new LambdaQueryWrapper<CrmClientData>().eq(CrmClientData::getBelongGroup, gid).select(
							CrmClientData::getCreateTime, CrmClientData::getId, CrmClientData::getLastFollowTime,
							CrmClientData::getClientStatus, CrmClientData::getBelongUserName,
							CrmClientData::getClientName));
			for (CrmClientData crmClientData : crmClientDatas) {
				// 单个客户信息
				Date createTime = crmClientData.getCreateTime();
				Date lastFollowTime = crmClientData.getLastFollowTime();
				Integer status = crmClientData.getClientStatus();
				String belongUserName = crmClientData.getBelongUserName();
				String clientName = crmClientData.getClientName();
				Date now = new Date();
				// 客户创建时间+ x天 = 过期释放时间
				DateTime overDate1 = DateUtil.offsetDay(createTime, one);
				long compare2 = 0;
				if (lastFollowTime != null) {
					DateTime overDate2 = DateUtil.offsetDay(lastFollowTime, two);
					compare2 = DateUtil.between(overDate2, now, DateUnit.DAY, false);
					if (compare2 == -four)
						remind(belongUserName, clientName, four);
				}
				DateTime overDate3 = DateUtil.offsetDay(createTime, three);
				// 时间比较
				long compare1 = DateUtil.between(overDate1, now, DateUnit.DAY, false);

				long compare3 = DateUtil.between(overDate3, now, DateUnit.DAY, false);
				// 判断1和2和3
				if ((lastFollowTime == null && compare1 >= 0) || (lastFollowTime != null && compare2 >= 0)
						|| (compare3 >= 0 && status != 3 && status != 4))
					release(crmClientData);
				// 判断4
				if (compare1 == -four || compare3 == -four)
					remind(belongUserName, clientName, four);
			}
		}
	}

	/**
	 * @param four
	 * @param name
	 */
	// app提醒
	private void remind(String belongUserName, String clienName, Integer four) {
		Logoutput(
				belongUserName + "的客户:" + clienName + "还有" + four + "天即将过期," + "正在向'" + belongUserName + "'的App发推送提醒");
		// TODO
	}

	// 释放到公海
	private void release(CrmClientData crmClientData) {
		crmClientData.setBelongUser(0); // 0-进入客户公海
		crmClientData.setBelongUserName(null);
		// 删除协作人列表
		CrmClientAssociate.builder().build().delete(
				new QueryWrapper<CrmClientAssociate>().lambda().eq(CrmClientAssociate::getCid, crmClientData.getId()));
		crmClientData.updateById();
	}
	
	@Value("${config-8timer.environmental-science}")
	public String environmental_science;
	
	public void Logoutput(String science) {
		
		if(!("pro").equals(environmental_science)) {
			
			System.out.println(science);
		}else {
			System.out.println("");
		}
		
	}

}