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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.fjs.scenic.entity.user.User;
import com.fjs.scenic.mapper.user.UserMapper;
import com.fjs.scenic.service.user.UserService;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.ReturnResult;

/**
 * This class is used for ...
 * @CreateDate ${date} ${time}
 */
@Service
public class UserServiceImpl implements UserService {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserMapper mapper;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Override
	public List<User> select(User record) {
		return mapper.select(record);
	}

	@Override
	public void delete(User record) {
		mapper.delete(record);
	}
	
	@Override
    public int update(User obj) {
        return mapper.updateByPrimaryKeySelective(obj);
    }

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

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

	@Override
	public void deleteByParam(String param) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("param", param);
		mapper.deleteByParam(map);
	}

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

	@Override
	public ReturnResult optionUser(User wxuser) {
		User user = mapper.getUserByOpenId(wxuser.getOpenid());
		int i = 0;
		if(user!=null){
			wxuser.setId(user.getId());
			i = mapper.updateByPrimaryKeySelective(wxuser);
		}else{
			wxuser.setStatus(Cons.USER_STATUS.NORMAL);
			i = mapper.insertSelective(wxuser);
		}
		if(i<=0){
			return ReturnResult.build(500, "操作用户失败");
		}
		/*String token = UUID.randomUUID().toString();
		redisTemplate.opsForValue().set(token, wxuser.getId()+"",60 * 60 * 2,TimeUnit.SECONDS);*/
		String token = wxuser.getOpenid();
		redisTemplate.opsForValue().set(token, wxuser.getId()+"",60 * 60 * 2,TimeUnit.SECONDS);
		return ReturnResult.ok(token);
	}

	@Override
	public User getUserById(Integer id){
		return  mapper.selectByPrimaryKey(id);
	}

	@Override
	public ReturnResult updatePhone(Integer id,String phone){

        Map<String,String> map=new HashMap<>();
		map.put("param","phone="+phone);
		List<User> list=mapper.selectByParam(map);
		if(list.size()>0){
			return ReturnResult.build(500,"该手机号已绑定！");
		}

		int row=mapper.updateByPrimaryKeySelective(new User(id, null, phone, null, null, null, null, new Date(), null));
		if (row>0) {
			return ReturnResult.ok();
		}
		return ReturnResult.build(500,"绑定失败！");
	}

	@Override
	public ReturnResult frozen(Integer id){
		User u=mapper.selectByPrimaryKey(id);
		if(u==null){
			return ReturnResult.build(500,"没有此用户");
		}

		User user=new User();
		user.setId(id);
		if(u.getStatus().equals(Cons.USER_STATUS.NORMAL)){
			user.setStatus(Cons.USER_STATUS.FROZEN);
		}else {
			user.setStatus(Cons.USER_STATUS.NORMAL);
		}

		mapper.updateByPrimaryKeySelective(user);
		return new ReturnResult(200,"ok",null);
	}

	@Override
	public String getOpenIdByOrderNo(String orderNo) {
		return mapper.getOpenIdByOrderNo(orderNo);
	}

}
