package com.xinelu.applet.service.healthconsultation.impl;

import com.xinelu.applet.mapper.healthconsultation.HealthConsultationMapper;
import com.xinelu.applet.service.healthconsultation.HealthConsultationService;
import com.xinelu.applet.utils.AppointmentTimeUtil;
import com.xinelu.applet.vo.healthconsultation.HealthConsultationOrderDTO;
import com.xinelu.applet.vo.healthconsultation.HealthConsultationVO;
import com.xinelu.applet.vo.healthconsultation.PatientAndTimeVO;
import com.xinelu.applet.vo.specialdisease.WeekDaysVO;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BuySourceEnum;
import com.xinelu.common.enums.GooodsOrderStatusEnum;
import com.xinelu.common.enums.OrderTypeEnum;
import com.xinelu.common.enums.PoserModuleTypeEnum;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.PageServiceUtil;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.goodsOrder.GoodsOrder;
import com.xinelu.manage.domain.goodsOrderDetails.GoodsOrderDetails;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.domain.poserInfo.PoserInfo;
import com.xinelu.manage.mapper.goodsOrder.GoodsOrderMapper;
import com.xinelu.manage.mapper.goodsOrderDetails.GoodsOrderDetailsMapper;
import com.xinelu.manage.mapper.hospitalpersoninfo.HospitalPersonInfoMapper;
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
import com.xinelu.manage.mapper.poserInfo.PoserInfoMapper;
import com.xinelu.manage.vo.patientinfo.PatientInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 健康咨询接口业务层实现类
 * @Author zh
 * @Date 2023-03-07
 */
@Slf4j
@Service
public class HealthConsultationServiceImpl implements HealthConsultationService {

    @Resource
    private PatientInfoMapper patientInfoMapper;
    @Resource
    private GoodsOrderMapper goodsOrderMapper;
    @Resource
    private GoodsOrderDetailsMapper goodsOrderDetailsMapper;
    @Resource
    private HealthConsultationMapper healthConsultationMapper;
    @Resource
    private PoserInfoMapper poserInfoMapper;
    @Resource
    private HospitalPersonInfoMapper hospitalPersonInfoMapper;
    @Resource
    private AppointmentTimeUtil appointmentTimeUtil;
    @Resource
    private PageServiceUtil pageServiceUtil;

    /**
     * 健康咨询-预约医生
     *
     * @return AjaxResult
     */
    @Override
    public TableDataInfo selectDepartment() {
        //查询海报及科室
        List<PoserInfo> poserList = poserInfoMapper.selectPoserListByModuleTyp(PoserModuleTypeEnum.HEALTH_CONSUTION_MODULE.getInfo());
        pageServiceUtil.startPage();
        List<HealthConsultationVO> healthConsultation = healthConsultationMapper.selectHospitalDepartmentList();
        if (CollectionUtils.isNotEmpty(healthConsultation)) {
            healthConsultation.get(0).setPoserInfoList(poserList);
        }
        return pageServiceUtil.getDataTable(healthConsultation);
    }

    /**
     * 健康咨询-预约医生(无分页)
     *
     * @return AjaxResult
     */
    @Override
    public List<HealthConsultationVO> selectDepartmentNoPage() {
        //查询海报及科室
        List<PoserInfo> poserList = poserInfoMapper.selectPoserListByModuleTyp(PoserModuleTypeEnum.HEALTH_CONSUTION_MODULE.getInfo());
        List<HealthConsultationVO> healthConsultation = healthConsultationMapper.selectHospitalDepartmentList();
        if (CollectionUtils.isNotEmpty(healthConsultation)) {
            healthConsultation.get(0).setPoserInfoList(poserList);
        }
        return healthConsultation;
    }

    /**
     * 单查科室人员
     *
     * @param hospitalPersonInfo 科室id
     * @return AjaxResult
     */
    @Override
    public List<HospitalPersonInfo> selectHospitalPerson(HospitalPersonInfo hospitalPersonInfo) {
        if (Objects.isNull(hospitalPersonInfo.getDepartmentId())) {
            return new ArrayList<>();
        }
        return hospitalPersonInfoMapper.selectHospitalPerson(hospitalPersonInfo);
    }

    /**
     * 健康咨询-信息确认
     *
     * @param patientId 用户信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult informationConfirmation(Long patientId) {
        PatientAndTimeVO patientAndTime = new PatientAndTimeVO();
        PatientInfo patientInfo = healthConsultationMapper.selectPatientById(patientId);
        if (Objects.isNull(patientInfo)) {
            return AjaxResult.error("用户信息不存在！");
        }
        LocalDate nowTime = LocalDate.now();
        //近七天的预约时间点数据集合
        List<WeekDaysVO> weekDaysList = Lists.newArrayList();
        appointmentTimeUtil.setWeekDayAndWeekDate(nowTime, weekDaysList);
        patientAndTime.setAppointmentTimeList(weekDaysList);
        patientAndTime.setPatientName(patientInfo.getPatientName());
        patientAndTime.setPhone(patientInfo.getPhone());
        return AjaxResult.success(patientAndTime);
    }

    /**
     * 手机App和微信小程序健康咨询确认订单方法
     *
     * @param healthConsultationOrderDTO 健康咨询订单
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertHealthConsultationOrder(HealthConsultationOrderDTO healthConsultationOrderDTO) {
        //判断当前会员信息是否存在
        PatientInfoVO patientInfo = patientInfoMapper.getPatientInfoById(healthConsultationOrderDTO.getPatientId());
        if (Objects.isNull(patientInfo)) {
            return AjaxResult.error("用户信息不存在，无法预约专家！");
        }
        // 根据id查询当前专家是否存在
        HospitalPersonInfo hospitalPersonInfo = hospitalPersonInfoMapper.selectHospitalPersonInfoById(healthConsultationOrderDTO.getHospitalPersonId());
        if (Objects.isNull(hospitalPersonInfo)) {
            return AjaxResult.error("当前专家信息不存在，请重新预约！");
        }
        //生成订单信息
        GoodsOrder goodsOrder = new GoodsOrder();
        BeanUtils.copyProperties(healthConsultationOrderDTO, goodsOrder);
        String goodOrderNo = StringUtils.fillZeroByPatientId(healthConsultationOrderDTO.getPatientId(), 5) + System.nanoTime();
        goodsOrder.setOrderNo(goodOrderNo);
        goodsOrder.setOrderStatus(GooodsOrderStatusEnum.WAIT_PAY.getInfo());
        goodsOrder.setDelFlag(0);
        goodsOrder.setOrderType(OrderTypeEnum.HEALTH_CONSULTATION.getInfo());
        goodsOrder.setBuySource(BuySourceEnum.HEALTH_CONSULTATION.getInfo());
        goodsOrder.setCreateTime(LocalDateTime.now());
        goodsOrder.setOrderTime(LocalDateTime.now());
        goodsOrder.setOriginalTotalPrice(healthConsultationOrderDTO.getTotalPrice());
	    goodsOrder.setHospitalPersonId(hospitalPersonInfo.getId());
	    goodsOrder.setHospitalPersonName(hospitalPersonInfo.getPersonName());
		goodsOrder.setHealthConsultationContent(healthConsultationOrderDTO.getHealthConsultationContent());
	    goodsOrder.setHealthAppointDate(healthConsultationOrderDTO.getHealthAppointDate());
	    goodsOrder.setConsultationInfoId(healthConsultationOrderDTO.getConsultationInfoId());

        int insertGoodsOrder = goodsOrderMapper.insertGoodsOrder(goodsOrder);
        if (insertGoodsOrder <= 0) {
            throw new ServiceException("预约订单新增信息失败，请联系管理员！");
        }
        GoodsOrderDetails goodsOrderDetails = new GoodsOrderDetails();
        goodsOrderDetails.setGoodsOrderId(goodsOrder.getId());
        goodsOrderDetails.setOrderNo(goodOrderNo);
        goodsOrderDetails.setGoodsName("健康咨询");
        goodsOrderDetails.setTotalPrice(healthConsultationOrderDTO.getTotalPrice());
        goodsOrderDetails.setDelFlag(0);
        goodsOrderDetails.setCreateTime(LocalDateTime.now());
        int insertGoodsOrderDetails = goodsOrderDetailsMapper.insertGoodsOrderDetails(goodsOrderDetails);
        if (insertGoodsOrderDetails <= 0) {
            throw new ServiceException("预约订单明细新增信息失败，请联系管理员！");
        }
        return AjaxResult.success(goodsOrderMapper.getGoodsOrderByOrderNo(goodOrderNo));
    }
}
