package com.younike.webApi;


import com.younike.admin.service.SysLoginService;
import com.younike.admin.service.SysPasswordService;
import com.younike.common.annotation.OpLog;
import com.younike.common.constant.BusinessType;
import com.younike.common.constant.OperatorType;
import com.younike.common.core.dto.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.younike.common.core.dto.AjaxResult.success;

/**
 * (信息发布类网站的)注册与登录接口
 */
@RestController
@RequestMapping("/webLogin")
public class WebLoginController {

    @Autowired
    private SysPasswordService sysPasswordService;
    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("/login")
    @OpLog(type = BusinessType.LOGIN, operatorType = OperatorType.MANAGE, isSaveRequestData = false)
    public AjaxResult ajaxLogin(HttpServletRequest request, @RequestBody Map<String, String> payload) {
        request.setAttribute("captcha", payload.get("code"));
        request.setAttribute("uuid", payload.get("uuid"));
        UsernamePasswordToken token = new UsernamePasswordToken(payload.get("username"), payload.get("password"));
        Subject subject = SecurityUtils.getSubject();
//        subject.login(token);
//        List<ZxUser> list = zxUserService.lambdaQuery().eq(ZxUser::getUserName, payload.get("username")).eq(ZxUser::getPassword, payload.get("password"))
//                .list();
//        if (CollectionUtils.isEmpty(list)) {
//            return AjaxResult.error("用户名或密码错误");
//        }
        return success();
    }

    /**
     * 注册用户
     *
     * @param user 注册用户
     */
//    @OpLog(type = BusinessType.INSERT, title = "注册用户")
//    @PostMapping
//    public APIResult addUser(@RequestBody ZxUserVo user) {
//        if (!user.getPassword().equals(user.getConfirmPassword())) {
//            return ResultGenerator.getFailResult("密码与确认密码不一致,请重新输入");
//        }
//        // 对象赋值
//        ZxUser zxUser = new ZxUser();
//        zxUser.setLoginName(user.getLoginName());
//        zxUser.setUserName(user.getLoginName());
//        zxUser.setPhonenumber(user.getPhonenumber());
//        zxUser.setEmail(user.getEmail());
//        //检查唯一性
//        if (!zxUserService.checkPhoneUnique(zxUser).equals(UserConstants.USER_PHONE_UNIQUE)) {
//            return ResultGenerator.getFailResult("该手机号码已存在");
//        }
//        if (!zxUserService.checkEmailUnique(zxUser).equals(UserConstants.USER_EMAIL_UNIQUE)) {
//            return ResultGenerator.getFailResult("该邮箱已存在");
//        }
//        if (!zxUserService.checkLoginNameUnique(zxUser.getLoginName()).equals(UserConstants.LOGIN_NAME_UNIQUE)) {
//            return ResultGenerator.getFailResult("该用户名称已存在");
//        }
//
//        zxUser.setSalt(ShiroUtils.randomSalt());
//        zxUser.setPassword(user.getPassword());
//        zxUser.setCreateBy(user.getLoginName());
//        zxUser.setCreateTime(new Date());
//        // 新增注册用户
//        zxUserService.insertZxUser(zxUser);
//        return ResultGenerator.getSuccessResult();
//    }

}
