//package com.rrtx.services.api;
//
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.util.RandomUtil;
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.rrtx.fap.core.entity.R;
//import com.rrtx.fap.starter.genid.service.IdService;
//import com.rrtx.services.common.annotation.Log;
//import com.rrtx.services.common.constant.ContextParam;
//import com.rrtx.services.common.constant.HostConstant;
//import com.rrtx.services.common.constant.PbConstant;
//import com.rrtx.services.common.controller.BaseController;
//import com.rrtx.services.common.entity.User;
//import com.rrtx.services.common.exception.BusinessCodeException;
//import com.rrtx.services.common.exception.BusinessException;
//import com.rrtx.services.common.http.HostSendMsg;
//import com.rrtx.services.common.utils.CommonUtil;
//import com.rrtx.services.common.utils.DateUtil;
//import com.rrtx.services.common.utils.MD5Util;
//import com.rrtx.services.common.utils.TranslateUtil;
//import com.rrtx.services.db.entity.PbCstAttendance;
//import com.rrtx.services.db.entity.PbCstInf;
//import com.rrtx.services.db.entity.PbLogonControl;
//import com.rrtx.services.db.entity.PbStudyNotes;
//import com.rrtx.services.db.service.IPbCstAttendanceService;
//import com.rrtx.services.db.service.IPbCstInfService;
//import com.rrtx.services.db.service.IPbLogonControlService;
//import com.rrtx.services.service.IPublicService;
//import com.rrtx.services.service.IRedisService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.json.JSONObject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.validation.constraints.NotBlank;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * 用户基本信息校验表
// */
//@Slf4j
//@RestController
//@RequestMapping("API")
//public class UserController extends BaseController {
//
//    @Autowired
//    IPbCstInfService cstInfService; // 用户基本信息表
//    @Autowired
//    IPbLogonControlService logonControlService; // 登录控制表
//    @Autowired
//    IRedisService redisService;
//    @Autowired
//    IPublicService publicService; // 公共服务
//
//    @Autowired
//    private IdService idService; // 获取序列号
//    @Autowired
//    IPbCstAttendanceService pbCstAttendanceService;
//
//    @Log("1.2.5 找回密码")
//    @RequestMapping("modifyPwd")
//    public R modifyPwd(@NotBlank(message = "{required}") String smSuccCode , // 校验代码
//                         @NotBlank(message = "{required}") String pwd  , // 密码
//                         @NotBlank(message = "{required}") String cfPwd, // 确认密码
//                         @NotBlank(message = "{required}") String currentId, // 当前用户ID
//                         @NotBlank(message = "{required}") String sendCode, // 验证码标识
//                         HttpServletRequest request
//            ) throws BusinessException, BusinessCodeException {
//        // 输入条件判断
//        if(CommonUtil.isEmpty(smSuccCode)
//                || CommonUtil.isEmpty(pwd)
//                || CommonUtil.isEmpty(cfPwd)) {
//            // 【参数不全】
//            throw new BusinessCodeException("PBSE9000");
//        }
//        // 验证验证码
//        //        setTempVal(user.getCstno() +"_"+rand+ "_sendCode", code);
//        String msgCheckKey = currentId +"_"+sendCode+ "_sendCode";
//        System.out.println("msgCheckKey:"+msgCheckKey);
//        String sessionSendCode = getTempVal(msgCheckKey);;
//        System.out.println("getCode"+sessionSendCode);
////        if(CommonUtil.isNotEmpty(currentId)) {
////            sessionSendCode = getTempVal(currentId +"_"+sendCode+ "_sendCode");
////        } else {
////            sessionSendCode = getSessionVal("smSuccCode");
////        }
//        if(CommonUtil.isEmpty(sessionSendCode)
//                || !sessionSendCode.equals(smSuccCode)) {
//            // 【数据异常】
//            throw new BusinessCodeException("PBSE9001");//TODO
//        }else{//清除缓存数据
//            removeTempVal(msgCheckKey);
//        }
//
//        if(!pwd.equals(cfPwd)) {
//            // 两次输入的密码不一致
//            throw new BusinessCodeException("PBSE0006");
//        }
//
//        // 更新用户密码
//        String userId = currentId;
//        PbCstInf user = cstInfService.getById(userId);
//        String password = MD5Util.encrypt(user.getCstno(), pwd);
//        user.setPassword(password);
//        user.setModifyTime(new DateTime());
//        user.setCstno(user.getCstno());
//        cstInfService.updateById(user);
//        // 更新登录控制表
//        PbLogonControl logonControl = new PbLogonControl();
//        logonControl.setCstId(userId);
//        logonControl.setErrCount("0");
//        logonControl.setLspwTime(DateUtil.getCurrentTime());
////        logonControl.setDevId(ContextParam.DEV_NO.get());
//
//        LambdaQueryWrapper<PbLogonControl> lgQuerey = new LambdaQueryWrapper<PbLogonControl>();
//        lgQuerey.eq(PbLogonControl::getCstId, user.getCstno());
//        lgQuerey.eq(PbLogonControl::getDevId, ContextParam.DEV_NO.get());
//
//        if(!logonControlService.update(logonControl, lgQuerey)) {
//            logonControlService.insertSelective(logonControl);
//        }
//
//        insertLog(currentId, TranslateUtil.val("修改密码"), "00000000");
//
//        return R.ok();
//    }
//
//
//    @Log("1.2.3 发送短信验证码")
//    @RequestMapping("sendSmsMsg")
//    public R sendSmsMsg(@NotBlank(message = "{required}") String mobileNo, // 手机号
//                           @NotBlank(message = "{required}") String type,// 1：首次登录 只验证用户名 2：忘记密码，可输入别名
//                           HttpServletRequest request
//        ) throws BusinessException, BusinessCodeException {
//        // 输入条件判断
//        if(CommonUtil.isEmpty(mobileNo)
//                ||  CommonUtil.isEmpty(type) ) {
//            // 【参数不全】
//            throw new BusinessCodeException("PBSE9000");
//        }
//
//        // 查询条件
//        LambdaQueryWrapper<PbCstInf> search = new LambdaQueryWrapper<PbCstInf>();
//        search.eq(PbCstInf::getMobile, mobileNo);// 手机号码
//
//
//        // 根据条件查询
//        List<PbCstInf> isExit = cstInfService.list(search);
//        if (isExit == null || isExit.size() == 0|| isExit.size() > 1) {
//            throw new BusinessCodeException("PBSE0007");//用户信息不存在，或者手机号存在多条异常数据
//        }
//        PbCstInf user = isExit.get(0);
//
//        String rand = RandomUtil.randomString(32);
//        //发送短信TODO 未处理
//        String transType ="";//业务类型
//        String code  = publicService.sendMsg(mobileNo,transType);
//        String key = user.getCstno() +"_"+rand+ "_sendCode";
//        System.out.println("key:"+key);
//        setTempVal(key, code);
//        System.out.println("getKey:"+getTempVal(key));
//        Map<String, String> retMap = new HashMap<String, String>();
//        retMap.put("sendCode", rand);// 校验代码
//        retMap.put("currentId", user.getCstno());// 校验代码
//
//        return R.ok().data(retMap);
//    }
//
//    @Log("1.2.2 校验用户信息")
//    @RequestMapping("isExitUserInf")
//    public R isExitUserInf(@NotBlank(message = "{required}") String mobileNo, // 用户名或别名
//                   @NotBlank(message = "{required}") String type // 1：首次登录 只验证用户名 2：忘记密码，可输入别名
//                ) throws BusinessCodeException {
//
//
////        // 2：忘记密码，可输入别名需要输入别名
//////        if(CommonUtil.isEmpty(useName)
//////                && "2".equals(type) ) {
//////            // 【参数不全】
//////            throw new BusinessCodeException("JPUB001");
//////        }
//
//        // 查询条件
//        LambdaQueryWrapper<PbCstInf> search = new LambdaQueryWrapper<PbCstInf>();
//        search.eq(PbCstInf::getMobile, mobileNo);// 电子银行ID
//        // search.isNull(PbCstInf::getPassword);
//        // 根据条件查询
//        List<PbCstInf> isExit = cstInfService.list(search);
//        if (isExit == null || isExit.size() == 0) {
//            throw new BusinessCodeException("PBSE0007");//用户不存在
//        }
//        // 2：忘记密码
//        if("2".equals(type)
//                && CommonUtil.isEmpty(isExit.get(0).getPassword())) {
//            // 请走首次登录流程
//            throw new BusinessCodeException("");//TODO
//        }
//        //首次登陆 找回密码
//        if("1".equals(type)
//                && CommonUtil.isNotEmpty(isExit.get(0).getPassword())) {
//            // 从未登陆过，
//            throw new BusinessCodeException("");//TODO
//        }
//
//        Map<String, Object> retMap = new HashMap<String, Object>();
//        retMap.put("mobileNo",  CommonUtil.maskMobile(isExit.get(0).getMobile()));
//        return R.ok().data(retMap);
//
//    }
////
////    @Log("1.1.6 设定密码（/setPwd）")
////    @RequestMapping("setPwd")
////    public R setPwd(@NotBlank(message = "{required}") String smSuccCode , // 校验代码
////                       @NotBlank(message = "{required}") String pwd  , // 密码
////                       @NotBlank(message = "{required}") String currentId// 当前用户ID
////        ) throws BusinessCodeException {
////        // 输入条件判断
////        if(CommonUtil.isEmpty(smSuccCode)
////                || CommonUtil.isEmpty(pwd)
////                || CommonUtil.isEmpty(currentId)) {
////            // 【参数不全】
////            throw new BusinessCodeException("PBSE9000");
////        }
////        // 获取Session数据
////        String sessionSendCode = getTempVal(currentId + "smSuccCode");
////        if(CommonUtil.isEmpty(sessionSendCode)
////                || !sessionSendCode.equals(smSuccCode)) {
////            // 【数据异常】
////            throw new BusinessCodeException("");//TODO
////        }
////
////        removeTempVal(currentId + "smSuccCode");
////
////        // 更新用户密码
////        String userId = currentId;
////        PbCstInf user = cstInfService.getById(userId);
////        String password = MD5Util.encrypt(user.getCstno(), pwd);
////        user.setPassword(password);
//////        user.setModifyUser(user.getId());
////        user.setModifyTime(new DateTime());
////        cstInfService.updateById(user);
////
////        // 更新登录控制表
////        PbLogonControl logonControl = new PbLogonControl();
////        logonControl.setCstId(userId);
////        logonControl.setErrCount("0");
////        logonControl.setLspwTime(DateUtil.getCurrentTime());
////        logonControl.setDevId("0000000000");
////        LambdaQueryWrapper<PbLogonControl> lgQuerey = new LambdaQueryWrapper<PbLogonControl>();
////        lgQuerey.eq(PbLogonControl::getCstId, user.getCstno());
////        lgQuerey.eq(PbLogonControl::getDevId, ContextParam.DEV_NO.get());
////
////        if(!logonControlService.update(logonControl, lgQuerey)) {
////            logonControlService.insertSelective(logonControl);
////        }
////        insertLog(currentId, TranslateUtil.val("设定密码"), "00000000");
////        return R.ok();
////    }
//    @Log("1.2.1 签到（/signIn）")
//    @RequestMapping("signIn")
//    public R signIn()
//            throws BusinessException, BusinessCodeException {
//        User user = getCurrentUser(PbConstant.TEST_MODE);
//        //查询当天是否已签到
//        LambdaQueryWrapper<PbCstAttendance> query = new LambdaQueryWrapper<>();
//        query.eq(PbCstAttendance::getCstno,user.getCstno());
//        query.eq(PbCstAttendance::getDate,DateUtil.getCurrentDateFormt());
//
//        List<PbCstAttendance> list =  pbCstAttendanceService.list(query);
//        if(list.size() > 0){
//            throw new BusinessCodeException("PBSE0600");//用户已签到，请勿重复操作
//        }
//        PbCstAttendance signIn = new PbCstAttendance();
//        signIn.setFlowno(idService.genId()+"");
//        signIn.setCstno(user.getCstno());
//        signIn.setDate(DateUtil.getCurrentDateFormt());
//        signIn.setCreateTime(new DateTime());
//        pbCstAttendanceService.insertSelective(signIn);
//        return R.ok();
//    }
//
//    @Log("1.2.1 我的签到记录（/signInRecord）")
//    @RequestMapping("signInRecord")
//    public R signInRecord(String month,String date)//年月份 格式yyyy-mm-dd
//            throws BusinessException, BusinessCodeException {
//        User user = getCurrentUser(PbConstant.TEST_MODE);
//        //查询当天是否已签到
//        LambdaQueryWrapper<PbCstAttendance> query = new LambdaQueryWrapper<>();
//        query.eq(PbCstAttendance::getCstno,user.getCstno());
//        query.like(PbCstAttendance::getDate,month);
//        if(CommonUtil.isNotEmpty(date)){
//            query.eq(PbCstAttendance::getDate,date);
//        }
//        List<PbCstAttendance> list =  pbCstAttendanceService.list(query);
//        return R.ok(list);
//    }
//}
