package com.eitc.patient.controller;

import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.core.page.TableDataInfo;
import com.eitc.common.enums.BusinessType;
import com.eitc.patient.domain.PatientAppointment;
import com.eitc.patient.dto.PatientAppointmentDto;
import com.eitc.patient.enums.PatientAppointmentStatusEnum;
import com.eitc.patient.service.IPatientAppointmentService;
import com.eitc.patient.vo.PatientAppointmentParamVO;
import com.eitc.patient.vo.PatientAppointmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * PatientAppointmentController
 *
 * @author mtx
 * @date: 2024/05/17 15:52
 */
@Api(tags = "患者预约")
@RestController
@RequestMapping("/patientAppointment/patientAppointment-appointment")
public class PatientAppointmentController extends BaseController {

    @Resource
    private IPatientAppointmentService patientAppointmentService;

    @GetMapping("/page")
    @ApiOperation("分页查询患者预约")
    public TableDataInfo page(PatientAppointmentParamVO patientAppointment) {
        List<PatientAppointmentDto> list = patientAppointmentService.findList(patientAppointment);
        return getDataTable(list);
    }

    /**
     * 查询患者预约
     */
    @GetMapping("/list")
    @ApiOperation("查询患者预约")
    public AjaxResult list(PatientAppointmentParamVO patientAppointment) {
        return success(patientAppointmentService.findList(patientAppointment));
    }

    /**
     * 获取患者预约详细信息
     */
    @GetMapping(value = "/detail")
    @ApiOperation("获取患者预约详细信息")
    public AjaxResult detail(String id) {
        PatientAppointment appointment = patientAppointmentService.getById(id);
        return success(patientAppointmentService.getPatientAppointmentDto(appointment));
    }

    /**
     * 修改患者预约状态
     *
     * @param dto 患者预约数据传输对象，包含预约状态和要更新的预约ID
     * @return AjaxResult 返回操作结果，成功或失败
     */
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/modifyState")
    @ApiOperation("修改状态")
    public AjaxResult modifyState(@RequestBody PatientAppointmentDto dto) {
        // 检查传入的预约状态是否有效
        if (!PatientAppointmentStatusEnum.getCodes().contains(dto.getAppointmentStatus())) {
            return error("状态异常");
        }
        PatientAppointment entry = new PatientAppointment();
        entry.setId(dto.getAppointmentId()); // 设置预约ID
        entry.setAppointmentStatus(dto.getAppointmentStatus()); // 设置新的预约状态
        // 更新预约状态并返回操作结果
        return toAjax(patientAppointmentService.updateById(entry));
    }

    /**
     * 新增患者预约
     */
    @Log(title = "新增患者预约", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增患者预约")
    public AjaxResult add(@Valid @RequestBody PatientAppointmentVO entry) {
        patientAppointmentService.addOrEdit(entry);
        return success();
    }

    /**
     * 修改患者预约
     */
    @Log(title = "修改患者预约", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改患者预约")
    public AjaxResult edit(@Valid @RequestBody PatientAppointmentVO entry) {
        if (StringUtils.isEmpty(entry.getAppointmentId())) {
            return error("id不能为空");
        }
        patientAppointmentService.addOrEdit(entry);
        return success();
    }

    /**
     * 删除患者预约
     */
    @Log(title = "删除患者预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除患者预约")
    public AjaxResult remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return toAjax(patientAppointmentService.removeBatchByIds(idList));
    }

}
