package com.greenorange.promotion.controller.userInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.common.BaseResponse;
import com.greenorange.promotion.common.ResultUtils;
import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.model.dto.userAccount.UserAccountAddRequest;
import com.greenorange.promotion.model.dto.userAccount.UserAccountUpdateRequest;
import com.greenorange.promotion.model.entity.UserAccount;
import com.greenorange.promotion.model.vo.userAccount.UserAccountVO;
import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.settle.UserAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
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;


/**
 * 用户账户 控制器
 */
@RestController
@RequestMapping("userAccount")
@Slf4j
@Tag(name = "用户账户管理")
@Transactional
public class UserAccountController {

    @Resource
    private UserAccountService userAccountService;

    @Resource
    private CommonService commonService;

    /**
     * 小程序端用户添加用户账户
     * @param userAccountAddRequest 用户账户添加请求体
     * @return 是否添加成功
     */
    @PostMapping("add")
    @Operation(summary = "小程序端用户添加用户账户", description = "参数：用户账户添加请求体，权限：管理员，方法名：addUserAccount")
    @RequiresPermission(mustRole = UserConstant.DEFAULT_ROLE)
//    @SysLog(title = "用户账户管理", content = "小程序端用户添加用户账户")
    public BaseResponse<Boolean> addUserAccount(@Valid @RequestBody UserAccountAddRequest userAccountAddRequest, HttpServletRequest request) {
        userAccountService.addUserAccount(userAccountAddRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 小程序端用户根据id修改用户账户信息
     * @param userAccountUpdateRequest 用户账户更新请求体
     * @return 是否更新成功
     */
    @PostMapping("update")
    @Operation(summary = "小程序端用户根据id修改用户账户信息", description = "参数：用户账户更新请求体，权限：管理员，方法名：updateUserAccount")
    @RequiresPermission(mustRole = UserConstant.DEFAULT_ROLE)
//    @SysLog(title = "用户账户管理", content = "小程序端用户根据id修改用户账户信息")
    public BaseResponse<Boolean> updateUserAccount(@Valid @RequestBody UserAccountUpdateRequest userAccountUpdateRequest, HttpServletRequest request) {
        userAccountService.updateUserAccount(userAccountUpdateRequest, request);
        return ResultUtils.success(true);
    }


    /**
     * 小程序端用户根据id查询用户账户
     * @return 用户账户信息
     */
    @PostMapping("queryById")
    @Operation(summary = "小程序端根据id查询用户账户", description = "参数：无，权限：管理员，方法名：queryUserAccountByUserId")
    @RequiresPermission(mustRole = UserConstant.DEFAULT_ROLE)
//    @SysLog(title = "用户账户管理", content = "web端管理员根据id查询用户账户")
    public BaseResponse<UserAccountVO> queryUserAccountByUserId(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = commonService.buildQueryWrapperByField(UserAccount::getUserId, userId, userAccountService);
        UserAccount userAccount = userAccountService.getOne(lambdaQueryWrapper);
        UserAccountVO userAccountVO = commonService.copyProperties(userAccount, UserAccountVO.class);
        return ResultUtils.success(userAccountVO);
    }
}