package com.gyt.shr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyt.shr.entity.CodeRecord;
import com.gyt.shr.entity.Person;
import com.gyt.shr.mapper.CodeRecordMapper;
import com.gyt.shr.service.CodeRecordService;
import com.gyt.shr.util.CharUtil;
import com.gyt.shr.util.GlobalUtil;
import com.gyt.shr.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.gyt.shr.constant.GlobalConstant.NORMAL_USER_LOGIN_STATUS;


/**
* @author coyle
 * 验证码
*/
@Service
@Slf4j
public class CodeRecordServiceImpl extends ServiceImpl<CodeRecordMapper, CodeRecord>  implements CodeRecordService {

    @Autowired
    private CodeRecordMapper codeRecordMapper;
    @Autowired
    private CharUtil charUtil;



    //
    /**
     * 查询是否有未过期的验证码
     */
    @Override
    public Result<Boolean> checkHasCode(String phone, String code,int offset) {
        QueryWrapper<CodeRecord> codeRecordQueryWrapper = new QueryWrapper<>();
        codeRecordQueryWrapper.eq("fnumber", phone);
//        codeRecordQueryWrapper.eq("cfisvalid", true);
        codeRecordQueryWrapper.eq("cfcode", code);
        Date dateWithOffset = getDate(offset);
        codeRecordQueryWrapper.gt("cfexpiresTime", dateWithOffset);
        List<CodeRecord> codeRecords = codeRecordMapper.selectList(codeRecordQueryWrapper);
        if (codeRecords.size() > 0) {
            return Result.success(true);
        }
        return Result.error(-1, "未找到有效的验证码");
    }


    /**
     * 根据身份证号码查询是否有未过期的验证码
     * @param idNo
     * @param code
     * @param offset
     * @return
     */
    @Override
    public Result<List<CodeRecord>> checkIdNo( HttpServletRequest request,String idNo, String code,int offset) {
        QueryWrapper<CodeRecord> codeRecordQueryWrapper = new QueryWrapper<>();
        codeRecordQueryWrapper.eq("cfidCardNo", idNo);
//        codeRecordQueryWrapper.eq("cfisvalid", true);
        codeRecordQueryWrapper.eq("cfcode", code);
        Date dateWithOffset = getDate(offset);
        codeRecordQueryWrapper.gt("cfexpiresTime", dateWithOffset);
        List<CodeRecord> codeRecords = codeRecordMapper.selectList(codeRecordQueryWrapper);
        if (codeRecords.size() > 0) {
            //对用户信息进行脱敏处理：密码不返回。
            Person safetyUser = GlobalUtil.getSafetyUser(codeRecords.get(0));
            // 7. 记录用户的登录态。
            request.getSession().setAttribute(NORMAL_USER_LOGIN_STATUS, safetyUser);
            // 8. 登录成功，返回用户ID
            return Result.success(codeRecords);
        }
        return Result.error(-1, "身份证号码错误或验证码已失效,请重新输入或重新获取验证码");
    }

    private   Date getDate(int offset) {
        // 获取当前日期并加上offset分钟
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, offset);
        Date dateWithOffset = calendar.getTime();
        return dateWithOffset;
    }
}




