package com.goafanti.achievement.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import java.util.Map;
import java.util.UUID;

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


import com.goafanti.achievement.service.AchievementInterestService;
import com.goafanti.common.dao.AchievementInterestMapper;
import com.goafanti.common.dao.DemandInterestMapper;
import com.goafanti.common.dao.NewsInterestMapper;
import com.goafanti.common.dao.ProjectInterestMapper;
import com.goafanti.common.dao.UserInterestMapper;
import com.goafanti.common.enums.InterestType;
import com.goafanti.common.model.AchievementInterest;
import com.goafanti.common.model.DemandInterest;
import com.goafanti.common.model.NewsInterest;
import com.goafanti.common.model.ProjectInterest;
import com.goafanti.common.model.User;
import com.goafanti.common.model.UserInterest;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

import com.goafanti.portal.bo.AchievementInterestListBo;


@Service
public class AchievementInterestServiceImpl extends BaseMybatisDao<AchievementInterestMapper>
		implements AchievementInterestService {
	@Autowired
	private AchievementInterestMapper achievementInterestMapper;
	@Autowired
	private DemandInterestMapper demandInterestMapper;
	@Autowired
	private UserInterestMapper userInterestMapper;
	@Autowired
	private NewsInterestMapper	newsInterestMapper;
	@Autowired
	private ProjectInterestMapper projectInterestMapper;
	
	
	@Override
	public String saveAchievementInterest(String id) {
		AchievementInterest ai = new AchievementInterest();
		ai.setId(UUID.randomUUID().toString());
		ai.setUid(TokenManager.getUserId());
		if (StringUtils.isBlank(ai.getUid())){
			return null;
		}
		ai.setAchievementId(id);
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		ai.setCreateTime(now.getTime());
		achievementInterestMapper.insert(ai);
		return ai.getId();
	}

	@Override
	public int saveCancelAchievementInterest(String id) {
		return achievementInterestMapper.deleteByPrimaryKey(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<AchievementInterestListBo> listAchievementInterest(Integer pNo, Integer pSize) {
		Map<String, Object> params = new HashMap<>();

		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		if (TokenManager.getToken() instanceof User) {
			params.put("uid", TokenManager.getUserId());
		}
		return (Pagination<AchievementInterestListBo>) findPage("findAchievementInterestListByPage",
				"findAchievementInterestCount", params, pNo, pSize);
	}

	@Override
	public AchievementInterest selectAchievementInterestByUidAndAchievementId(String uid, String id) {
		return achievementInterestMapper.selectAchievementInterestByUidAndAchievementId(uid, id);
	}

	@Override
	public String saveInterest(String uid,Integer type,String objectId,String interest) {
		int i=0; 
		
		if (type==InterestType.POLICY.getCode()) {
			NewsInterest n=new NewsInterest();
			n.setId(UUID.randomUUID().toString());
			n.setUid(uid);
			n.setNewId(Long.valueOf(objectId));
			n.setCreateTime(new Date());
			i= newsInterestMapper.insert(n);
		}
		if (type==InterestType.EXPERTS.getCode()) {
			UserInterest u=new UserInterest();
			u.setId(UUID.randomUUID().toString());
			u.setFromUid(uid);
			u.setToUid(objectId);
			u.setCreateTime(new Date());
			i= userInterestMapper.insert(u);
		}
		if (type==InterestType.DEMAND.getCode()) {
			DemandInterest d=new DemandInterest();
			d.setId(UUID.randomUUID().toString());
			d.setUid(uid);
			d.setDemandId(objectId);
			d.setCreateTime(new Date());
			i= demandInterestMapper.insert(d);
		}
		if (type==InterestType.ACHIEVEMENT.getCode()) {
			AchievementInterest a=new AchievementInterest();
			a.setId(UUID.randomUUID().toString());
			a.setUid(uid);
			a.setAchievementId(objectId);
			a.setCreateTime(new Date());
			i=	achievementInterestMapper.insert(a);
		}
		if (type==InterestType.PROJECT.getCode()) {
			ProjectInterest p=new ProjectInterest();
			p.setId(UUID.randomUUID().toString());
			p.setUid(uid);
			p.setProjectId(objectId);
			p.setCreateTime(new Date());
			i=projectInterestMapper.insert(p);
		}
		if (i==1) {
			return "关注成功";
		}
		return "关注失败";
		
	}

	@Override
	public String deleteInterest(String uid, Integer type, String objectId, String interest) {
		if (StringUtils.isNotBlank(TokenManager.getUserId())) {
			 uid=TokenManager.getUserId();
		}
		int i=0;
		if (type==InterestType.POLICY.getCode()) {
			i= newsInterestMapper.deleteInterest(uid,objectId);
		}
		if (type==InterestType.EXPERTS.getCode()) {
			i= userInterestMapper.deleteInterest(uid,objectId);
		}
		if (type==InterestType.DEMAND.getCode()) {
			i= demandInterestMapper.deleteInterest(uid,objectId);
		}
		if (type==InterestType.ACHIEVEMENT.getCode()) {
			 i=achievementInterestMapper.deleteInterest(uid,objectId);
		}
		if (type==InterestType.PROJECT.getCode()) {
			 i=projectInterestMapper.deleteInterest(uid,objectId);
		}
		if (i==1) {
			return "取消关注成功";
		}
		return "取消关注失败";
	}

}
