package com.kmall.api.service;

import com.kmall.api.dao.ApiSysConfigMapper;
import com.kmall.api.dao.ApiThirdMerchantBizMapper;
import com.kmall.api.dao.ApiUserLevelMapper;
import com.kmall.api.dao.ApiUserMapper;
import com.kmall.api.entity.SmsLogVo;
import com.kmall.api.entity.ThirdMerchantBizVo;
import com.kmall.api.entity.UserLevelVo;
import com.kmall.api.entity.UserVo;
import com.kmall.common.utils.DateUtils;
import com.kmall.common.utils.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class ApiUserService {
    @Autowired
    private ApiUserMapper userDao;
    @Autowired
    private ApiUserLevelMapper userLevelDao;
    @Autowired
    private ApiSysConfigMapper apiSysConfigMapper;
    @Autowired
    private ApiThirdMerchantBizMapper apiThirdMerchantBizMapper;

    public UserVo queryObject(Long userId) {
        return userDao.queryObject(userId);
    }

    public UserVo queryByOpenId(String openId) {
        return userDao.queryByOpenId(openId);
    }

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

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

    public void save(String mobile, String password) {
        UserVo user = new UserVo();
        user.setUser_level_id(1);
        user.setMobile(mobile);
        user.setUsername(mobile);
        user.setPassword(DigestUtils.sha256Hex(password));
        user.setRegister_time(new Date());
        userDao.save(user);
    }

    public void save(UserVo userVo) {
        userDao.save(userVo);
    }

    public void update(UserVo user) {
        userDao.update(user);
    }

    public void delete(Long userId) {
        userDao.delete(userId);
    }

    public void deleteBatch(Long[] userIds) {
        userDao.deleteBatch(userIds);
    }

    public List<UserVo> queryByMobile(String mobile) {
        return userDao.queryByMobile(mobile);
    }

//    public long login(String mobile, String password) {
//        UserVo user = queryByMobile(mobile);
//        Assert.isNull(user, "手机号或密码错误");
//
//        //密码错误
//        if (!user.getPassword().equals(DigestUtils.sha256Hex(password))) {
//            throw new RRException("手机号或密码错误");
//        }
//
//        return user.getId();
//    }

    public SmsLogVo querySmsCodeByUserId(Long user_id) {
        return userDao.querySmsCodeByUserId(user_id);
    }


//    public int saveSmsCodeLog(SmsLogVo smsLogVo) {
//        return userDao.saveSmsCodeLog(smsLogVo);
//    }

    public String getUserLevel(UserVo loginUser) {
        String result = "普通用户";
        UserLevelVo userLevelVo = userLevelDao.queryObject(loginUser.getUser_level_id());
        if (null != userLevelVo) {
            result = userLevelVo.getName();
        }
        return result;
    }

    public UserVo queryObjectByIdNoAndName(String idNo,String userName){
        return userDao.queryObjectByIdNoAndName(idNo, userName);
    }

    /**
     * 根据用户id判断上次活体人脸核身时间是否在可调用范围内（待小程序开放人脸识别类目）
     * @param userId
     * @return
     */
    public Boolean checkFaceTimeByUserId(Long userId, Long storeId){
        ThirdMerchantBizVo thirdMerchantBizVo = apiThirdMerchantBizMapper.queryDataByStoreId(storeId);
        if(thirdMerchantBizVo.getIsFaceCheck().equalsIgnoreCase("1")) {
            String value = apiSysConfigMapper.queryByKey("FACE_COLLECT_FREQUENCY_TIME");
            UserVo userVo = userDao.queryObject(userId);
            if (userVo != null && StringUtils.isNotEmpty(value)) {
                Integer frequencySecondTime = Integer.parseInt(value);
                String lastFaceTime = userVo.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();
                    Date nowDate = new Date();
                    if (addSecondTime.getTime() > nowDate.getTime()) {
                        return false;
                    } else {
                        //可调用身份验证范围内
                        return true;
                    }
                } else {//可调用身份验证范围内
                    return true;
                }
            }
        }else{
            return false;//第三方商户是否核验身份为0否，则不调起身份核验操作直接支付
        }
        return null;
    }
}
