package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.enums.BusinessType;
import com.eitc.common.utils.bean.BeanUtils;
import com.eitc.patient.domain.SchedulingStatus;
import com.eitc.patient.service.ISchedulingStatusService;
import com.eitc.patient.vo.SchedulingStatusVO;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * SchedulingStatusController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "排班状态")
@RestController
@RequestMapping("/scheduling-status")
public class SchedulingStatusController extends BaseController {

    @Resource
    private ISchedulingStatusService schedulingStatusService;

    @GetMapping("/list")
    @ApiOperation("查询排班状态")
    public AjaxResult list() {
        LambdaQueryWrapper<SchedulingStatus> eq = Wrappers.<SchedulingStatus>lambdaQuery().orderByAsc(SchedulingStatus::getOrderNum);
        List<SchedulingStatus> list = schedulingStatusService.list(eq);
        if (CollectionUtils.isEmpty(list)) {
            return success(new ArrayList<>());
        }
        List<SchedulingStatusVO> collect = list.stream().map(this::getSchedulingStatusVO).collect(Collectors.toList());
        return success(collect);
    }

    /**
     * 获取排班状态信息
     */
    @GetMapping(value = "/detail")
    @ApiOperation("获取排班状态信息")
    public AjaxResult detail(String id) {
        SchedulingStatusVO vo = getSchedulingStatusVO(schedulingStatusService.getById(id));
        return success(vo);
    }

    /**
     * 根据调度状态对象，创建并返回一个调度状态的VO对象。
     * 该方法主要用于将业务对象SchedulingStatus的属性复制到VO对象SchedulingStatusVO中，
     * 以便于在前端展示或进行其他业务操作。
     *
     * @param schedulingStatus 调度状态对象，包含调度相关的状态信息。
     * @return 返回一个SchedulingStatusVO对象，包含从SchedulingStatus复制的属性值。
     *         如果输入的schedulingStatus为null，将返回一个空的SchedulingStatusVO对象。
     */
    public SchedulingStatusVO getSchedulingStatusVO(SchedulingStatus schedulingStatus) {
        // 创建一个空的SchedulingStatusVO对象
        SchedulingStatusVO vo = new SchedulingStatusVO();

        // 如果输入的调度状态对象为空，则直接返回空的VO对象
        if(Objects.isNull(schedulingStatus)){
            return vo;
        }

        // 使用BeanUtils工具类将调度状态对象的属性值复制到VO对象中
        BeanUtils.copyProperties(schedulingStatus, vo);

        // 根据调度状态对象的startTime属性是否为空，设置VO对象的workStatus属性值
        // 如果startTime不为空，则workStatus设为1，表示工作状态启用；否则设为0，表示未启动
        vo.setWorkStatus(Objects.nonNull(schedulingStatus.getStartTime()) ? 1 : 0);

        // 返回填充完毕的VO对象
        return vo;
    }

    /**
     * 新增排班状态
     */
    @Log(title = "新增排班状态", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增排班状态")
    public AjaxResult add(@Valid @RequestBody SchedulingStatusVO schedulingStatus) {
        if (0 == schedulingStatus.getWorkStatus()) {
            schedulingStatus.setStartTime(null);
            schedulingStatus.setEndTime(null);
        } else if (1 == schedulingStatus.getWorkStatus()) {
            if (Objects.isNull(schedulingStatus.getStartTime()) || Objects.isNull(schedulingStatus.getEndTime())) {
                return error("开始时间或结束时间不能为空");
            }
        } else {
            return error("状态异常");
        }
        return toAjax(schedulingStatusService.save(schedulingStatus));
    }

    /**
     * 修改排班状态
     */
    @Log(title = "修改排班状态", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改排班状态")
    public AjaxResult edit(@Valid @RequestBody SchedulingStatusVO schedulingStatus) {
        if (StringUtils.isEmpty(schedulingStatus.getId())) {
            return error("id不能为空");
        }
        if (0 == schedulingStatus.getWorkStatus()) {
            schedulingStatus.setStartTime(null);
            schedulingStatus.setEndTime(null);
        } else if (1 == schedulingStatus.getWorkStatus()) {
            if (Objects.isNull(schedulingStatus.getStartTime()) || Objects.isNull(schedulingStatus.getEndTime())) {
                return error("开始时间或结束时间不能为空");
            }
        } else {
            return error("状态异常");
        }
        return toAjax(schedulingStatusService.updateById(schedulingStatus));
    }

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

}
