package com.kmall.admin.service.impl;

import com.kmall.admin.dao.UserDao;
import com.kmall.admin.entity.UserEntity;
import com.kmall.admin.fromcomm.dao.SysConfigDao;
import com.kmall.admin.service.UserService;
import com.kmall.common.utils.DateUtils;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Service实现类
 *
 * @author Scott
 * @email
 * @date 2017-08-16 15:02:28
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private SysConfigDao sysConfigDao;

    @Override
    public UserEntity queryObject(Integer id) {
        return userDao.queryObject(id);
    }

    @Override
    public List<UserEntity> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userDao.queryTotal(map);
    }

    @Override
    public int save(UserEntity user) {
        user.setRegisterTime(new Date());
        return userDao.save(user);
    }

    @Override
    public int update(UserEntity user) {
        return userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return userDao.deleteBatch(ids);
    }

    /**
     * 根据用户id判断上次活体人脸核身时间是否在可调用范围内
     * @param userId
     * @return
     */
    public Map checkFaceTimeByUserId(Long userId){
        Map map = new HashMap();
        String value = sysConfigDao.queryByKey("FACE_COLLECT_FREQUENCY_TIME");
        if(value==null){
            throw new RRException("请联系管理员配系统参数置FACE_COLLECT_FREQUENCY_TIME信息！");
        }
        UserEntity userEntity = userDao.queryObject(userId);
        if(userEntity != null && StringUtils.isNotEmpty(value)){
            Integer frequencySecondTime = Integer.parseInt(value);
            String lastFaceTime = userEntity.getLastFaceTime();
            if(StringUtils.isNotEmpty(lastFaceTime)) {
                Date lastFaceDate = DateUtils.convertStringToDate(lastFaceTime, DateUtils.DATE_TIME_PATTERN);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(lastFaceDate);
                calendar.add(Calendar.SECOND, frequencySecondTime);
                Date addSecondTime = calendar.getTime();
                map.put("addSecondTime", addSecondTime);
                Date nowDate = new Date();
                if (addSecondTime.getTime() > nowDate.getTime()) {
                    map.put("isCheck", false);
                    return map;
                } else {
                    //可调用身份验证范围内
                    map.put("isCheck", true);
                    return map;
                }
            }else{//可调用身份验证范围内
                map.put("isCheck", true);
                return map;
            }
        }
        return null;
    }
}
