package com.openhis.web.doctorstation.appservice.impl;

import java.util.Arrays;
import java.util.HashSet;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.core.common.utils.MessageUtils;
import com.core.common.utils.SecurityUtils;
import com.openhis.clinical.domain.ReservationRecord;
import com.openhis.clinical.service.IReservationRecordService;
import com.openhis.common.constant.PromptMsgConstant;
import com.openhis.common.utils.HisQueryUtils;
import com.openhis.web.doctorstation.appservice.IReservationRecordAppService;
import com.openhis.web.doctorstation.dto.ReservationRecordDto;
import com.openhis.web.doctorstation.mapper.ReservationRecordAppMapper;

/**
 * 医生站-预约记录 应用实现类
 */
@Service
public class ReservationRecordAppServiceImpl implements IReservationRecordAppService {

    @Resource
    ReservationRecordAppMapper reservationRecordAppMapper;

    @Resource
    IReservationRecordService iReservationRecordService;

    /**
     * 保存预约记录
     *
     * @param reservationRecordDto 预约记录dto
     * @return 结果
     */
    @Override
    public R<?> saveReservation(ReservationRecordDto reservationRecordDto) {
        // 当前登录账号的科室id
        Long orgId = SecurityUtils.getLoginUser().getOrgId();
        // 参与者id
        Long practitionerId;
        if (reservationRecordDto.getPractitionerId() != null) {
            practitionerId = reservationRecordDto.getPractitionerId();
        } else {
            practitionerId = SecurityUtils.getLoginUser().getPractitionerId();
        }

        ReservationRecord reservationRecord = new ReservationRecord();

        reservationRecord.setPatientName(reservationRecordDto.getPatientName()); // 患者名称
        reservationRecord.setPatientTel(reservationRecordDto.getPatientTel()); // 患者联系方式
        reservationRecord.setChiefComplaint(reservationRecordDto.getChiefComplaint()); // 主诉
        reservationRecord.setReservationTime(reservationRecordDto.getReservationTime());// 就诊预约时间
        reservationRecord.setRemark(reservationRecordDto.getRemark()); // 备注
        reservationRecord.setToothPosition(reservationRecordDto.getToothPosition()); // 牙位
        reservationRecord.setOrgId(orgId); // 预约科室id
        reservationRecord.setPractitionerId(practitionerId); // 预约医生id
        iReservationRecordService.save(reservationRecord);

        return R.ok(null, MessageUtils.createMessage(PromptMsgConstant.Common.M00001, new Object[] {"预约记录"}));
    }

    /**
     * 编辑预约记录
     *
     * @param reservationRecordDto 预约记录dto
     * @return 结果
     */
    @Override
    public R<?> editReservation(ReservationRecordDto reservationRecordDto) {
        // 当前登录账号的科室id
        Long orgId = SecurityUtils.getLoginUser().getOrgId();
        // 参与者id
        Long practitionerId;
        if (reservationRecordDto.getPractitionerId() != null) {
            practitionerId = reservationRecordDto.getPractitionerId();
        } else {
            practitionerId = SecurityUtils.getLoginUser().getPractitionerId();
        }

        ReservationRecord reservationRecord = new ReservationRecord();

        reservationRecord.setId(reservationRecordDto.getId()); // id
        reservationRecord.setPatientName(reservationRecordDto.getPatientName()); // 患者名称
        reservationRecord.setPatientTel(reservationRecordDto.getPatientTel()); // 患者联系方式
        reservationRecord.setChiefComplaint(reservationRecordDto.getChiefComplaint()); // 主诉
        reservationRecord.setReservationTime(reservationRecordDto.getReservationTime());// 就诊预约时间
        reservationRecord.setRemark(reservationRecordDto.getRemark()); // 备注
        reservationRecord.setToothPosition(reservationRecordDto.getToothPosition()); // 牙位
        reservationRecord.setOrgId(orgId); // 预约科室id
        reservationRecord.setPractitionerId(practitionerId); // 预约医生id
        iReservationRecordService.updateById(reservationRecord);

        return R.ok(null, MessageUtils.createMessage(PromptMsgConstant.Common.M00002, new Object[] {"预约记录"}));
    }

    /**
     * 删除预约记录
     *
     * @param id id
     * @return 结果
     */
    @Override
    public R<?> delReservation(Long id) {
        iReservationRecordService.removeById(id);
        return R.ok(null, MessageUtils.createMessage(PromptMsgConstant.Common.M00005, new Object[] {"预约记录"}));
    }

    /**
     * 查询 预约记录
     *
     * @param reservationRecordDto 查询条件
     * @param searchKey 模糊查询关键字
     * @param pageNo 当前页
     * @param pageSize 每页多少条
     * @param request 请求
     * @return 预约记录
     */
    @Override
    public IPage<ReservationRecordDto> getReservationInfo(ReservationRecordDto reservationRecordDto, String searchKey,
        Integer pageNo, Integer pageSize, HttpServletRequest request) {

        // 构建查询条件
        QueryWrapper<ReservationRecordDto> queryWrapper = HisQueryUtils.buildQueryWrapper(reservationRecordDto,
            searchKey, new HashSet<>(Arrays.asList("patient_name", "chief_complaint", "patient_tel")), request);

        return reservationRecordAppMapper.getReservationInfo(new Page<>(pageNo, pageSize), queryWrapper);
    }

}
