package com.xinelu.manage.service.hospitalpersoncertificate.impl;

import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.dto.hospitalpersoncertificate.CertificateUrlDTO;
import com.xinelu.manage.dto.hospitalpersoncertificate.HospitalPersonCertificateDTO;
import com.xinelu.manage.domain.hospitalpersoncertificate.HospitalPersonCertificate;
import com.xinelu.manage.mapper.hospitalpersoncertificate.HospitalPersonCertificateMapper;
import com.xinelu.manage.service.hospitalpersoncertificate.IHospitalPersonCertificateService;
import com.xinelu.manage.vo.hospitalpersoncertificate.HospitalPersonCertificateVO;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 健康咨询-科室人员资质证书信息Service业务层处理
 *
 * @author xinyilu
 * @date 2023-02-23
 */
@Service
public class HospitalPersonCertificateServiceImpl implements IHospitalPersonCertificateService {
    @Resource
    private HospitalPersonCertificateMapper hospitalPersonCertificateMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;

    /**
     * 查询健康咨询-科室人员资质证书信息
     *
     * @param id 健康咨询-科室人员资质证书信息主键
     * @return 健康咨询-科室人员资质证书信息
     */
    @Override
    public HospitalPersonCertificateVO selectHospitalPersonCertificateById(Long id) {
        HospitalPersonCertificateVO hospitalPersonCertificateVO = new HospitalPersonCertificateVO();
        List<HospitalPersonCertificateVO> hospitalPersonCertificateVOList = hospitalPersonCertificateMapper.selectHospitalPersonCertificateById(id);
        BeanUtils.copyProperties(hospitalPersonCertificateVOList.get(0), hospitalPersonCertificateVO);
        List<CertificateUrlDTO> certificateUrlDTO = new ArrayList<>();
        hospitalPersonCertificateVOList.forEach(hospital -> {
            CertificateUrlDTO certificateUrl = new CertificateUrlDTO();
            certificateUrl.setCertificateUrl(hospital.getCertificateUrl());
            certificateUrlDTO.add(certificateUrl);
            hospitalPersonCertificateVO.setCertificateUrlList(certificateUrlDTO);
        });
        return hospitalPersonCertificateVO;
    }

    /**
     * 查询健康咨询-科室人员资质证书信息列表
     *
     * @param hospitalPersonCertificate 健康咨询-科室人员资质证书信息
     * @return 健康咨询-科室人员资质证书信息
     */
    @Override
    public List<HospitalPersonCertificateVO> selectHospitalPersonCertificateList(HospitalPersonCertificateDTO hospitalPersonCertificate) {
        return hospitalPersonCertificateMapper.selectHospitalPersonCertificateList(hospitalPersonCertificate);
    }

    /**
     * 新增健康咨询-科室人员资质证书信息
     *
     * @param hospitalPersonCertificate 健康咨询-科室人员资质证书信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertHospitalPersonCertificate(HospitalPersonCertificateDTO hospitalPersonCertificate) {
        if (Objects.isNull(hospitalPersonCertificate)) {
            return AjaxResult.error("请添加科室人员资质证书信息！");
        }
        List<HospitalPersonCertificate> hospitalPersonCertificateList = new ArrayList<>();
        for (CertificateUrlDTO object : hospitalPersonCertificate.getCertificateUrlList()) {
            HospitalPersonCertificate hospitalPerson = new HospitalPersonCertificate();
            BeanUtils.copyProperties(hospitalPersonCertificate, hospitalPerson);
            hospitalPerson.setCertificateUrl(object.getCertificateUrl());
            hospitalPerson.setCreateTime(LocalDateTime.now());
            hospitalPerson.setCreateBy(SecurityUtils.getUsername());
            hospitalPerson.setCertificateCode(Constants.CERTIFICATE_CODE + generateSystemCodeUtil.generateSystemCode(Constants.CERTIFICATE_CODE));
            hospitalPersonCertificateList.add(hospitalPerson);
        }
        int hospitalPersonCertificates = hospitalPersonCertificateMapper.insertHospitalPersonCertificateList(hospitalPersonCertificateList);
        if (hospitalPersonCertificates <= 0) {
            throw new ServiceException("新增科室人员资质证书信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改健康咨询-科室人员资质证书信息
     *
     * @param hospitalPersonCertificate 健康咨询-科室人员资质证书信息
     * @return 结果
     */
    @Override
    public AjaxResult updateHospitalPersonCertificate(HospitalPersonCertificateDTO hospitalPersonCertificate) {
        int deleteHospitalPersonCertificateById = hospitalPersonCertificateMapper.deleteHospitalPersonCertificateById(hospitalPersonCertificate.getHospitalPersonId());
        if (deleteHospitalPersonCertificateById < 0) {
            return AjaxResult.error("修改信息失败，请联系管理员！");
        }
        List<HospitalPersonCertificate> hospitalPersonCertificateList = new ArrayList<>();
        for (CertificateUrlDTO object : hospitalPersonCertificate.getCertificateUrlList()) {
            HospitalPersonCertificate hospitalPerson = new HospitalPersonCertificate();
            BeanUtils.copyProperties(hospitalPersonCertificate, hospitalPerson);
            hospitalPerson.setCertificateUrl(object.getCertificateUrl());
            hospitalPerson.setCreateTime(LocalDateTime.now());
            hospitalPerson.setCreateBy(SecurityUtils.getUsername());
            hospitalPerson.setCertificateCode(Constants.CERTIFICATE_CODE + generateSystemCodeUtil.generateSystemCode(Constants.CERTIFICATE_CODE));
            hospitalPersonCertificateList.add(hospitalPerson);
        }
        int hospitalPersonCertificates = hospitalPersonCertificateMapper.insertHospitalPersonCertificateList(hospitalPersonCertificateList);
        if (hospitalPersonCertificates <= 0) {
            throw new ServiceException("新增科室人员资质证书信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除健康咨询-科室人员资质证书信息
     *
     * @param ids 需要删除的健康咨询-科室人员资质证书信息主键
     * @return 结果
     */
    @Override
    public int deleteHospitalPersonCertificateByIds(Long[] ids) {
        return hospitalPersonCertificateMapper.deleteHospitalPersonCertificateByIds(ids);
    }

}
