package com.eitc.patient.controller;

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.core.domain.entity.SysUser;
import com.eitc.common.enums.BusinessType;
import com.eitc.patient.domain.SchedulingConfiguration;
import com.eitc.patient.service.ISchedulingConfigurationService;
import com.eitc.patient.vo.SchedulingConfigurationVO;
import com.eitc.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * SchedulingConfigurationController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "排班配置")
@RestController
@RequestMapping("/scheduling-configuration")
public class SchedulingConfigurationController extends BaseController {

    @Resource
    private ISchedulingConfigurationService schedulingConfigurationService;
    @Resource
    private ISysUserService sysUserService;

    /**
     * 查询排班
     */
    @GetMapping("/list")
    @ApiOperation("查询排班配置")
    public AjaxResult list() throws ParseException {
        List<SchedulingConfigurationVO> resultList = new ArrayList<>();
        List<SysUser> users = sysUserService.selectUserList(new SysUser());
        List<SchedulingConfiguration> list = schedulingConfigurationService.list();
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        }
        Map<String, List<SchedulingConfiguration>> collect = list.stream().collect(Collectors.groupingBy(SchedulingConfiguration::getDoctorId));
        for (SysUser user : users) {
            String doctorId = String.valueOf(user.getUserId());
            SchedulingConfigurationVO vo = new SchedulingConfigurationVO();
            List<SchedulingConfiguration> configurationList = collect.get(doctorId);

            if (CollectionUtils.isNotEmpty(configurationList)) {
                vo.setMondayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.MONDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
                vo.setTuesdayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.TUESDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
                vo.setWednesdayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.WEDNESDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
                vo.setThursdayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.THURSDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
                vo.setFridayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.FRIDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
                vo.setSaturdayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.SATURDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
                vo.setSundayStatus(configurationList.stream().filter(item -> Objects.equals(item.getWeekNum(), Calendar.SUNDAY)).findFirst().orElse(new SchedulingConfiguration()).getWorkStatus());
            }

            vo.setDoctorId(doctorId);
            vo.setDoctorName(user.getNickName());
            resultList.add(vo);
        }

        return success(resultList);
    }

    /**
     * 新增排班配置
     * 通过接收一个包含排班信息的列表，为医生新增一周内的排班配置。
     * 每个排班配置项对应医生在一周内某一天的工作状态。
     *
     * @param list 包含待新增排班配置信息的列表，每个元素是一个SchedulingConfigurationVO对象。
     * @return 返回操作结果，成功则包含新增的排班配置数量。
     */
    @Log(title = "新增排班配置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增排班配置")
    public AjaxResult add(@ApiParam(name = "list") @Valid @RequestBody List<SchedulingConfigurationVO> list) {
        // 初始化用于存储待保存的排班配置对象的列表
        SchedulingConfiguration schedulingConfiguration;
        List<SchedulingConfiguration> schedulingConfigurationList = new ArrayList<>();
        // 初始化用于存储所有医生ID的列表，用于后续删除旧的排班配置
        List<String> idList = new ArrayList<>();

        // 遍历输入的排班配置VO对象列表
        for (SchedulingConfigurationVO schedulingConfigurationVO : list) {
            String doctorId = schedulingConfigurationVO.getDoctorId();
            idList.add(doctorId);

            // 根据每一天的工作状态，创建并添加相应的排班配置对象到待保存的列表中
            if (Objects.nonNull(schedulingConfigurationVO.getMondayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.MONDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getMondayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
            if (Objects.nonNull(schedulingConfigurationVO.getTuesdayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.TUESDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getTuesdayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
            if (Objects.nonNull(schedulingConfigurationVO.getWednesdayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.WEDNESDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getWednesdayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
            if (Objects.nonNull(schedulingConfigurationVO.getThursdayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.THURSDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getThursdayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
            if (Objects.nonNull(schedulingConfigurationVO.getFridayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.FRIDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getFridayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
            if (Objects.nonNull(schedulingConfigurationVO.getSaturdayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.SATURDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getSaturdayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
            if (Objects.nonNull(schedulingConfigurationVO.getSundayStatus())) {
                schedulingConfiguration = new SchedulingConfiguration();
                schedulingConfiguration.setDoctorId(doctorId);
                schedulingConfiguration.setWeekNum(Calendar.SUNDAY);
                schedulingConfiguration.setWorkStatus(schedulingConfigurationVO.getSundayStatus());
                schedulingConfigurationList.add(schedulingConfiguration);
            }
        }

        // 删除医生之前的排班配置，以更新排班信息
        schedulingConfigurationService.remove(Wrappers.<SchedulingConfiguration>lambdaQuery().in(SchedulingConfiguration::getDoctorId, idList));
        // 保存新的排班配置
        return success(schedulingConfigurationService.saveBatch(schedulingConfigurationList));
    }


}
