package com.fs.doctor.controller;

import com.fs.common.annotation.Log;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.doctor.domain.DoctorLevelApply;
import com.fs.doctor.service.IDoctorLevelApplyService;
import com.fs.doctor.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲者定级申请Controller
 * 
 * @author fs
 * @date 2025-12-30
 */
@RestController
@RequestMapping("/doctor/apply")
public class DoctorLevelApplyController extends BaseController
{
    @Autowired
    private IDoctorLevelApplyService doctorLevelApplyService;

    /**
     * 查询讲者定级申请列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(DoctorLevelApply doctorLevelApply)
    {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        doctorLevelApply.setCompanyId(currentCompanyId);
        List<DoctorLevelApply> list = doctorLevelApplyService.selectDoctorLevelApplyList(doctorLevelApply);
        return getDataTable(list);
    }

    /**
     * 导出讲者定级申请列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:export')")
    @Log(title = "讲者定级申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DoctorLevelApply doctorLevelApply)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        doctorLevelApply.setCompanyId(currentCompanyId);
        List<DoctorLevelApply> list = doctorLevelApplyService.selectDoctorLevelApplyList(doctorLevelApply);
        ExcelUtil<DoctorLevelApply> util = new ExcelUtil<DoctorLevelApply>(DoctorLevelApply.class);
        return util.exportExcel(list, "讲者定级申请数据");
    }

    /**
     * 获取讲者定级申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(doctorLevelApplyService.selectDoctorLevelApplyById(id));
    }

    /**
     * 新增讲者定级申请
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:add')")
    @Log(title = "讲者定级申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoctorLevelApplyVO doctorLevelApplyVO)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        doctorLevelApplyVO.setCompanyId(currentCompanyId);
        return toAjax(doctorLevelApplyService.insertDoctorLevelApply(doctorLevelApplyVO));
    }

    /**
     * 修改讲者定级申请
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:edit')")
    @Log(title = "讲者定级申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoctorLevelApplyVO doctorLevelApplyVO)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        doctorLevelApplyVO.setCompanyId(currentCompanyId);
        return toAjax(doctorLevelApplyService.updateDoctorLevelApply(doctorLevelApplyVO));
    }

    /**
     * 讲者定级申请
     */
    @Log(title = "讲者定级申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(doctorLevelApplyService.deleteDoctorLevelApplyByIds(ids));
    }


    @Log(title = "讲者定级申请审核与查看页面信息")
    @GetMapping(value = "/getDoctorApplyDetailInfoById")
    public AjaxResult getDoctorApplyDetailInfoById(@RequestParam("id") Long id){

        return AjaxResult.success(doctorLevelApplyService.getDoctorApplyDetailInfoById(id));
    }
    @PreAuthorize("@ss.hasPermi('doctor:apply:audit')")
    @Log(title = "讲者定级申请审核", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/doAudit")
    public void doAudit(@RequestBody DoctorApplyAuditReqVO doctorApplyAuditReqVO){
        doctorLevelApplyService.doAudit(doctorApplyAuditReqVO);
    }



    /**
     * 1. 新增定级申请（混合提交）
     * 申请表 + 动态表单数据
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:add')")
    @Log(title = "讲者定级申请-动态", businessType = BusinessType.INSERT)
    @PostMapping("/dynamic/add")
    public AjaxResult addDynamicApply(@RequestBody DynamicApplyAddVO addVO) {
        // 1. 设置公司ID
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        addVO.getApply().setCompanyId(currentCompanyId);

        // 2. 调用Service
        Long applyId = doctorLevelApplyService.insertDynamicApply(addVO);
        return AjaxResult.success(applyId);
    }

    /**
     * 2. 修改定级申请（混合提交）
     */
    @PreAuthorize("@ss.hasPermi('doctor:apply:edit')")
    @Log(title = "讲者定级申请-动态", businessType = BusinessType.UPDATE)
    @PutMapping("/dynamic/update")
    public AjaxResult updateDynamicApply(@RequestBody DynamicApplyUpdateVO updateVO) {
        // 1. 设置公司ID
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        updateVO.getApply().setCompanyId(currentCompanyId);

        // 2. 调用Service
        doctorLevelApplyService.updateDynamicApply(updateVO);
        return AjaxResult.success();
    }


}
