package com.ruoyi.web.controller.client;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ShopUser;
import com.ruoyi.common.core.domain.entity.ShopUserResq;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.AliConfigService;
import com.ruoyi.system.service.ShopUserService;
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;

@RequestMapping("/client")
@RestController
public class ClientShopUserController {

    @Autowired
    private SysLoginService loginService;
    @Autowired
    private AliConfigService aliConfigService;
    @Autowired
    private ShopUserService shopUserService;


    /**
     * 获取验证码
     */
    @PostMapping("/getCode")
    public AjaxResult getCode(@RequestBody ShopUser shopUser){
        String result = aliConfigService.sendMsg(shopUser);
        if (result != null){
            return AjaxResult.success();
        }
        return AjaxResult.error("发送验证码失败");
    }

    /**
     * 登录方法
     *
     * @return 结果
     */
    @PostMapping("/shopLogin")
    public AjaxResult shopLogin(@RequestBody ShopUserResq userResq)
    {
        AjaxResult ajax = AjaxResult.success();
        ShopUser shopUser = shopUserService.login(userResq);
        if (shopUser == null || shopUser.getMsg() != null){
            return new AjaxResult(999,"登录失败");
        }else {
            // 生成令牌
            String token = loginService.shopUserLogin(shopUser.getUsername(), shopUser.getPassword());
            ajax.put("data", shopUser);
            ajax.put(Constants.TOKEN, token);
            return ajax;
        }
    }

    /**
     * 修改个人信息
     * @param shopUser 个人信息
     * @return 成功
     */
    @PostMapping("/updateUser")
    public AjaxResult modifyUserInfo(@RequestBody ShopUser shopUser){
        return AjaxResult.success(shopUserService.modifyUser(shopUser));
    }




}
