package com.ruoyi.os.service.impl;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.os.domain.HealthcareOsConventionalDrug;
import com.ruoyi.os.domain.HealthcareOsConventionalPrescriptions;
import com.ruoyi.os.mapper.HealthcareOsConventionalDrugMapper;
import com.ruoyi.os.mapper.HealthcareOsConventionalPrescriptionsMapper;
import com.ruoyi.os.service.IConventionalPrescriptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *  常规处方Service业务层处理
 *
 * @author 何顺
 * @date 2024-09-24
 */
@Service
public class ConventionalPrescriptionsServiceImpl implements IConventionalPrescriptionsService {


    @Autowired
    private HealthcareOsConventionalPrescriptionsMapper healthcareOsConventionalPrescriptionsMapper;

    @Autowired
    private HealthcareOsConventionalDrugMapper healthcareOsConventionalDrugMapper;


    /**
     *  获取 常规 处方 的 列表
     *  @param healthcareOsConventionalPrescriptions 主诉主键
     *  @return 列表
     */
    @Override
    public List<HealthcareOsConventionalPrescriptions> gainConventionalPrescriptionsList(HealthcareOsConventionalPrescriptions healthcareOsConventionalPrescriptions)
    {
        return healthcareOsConventionalPrescriptionsMapper.selectHealthcareOsConventionalPrescriptionsList(healthcareOsConventionalPrescriptions);
    }

    /**
     *  获取 常规 处方 的 详情
     *  @param id id
     *  @return HealthcareOsConventionalPrescriptions
     */
    public HealthcareOsConventionalPrescriptions gainConventionalPrescriptions(Long id)
    {
        // 获取 常规 处方 基础
        HealthcareOsConventionalPrescriptions conventionalPrescriptions = healthcareOsConventionalPrescriptionsMapper.selectHealthcareOsConventionalPrescriptionsById(id);
        // 获取 常规 处方 的 药物
        HealthcareOsConventionalDrug conventionalDrug = new HealthcareOsConventionalDrug();
        conventionalDrug.setConventionalPrescriptionId(id);
        List<HealthcareOsConventionalDrug> conventionalDrugsList = healthcareOsConventionalDrugMapper.selectHealthcareOsConventionalDrugList(conventionalDrug);
        //
        conventionalPrescriptions.setDrugUseList(conventionalDrugsList);
        //
        return conventionalPrescriptions;
    }

    /**
     *   添加 常规 处方 的 详情
     *   @param healthcareOsConventionalPrescriptions id
     *   @return 结果
     */
    public int addConventionalPrescriptions(HealthcareOsConventionalPrescriptions healthcareOsConventionalPrescriptions)
    {
        // 添加  时间
        Date addTime = DateUtils.getNowDate();
        // 获取用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //
        healthcareOsConventionalPrescriptions.setCreateBy(loginUser.getUsername());
        healthcareOsConventionalPrescriptions.setCreateTime(addTime);
        if(healthcareOsConventionalPrescriptionsMapper.insertHealthcareOsConventionalPrescriptions(healthcareOsConventionalPrescriptions) == 1) {
            //
            List<HealthcareOsConventionalPrescriptions> conventionalPrescriptionsList = healthcareOsConventionalPrescriptionsMapper.selectHealthcareOsConventionalPrescriptionsList(healthcareOsConventionalPrescriptions);
            if(conventionalPrescriptionsList.size() == 1) {
                // 获取 添加 的 处方 的 id
                Long conventionalPrescriptionsId = conventionalPrescriptionsList.get(0).getId();
                //
                if(healthcareOsConventionalPrescriptions.getDrugUseList() != null) {
                    healthcareOsConventionalPrescriptions.getDrugUseList().forEach(model-> {
                        // id
                        model.setConventionalPrescriptionId(conventionalPrescriptionsId);
                        //
                        model.setCreateBy(loginUser.getUsername());
                        model.setCreateTime(addTime);
                        healthcareOsConventionalDrugMapper.insertHealthcareOsConventionalDrug(model);
                    });
                }
            }
        }
        return 1;
    }

    /**
     *  修改 常规 处方 的 详情
     *  @param healthcareOsConventionalPrescriptions id
     *  @return 结果
     */
    public int updateConventionalPrescriptions(HealthcareOsConventionalPrescriptions healthcareOsConventionalPrescriptions)
    {
        // 更新  时间
        Date updateTime = DateUtils.getNowDate();
        // 获取用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 处方 id
        Long conventionalPrescriptionsId = healthcareOsConventionalPrescriptions.getId();

        // 更新  处方 基本
        healthcareOsConventionalPrescriptionsMapper.updateHealthcareOsConventionalPrescriptions(healthcareOsConventionalPrescriptions);

        // 获取 常规 药品
        HealthcareOsConventionalDrug conventionalDrug = new HealthcareOsConventionalDrug();
        conventionalDrug.setConventionalPrescriptionId(conventionalPrescriptionsId);
        List<HealthcareOsConventionalDrug> conventionalDrugList = healthcareOsConventionalDrugMapper.selectHealthcareOsConventionalDrugList(conventionalDrug);
        // 更新  处方 的 药物
        //  先 进行 删除 数据
        if(conventionalDrugList.size() > 0 ) {
            // 进行 删除
            conventionalDrugList.forEach(model-> {
                healthcareOsConventionalDrugMapper.deleteHealthcareOsConventionalDrugById(model.getId());
            });
        }
        // 在 添加  药物
        // 判断 是否 是 空值
        if(healthcareOsConventionalPrescriptions.getDrugUseList().size() > 0) {
            healthcareOsConventionalPrescriptions.getDrugUseList().forEach(model -> {
                // 处方 id
                model.setConventionalPrescriptionId(conventionalPrescriptionsId);
                // 添加
                model.setCreateBy(loginUser.getUsername());
                model.setCreateTime(updateTime);
                healthcareOsConventionalDrugMapper.insertHealthcareOsConventionalDrug(model);
            });
        }
        return 1;
    }

    /**
     *  添加 常规 处方 的 详情
     *  @param id id
     *  @return 结果
     */
    public int deleteConventionalPrescriptions(Long id)
    {
        //
        healthcareOsConventionalPrescriptionsMapper.deleteHealthcareOsConventionalPrescriptionsById(id);
        //
        HealthcareOsConventionalDrug conventionalDrug = new HealthcareOsConventionalDrug();
        conventionalDrug.setConventionalPrescriptionId(id);
        List<HealthcareOsConventionalDrug> conventionalDrugList = healthcareOsConventionalDrugMapper.selectHealthcareOsConventionalDrugList(conventionalDrug);
        //
        if(conventionalDrugList.size() > 0) {
            conventionalDrugList.forEach(model -> {
                //
                healthcareOsConventionalDrugMapper.deleteHealthcareOsConventionalDrugById(model.getId());
            });
        }
        return 1;
    }

}
