package com.ruoyi.web.controller.ss;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ValidGroup;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.account.domain.Account;
import com.ruoyi.ss.account.domain.AccountVO;
import com.ruoyi.ss.account.domain.AccountQuery;
import com.ruoyi.ss.account.service.AccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户账户Controller
 *
 * @author ruoyi
 * @date 2024-07-27
 */
@RestController
@RequestMapping("/ss/account")
public class AccountController extends BaseController
{
    @Autowired
    private AccountService smAccountService;

    /**
     * 查询用户账户列表
     */
    @PreAuthorize("@ss.hasPermi('ss:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountQuery query)
    {
        startPage();
        startOrderBy();
        List<AccountVO> list = smAccountService.selectSmAccountList(query);
        return getDataTable(list);
    }

    /**
     * 导出用户账户列表
     */
    @PreAuthorize("@ss.hasPermi('ss:account:export')")
    @Log(title = "用户账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountQuery query)
    {
        List<AccountVO> list = smAccountService.selectSmAccountList(query);
        ExcelUtil<AccountVO> util = new ExcelUtil<AccountVO>(AccountVO.class);
        util.exportExcel(response, list, "用户账户数据");
    }

    /**
     * 获取用户账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('ss:account:query')")
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId)
    {
        return success(smAccountService.selectSmAccountByAccountId(accountId));
    }

    /**
     * 新增用户账户
     */
    @PreAuthorize("@ss.hasPermi('ss:account:add')")
    @Log(title = "用户账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Account account)
    {
        return toAjax(smAccountService.insertSmAccount(account));
    }

    /**
     * 修改用户账户
     */
    @PreAuthorize("@ss.hasPermi('ss:account:edit')")
    @Log(title = "用户账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody  @Validated(ValidGroup.Update.class) Account account)
    {
        return toAjax(smAccountService.updateSmAccount(account));
    }

    /**
     * 删除用户账户
     */
    @PreAuthorize("@ss.hasPermi('ss:account:remove')")
    @Log(title = "用户账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds) {
        return toAjax(smAccountService.deleteSmAccountByAccountIds(accountIds));
    }

    /**
     * 根据类型获取用户账户
     * @param userId 用户ID
     * @param type 类型
     */
    @GetMapping("/getUserAccountByType")
    public AjaxResult getUserAccountByType(@RequestParam Long userId, @RequestParam String type) {
        return success(smAccountService.selectUserAccountByType(userId, type));
    }
}
