package com.fjs.scenic.service.user.impl;

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

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

import com.fjs.scenic.entity.user.LinkMan;
import com.fjs.scenic.mapper.user.LinkManMapper;
import com.fjs.scenic.service.user.LinkManService;
import com.fjs.scenic.utils.ReturnResult;

@Service
public class LinkManServiceImpl implements LinkManService {

	@Autowired
	private LinkManMapper linkManMappper;

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return linkManMappper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(LinkMan record) {
		return linkManMappper.insert(record);
	}

	@Override
	public int insertSelective(LinkMan record) {
		return linkManMappper.insertSelective(record);
	}

	@Override
	public LinkMan selectByPrimaryKey(Integer id) {
		return linkManMappper.selectByPrimaryKey(id);
	}

	@Override
	public int update(LinkMan record) {
		return linkManMappper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(LinkMan record) {
		return linkManMappper.updateByPrimaryKey(record);
	}

	@Override
	public List<LinkMan> select(LinkMan record) {
		return linkManMappper.select(record);
	}

	@Override
	public int delete(LinkMan record) {
		return linkManMappper.delete(record);
	}

	@Override
	public ReturnResult delete180(Integer id) {
		LinkMan linkman = linkManMappper.selectByPrimaryKey(id);
		if (linkman == null) {
			return ReturnResult.error("非法操作");
		}
		if(linkman.getCreateTime()!=null){
			long day = toDay(linkman.getCreateTime());
			if(day<180){
				return ReturnResult.error("删除失败，该联系人信息还有"+(180-day)+"天才能删除");
			}
		}
		int i = linkManMappper.deleteByPrimaryKey(id);
		if(i<1){
			return ReturnResult.error("网络异常,删除失败");
		}else{
			return ReturnResult.ok("删除成功");
		}
	}

	@Override
	public int updateByParam(String param) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("param", param);
		return linkManMappper.updateByParam(map);
	}

	@Override
	public List<LinkMan> selectByParam(String field, String param) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("field", field);
		map.put("param", param);
		return linkManMappper.selectByParam(map);
	}

	@Override
	public ReturnResult addLinkMan(LinkMan linkMan) {
		int linkManCount = linkManMappper.getLinkManCount(linkMan.getUserId());
		if (linkManCount >= 10) {
			return ReturnResult.error("联系人超过10个，请删除后再添加");
		}
		LinkMan repeatMan = new LinkMan();
		repeatMan.setIdCard(linkMan.getIdCard());
		repeatMan.setUserId(linkMan.getUserId());
		// 检验当前用户下该身份证 是否已存在
		List<LinkMan> repeatInsert = linkManMappper.select(repeatMan);
		if (repeatInsert != null && repeatInsert.size() > 0) {
			return ReturnResult.error("此联系人已存在");
		}
		// 当新增的 为 默认的话，其他游客 设置为不是默认
		if (linkMan.getIsDefalt().equals("1")) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("param", " is_defalt=0 where user_id = " + linkMan.getUserId());
			linkManMappper.updateByParam(map);
		}
		linkMan.setCreateTime(new Date());
		int insertSelective = linkManMappper.insertSelective(linkMan);
		if (insertSelective < 1) {
			return ReturnResult.error("新增联系人失败");
		}
		return ReturnResult.ok("新增成功");
	}

	private long toDay(Date endDate) {
		long nd = 1000 * 24 * 60 * 60;
		long diff = new Date().getTime() - endDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		return day;
	}
}
