package com.ruoyi.web.bst;

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

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

/**
 * 收款账户Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/bst/account")
public class AccountController extends BaseController
{
    @Autowired
    private AccountService accountService;

    @Autowired 
    private AccountValidator accountValidator;
    
    /**
     * 查询收款账户列表
     */
    @PreAuthorize("@ss.hasPermi('bst:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountQuery query)
    {
        query.setScope(true);
        startPage();
        startOrderBy();
        List<AccountVO> list = accountService.selectAccountList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('bst:account:list')")
    @PostMapping("/listByIds")
    public AjaxResult listByIds(@RequestBody List<Long> ids) {
        List<AccountVO> list = accountService.selectAccountListByIds(ids, true);
        return success(list);
    }

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

    /**
     * 获取收款账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        AccountVO vo = accountService.selectAccountById(id, true);
        return success(vo);
    }

    /**
     * 新增收款账户
     */
    @PreAuthorize("@ss.hasPermi('bst:account:add')")
    @Log(title = "收款账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Account account)
    {
        // 非系统管理员，只能新增自己的收款账户
        if (!isSysAdmin() || account.getUserId() == null) {
            account.setUserId(getUserId());
        }
        return toAjax(accountService.insertAccount(account));
    }

    /**
     * 修改收款账户
     */
    @PreAuthorize("@ss.hasPermi('bst:account:edit')")
    @Log(title = "收款账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Account account)
    {
        if (!isSysAdmin()) {
            return error("您无权修改收款账户");
        }
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除收款账户
     */
    @PreAuthorize("@ss.hasPermi('bst:account:remove')")
    @Log(title = "收款账户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        if (!accountValidator.canDelAll(ids)) {
            return error("您无权删除ID为" + ids + "的收款账户");
        }
        return toAjax(accountService.logicDel(ids));
    }
}
