package com.czg.controller.user;

import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.user.userinfo.UserInfoDTO;
import com.czg.account.dto.user.userinfo.UserInfoEditDTO;
import com.czg.account.dto.user.userinfo.UserInfoPwdEditDTO;
import com.czg.account.service.UserInfoService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息管理
 * @author Administrator
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户信息获取
     * @return 用户信息
     */
    @GetMapping
    public CzgResult<UserInfoDTO> get() {
        return CzgResult.success(userInfoService.getInfo(StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 用户信息修改
     * @return 用户信息
     */
    @PutMapping
    public CzgResult<Boolean> update(@RequestBody UserInfoEditDTO userInfoEditDTO) {
        return CzgResult.success(userInfoService.updateInfo(StpKit.USER.getShopId(), StpKit.USER.getLoginIdAsLong(), userInfoEditDTO));
    }

    /**
     * 用户密码修改
     * @return 是否成功
     */
    @PutMapping("/pwd")
    public CzgResult<Boolean> updatePwd(@RequestBody UserInfoPwdEditDTO userInfoPwdEditDTO) {
        if (StrUtil.isNotBlank(userInfoPwdEditDTO.getCheckPayPwd()) && !userInfoPwdEditDTO.getPayPwd().equals(userInfoPwdEditDTO.getCheckPayPwd())) {
            return CzgResult.failure("两次密码不一致");
        }
        return CzgResult.success(userInfoService.updatePwd(StpKit.USER.getLoginIdAsLong(), userInfoPwdEditDTO));
    }

    /**
     * 验证码获取
     * @return 是否获取成功
     */
    @GetMapping("/code")
    public CzgResult<Boolean> sendSmsCode() {
        return CzgResult.success(userInfoService.getCode(StpKit.USER.getLoginIdAsLong(), "wxMiniPwd"));
    }




}
