package org.dromara.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.web.domain.bo.PatientAccountBo;
import org.dromara.web.domain.vo.PatientAccountVo;
import org.dromara.web.service.IPatientAccountService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 患者账户信息
 *
 * @author Lion Li
 * @date 2025-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/settlement/accountSettleAccounts")
public class PatientAccountController extends BaseController {

    private final IPatientAccountService patientAccountService;

    /**
     * 查询患者账户信息列表
     */
    @SaCheckPermission("settlement:accountSettleAccounts:list")
    @GetMapping("/list")
    public TableDataInfo<PatientAccountVo> list(PatientAccountBo bo, PageQuery pageQuery) {
        return patientAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出患者账户信息列表
     */
    @SaCheckPermission("settlement:accountSettleAccounts:export")
    @Log(title = "患者账户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatientAccountBo bo, HttpServletResponse response) {
        List<PatientAccountVo> list = patientAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "患者账户信息", PatientAccountVo.class, response);
    }

    /**
     * 获取患者账户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("settlement:accountSettleAccounts:query")
    @GetMapping("/{id}")
    public R<PatientAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(patientAccountService.queryById(id));
    }

    /**
     * 新增患者账户信息
     */
    @SaCheckPermission("settlement:accountSettleAccounts:add")
    @Log(title = "患者账户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatientAccountBo bo) {
        return toAjax(patientAccountService.insertByBo(bo));
    }

    /**
     * 修改患者账户信息
     */
    @SaCheckPermission("settlement:accountSettleAccounts:edit")
    @Log(title = "患者账户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatientAccountBo bo) {
        return toAjax(patientAccountService.updateByBo(bo));
    }

    /**
     * 删除患者账户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("settlement:accountSettleAccounts:remove")
    @Log(title = "患者账户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(patientAccountService.deleteWithValidByIds(List.of(ids), true));
    }
}
