package com.cwhelp.admin.system.controller.api;

import com.cwhelp.common.constant.ApiConst;
import com.cwhelp.common.utils.ResultVoUtil;
import com.cwhelp.common.vo.ResultVo;
import com.cwhelp.component.actionLog.action.UserAction;
import com.cwhelp.component.actionLog.annotation.ActionLog;
import com.cwhelp.devtools.generate.utils.jAngel.utils.StringUtil;
import com.cwhelp.modules.business.domain.BssEmployee;
import com.cwhelp.modules.system.domain.Role;
import com.cwhelp.modules.system.domain.User;
import com.cwhelp.modules.system.service.ApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.UUID;

/**
 * Created by huangjc on 2019/12/1 0001.
 */
@RestController
@RequestMapping("/api")
public class ApiLoginController {

    @Autowired
    private ApiUserService apiUserService;

    @PostMapping("/me/login")
    @ResponseBody
    @ActionLog(key = UserAction.API_USER_LOGIN, action = UserAction.class)
    public ResultVo apiUserlogin(HttpServletRequest request, @RequestParam("telephone") String telephone, @RequestParam("password") String password){

        if(StringUtil.isBlank(telephone) || StringUtil.isBlank(password)){
            return new ResultVo(ApiConst.API_PARAM_BLANK_CODE, ApiConst.API_PARAM_BLANK_MSG);
        }

        User user = apiUserService.checkLoginPremission(telephone);
        if(user == null){
            return new ResultVo(ApiConst.API_LOGIN_PWD_ERR_CODE, ApiConst.API_LOGIN_PWD_ERR_MSG);
        }else {
            Iterator<Role> iterator = user.getRoles().iterator();
            boolean hasPremission = false;
            while(iterator.hasNext()){
                if (iterator.next().getName().contains(ApiConst.API_PREMISSION_USER_STR)) {
                    hasPremission = true;
                    break;
                }
            }
            if (hasPremission) {
                //暂定密码为 telephone后六位
                if(password.equals(telephone.substring(5, telephone.length()))){
                    HttpSession session = request.getSession();
                    String token = UUID.randomUUID().toString();
                    session.setAttribute(token,user);
                    return ResultVoUtil.success(ApiConst.API_LOGIN_SUCCESS_MSG, token);
                }else{
                    return new ResultVo(ApiConst.API_LOGIN_PWD_ERR_CODE, ApiConst.API_LOGIN_PWD_ERR_MSG);
                }
            } else {
                return new ResultVo(ApiConst.API_NOT_PREMISSION_CODE, ApiConst.API_NOT_PREMISSION_MSG);
            }
        }
    }

    @PostMapping("/biz/modifyPwd")
    @ResponseBody
//    @ActionLog(key = UserAction.API_USER_LOGIN, action = UserAction.class)
    public ResultVo modifyPwd(@RequestParam("telephone") String telephone,
                              @RequestParam("oldPassword") String oldPassword,
                              @RequestParam("newPassword") String newPassword){

        if(StringUtil.isBlank(telephone) || StringUtil.isBlank(oldPassword) || StringUtil.isBlank(newPassword)){
            return new ResultVo(ApiConst.API_PARAM_BLANK_CODE, ApiConst.API_PARAM_BLANK_MSG);
        }

        BssEmployee user = apiUserService.getUserByPhoneNum(telephone);
        if(user == null){
            return new ResultVo(ApiConst.API_SYS_ERR_CODE, ApiConst.API_SYS_ERR_MSG);
        }else {
//            user.setPassword(newPassword);
//            apiUserService.save(user);
            return new ResultVo(ApiConst.API_OPT_SUCCESS_CODE, ApiConst.API_OPT_SUCCESS_MSG);
        }
    }

    public static void main(String[] args) {
        String tel = "16621001775";

        System.out.println(tel.substring(5, tel.length()));
    }

}
