保誠-保戶業務員媒合平台
wayne
2022-02-17 a3716f72066d25d745f4d5103ff23a553c3e102b
pamapi/src/main/java/com/pollex/pam/appointment/process/ClosedProcess.java
@@ -1,54 +1,65 @@
package com.pollex.pam.appointment.process;
import java.util.Optional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.pollex.pam.domain.Appointment;
import com.pollex.pam.domain.AppointmentClosedInfo;
import com.pollex.pam.enums.ContactStatusEnum;
import com.pollex.pam.repository.AppointmentClosedInfoRepository;
import com.pollex.pam.service.AppointmentClosedInfoService;
import com.pollex.pam.service.AppointmentService;
import com.pollex.pam.service.SatisfactionService;
import com.pollex.pam.service.dto.AbstractAppointmentProcessDTO;
import com.pollex.pam.service.dto.ClosedProcessDTO;
import com.pollex.pam.service.dto.DoneProcessDTO;
@Service
@Transactional
public class ClosedProcess implements AppointmentProcessInterface{
   @Autowired
   AppointmentClosedInfoRepository appointmentClosedInfoRepository;
   @Autowired
   AppointmentService appointmentService;
   @Autowired
   AppointmentClosedInfoService appointmentClosedInfoService;
   @Autowired
   SatisfactionService satisfactionService;
   @Override
   public void doProcess(AbstractAppointmentProcessDTO processDTO) {
      checkClosedInfo(processDTO.getAppointmentId());
      ClosedProcessDTO doneProcess = (ClosedProcessDTO)processDTO;
      BeanUtils.copyProperties(processDTO, doneProcess);
   public AppointmentClosedInfo create(AbstractAppointmentProcessDTO processDTO) {
      ClosedProcessDTO closeProcess = toClosedProcessDTO(processDTO);
      AppointmentClosedInfo closedInfo = new AppointmentClosedInfo();
      BeanUtils.copyProperties(doneProcess, closedInfo);
      appointmentClosedInfoRepository.save(closedInfo);
      BeanUtils.copyProperties(closeProcess, closedInfo);
      Appointment appointment = appointmentService.findById(processDTO.getAppointmentId());
      satisfactionService.createAppointmentSatisfaction(appointment);
      return appointmentClosedInfoRepository.save(closedInfo);
   }
   private void checkClosedInfo(Long appointmentId) {
      Optional<AppointmentClosedInfo> closedInfo = appointmentClosedInfoRepository.findByAppointmentId(appointmentId);
      if(closedInfo.isPresent()) {
         throw new IllegalArgumentException("appointment closed info exist");
      }
   private ClosedProcessDTO toClosedProcessDTO(AbstractAppointmentProcessDTO processDTO) {
      ClosedProcessDTO closeProcess = (ClosedProcessDTO)processDTO;
      BeanUtils.copyProperties(processDTO, closeProcess);
      return closeProcess;
   }
   @Override
   public ContactStatusEnum getProcessType() {
      return ContactStatusEnum.CLOSED;
   }
   @Override
   public AppointmentClosedInfo editClosedInfo(
         AbstractAppointmentProcessDTO abstractDTO
         , AppointmentClosedInfo closedInfo) {
      ClosedProcessDTO closeProcess =  toClosedProcessDTO(abstractDTO);
      BeanUtils.copyProperties(closeProcess, closedInfo);
      return appointmentClosedInfoRepository.save(closedInfo);
   }
}