package com.fs.his.service.impl;

import java.util.Collections;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.course.param.DiagnosisConfirmParam;
import com.fs.course.param.FsFirstDiagnosisListUParam;
import com.fs.course.vo.FsFirstDiagnosisListUVO;
import com.fs.his.domain.FsDoctor;
import com.fs.his.param.FsDiagnosisFillDParam;
import com.fs.his.param.FsDiagnosisListDParam;
import com.fs.his.param.FsFirstDiagnosisParam;
import com.fs.his.service.IFsDoctorService;
import com.fs.his.vo.FsDiagnosisListDVO;
import com.fs.his.vo.FsFirstDiagnosisVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsFirstDiagnosisMapper;
import com.fs.his.domain.FsFirstDiagnosis;
import com.fs.his.service.IFsFirstDiagnosisService;
import org.springframework.util.CollectionUtils;

/**
 * 初诊单Service业务层处理
 * 
 * @author fs
 * @date 2025-09-01
 */
@Service
public class FsFirstDiagnosisServiceImpl extends ServiceImpl<FsFirstDiagnosisMapper, FsFirstDiagnosis> implements IFsFirstDiagnosisService {


    @Autowired
    private FsFirstDiagnosisMapper diagnosisMapper;

    @Autowired
    private IFsDoctorService doctorService;

    /**
     * 查询初诊单
     * 
     * @param id 初诊单主键
     * @return 初诊单
     */
    @Override
    public FsFirstDiagnosis selectFsFirstDiagnosisById(Long id)
    {
        return baseMapper.selectFsFirstDiagnosisById(id);
    }

    /**
     * 查询初诊单列表
     * 
     * @param fsFirstDiagnosis 初诊单
     * @return 初诊单
     */
    @Override
    public List<FsFirstDiagnosis> selectFsFirstDiagnosisList(FsFirstDiagnosis fsFirstDiagnosis)
    {
        return baseMapper.selectFsFirstDiagnosisList(fsFirstDiagnosis);
    }

    @Override
    public List<FsFirstDiagnosisVO> selectFsFirstDiagnosisVOList(FsFirstDiagnosisParam param) {
        return diagnosisMapper.selectFsFirstDiagnosisVOList(param);
    }

    /**
     * 新增初诊单
     * 
     * @param fsFirstDiagnosis 初诊单
     * @return 结果
     */
    @Override
    public int insertFsFirstDiagnosis(FsFirstDiagnosis fsFirstDiagnosis)
    {
        fsFirstDiagnosis.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsFirstDiagnosis(fsFirstDiagnosis);
    }

    /**
     * 修改初诊单
     * 
     * @param fsFirstDiagnosis 初诊单
     * @return 结果
     */
    @Override
    public int updateFsFirstDiagnosis(FsFirstDiagnosis fsFirstDiagnosis)
    {
        fsFirstDiagnosis.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsFirstDiagnosis(fsFirstDiagnosis);
    }

    /**
     * 批量删除初诊单
     * 
     * @param ids 需要删除的初诊单主键
     * @return 结果
     */
    @Override
    public int deleteFsFirstDiagnosisByIds(Long[] ids)
    {
        return baseMapper.deleteFsFirstDiagnosisByIds(ids);
    }

    /**
     * 删除初诊单信息
     * 
     * @param id 初诊单主键
     * @return 结果
     */
    @Override
    public int deleteFsFirstDiagnosisById(Long id)
    {
        return baseMapper.deleteFsFirstDiagnosisById(id);
    }

    @Override
    public FsFirstDiagnosis getByUserId(Long userId) {
        FsFirstDiagnosis firstDiagnosis = baseMapper.getByUserId(userId);
        if (firstDiagnosis == null) {
            firstDiagnosis = new FsFirstDiagnosis();
        }
        return firstDiagnosis;
    }

    @Override
    public List<FsDiagnosisListDVO> getDiagnosisList(FsDiagnosisListDParam param) {
        return diagnosisMapper.selectFsDiagnosisListDVO(param);
    }

    @Override
    public List<FsFirstDiagnosisListUVO> selectFsFirstDiagnosisListUVO(FsFirstDiagnosisListUParam param) {
        List<FsFirstDiagnosisListUVO> listUVOS = diagnosisMapper.selectFsFirstDiagnosisListUVO(param);
        if (!CollectionUtils.isEmpty(listUVOS)) {
            listUVOS.forEach(listUVO -> {
                if(listUVO.getDoctorStatus() != 1) {
                    listUVO.setSignUrl(null);
                }
            });
        }
        return listUVOS;
    }

    @Override
    public FsFirstDiagnosisListUVO getInfo(Long id) {
        FsFirstDiagnosisListUVO info = diagnosisMapper.getInfo(id);
        if (info != null && info.getDoctorStatus() != 1) {
            info.setSignUrl(null);
        }
        return info;
    }

    @Override
    public R confirm(DiagnosisConfirmParam param) {
        FsFirstDiagnosis diagnosis = diagnosisMapper.selectFsFirstDiagnosisById(param.getId());
        if (diagnosis == null) {
            return R.error("初诊单不存在");
        }
        if (diagnosis.getUserStatus() != 0) {
            return R.error("初诊单已确认");
        }
        if (diagnosis.getDoctorStatus() != 1) {
            return R.error("医生未签字无法确认");
        }
        FsFirstDiagnosis map = new FsFirstDiagnosis();
        map.setId(diagnosis.getId());
        map.setUserStatus(1);
        int i = diagnosisMapper.updateFsFirstDiagnosis(map);
        if (i > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R fill(FsDiagnosisFillDParam param) {
        if (param.getId() == null) {
            return R.error("初诊单id不能为空");
        }
        FsFirstDiagnosis diagnosis = diagnosisMapper.selectFsFirstDiagnosisById(param.getId());
        FsFirstDiagnosis map = new FsFirstDiagnosis();
        if (diagnosis == null) {
            return R.error("初诊单不存在");
        }
        if (diagnosis.getDoctorId() == null){
            FsDoctor doctor = doctorService.selectFsDoctorByDoctorId(param.getDoctorId());
            if (doctor == null) {
                return R.error("医生不存在");
            }
            map.setDoctorName(doctor.getDoctorName());
            map.setDoctorDep(doctor.getPosition());
            map.setDoctorCertificate(doctor.getCertificateCode());
            map.setDoctorId(doctor.getDoctorId());
        }
        map.setId(diagnosis.getId());
        map.setDoctorStatus(1);
        BeanUtils.copyProperties(param, map);
        int i = diagnosisMapper.updateFsFirstDiagnosis(map);
        if (i > 0) {
            return R.ok();
        }
        return R.error();
    }
}
