Skip to content
On this page

Service

  • @Service 서비스에는 을 붙여서 서비스 역할을 하는 컴포넌트로 등록한다.
  • @Transactional 저장하는 메소드는 트랜잭션 처리를 위해 붙여준다.
  • 저장 처리할때 그리드데이터는 CollectionView 클래스를 활용 할 수 있다.

Example

package com.vmerp.sample.form_grid;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vmerp.business.common.autoseq.AutoseqService;
import com.vmerp.business.common.file.FileService;
import com.vmerp.business.common.model.CmmFile;
import com.vmerp.business.common.model.SaveCounter;
import com.vmerp.business.common.model.UploadParam;
import com.vmerp.common.collection.CollectionView;
import com.vmerp.common.exception.BusinessException;
import com.vmerp.sample.model.FormGrid;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class FormGridService {

	@Autowired
	FormGridMapper mapper;

	@Autowired
	AutoseqService autoseqService;
	@Autowired
	FileService fileService;

	/**
	 * 목록을 조회한다.
	 * @param param
	 * @return
	 */
	public List<FormGrid> query(FormGridParam param) {
		return mapper.selectList(param);
	}

	/**
	 * 저장한다.
	 * @param param
	 * @return
	 */
	@Transactional
	public int save(FormGridParam param) {
		int resultCount = 0;
		log.debug("save(FormGridSave param) => {}", param);

//		FormGridParam qParam = FormGridParam.builder().idPk(1).build();
//		FormGrid formGridUpdater = mapper.selectModel(qParam);
//
//		log.debug("selected formGridUpdater = {}", formGridUpdater);
//		
//		formGridUpdater.setCharText("Service에서 FormGrid 조회해서 값 수정하고 update");
//		// 문자열로 날짜 지정
//		formGridUpdater.setDateDate(Date.valueOf("2021-04-01"));
//
//		// 날짜 연산 calendar 이용
//		Calendar cal = Calendar.getInstance();
//		cal.set(2021, 4, 2); 
//		Date dateDate1 = new Date(cal.getTimeInMillis());
//		cal.add(Calendar.DAY_OF_MONTH, 10);
//		Date dateDate2 = new Date(cal.getTimeInMillis());
//
//		formGridUpdater.setDateDate1(dateDate1);
//		formGridUpdater.setDateDate2(dateDate2);
//		formGridUpdater.setDateTimestamp(dateTimestamp);
//		formGridUpdater.setDateTimestamptz(dateTimestamptz);

		CollectionView<FormGrid> formGrids = param.getFlex();
		if (formGrids != null) {
			List<FormGrid> items = formGrids.items();
			List<FormGrid> itemsAdded = formGrids.itemsAdded();
			List<FormGrid> itemsEdited = formGrids.itemsEdited();
			List<FormGrid> itemsRemoved = formGrids.itemsRemoved();

			log.debug("save(FormGridSave param) 내용");
			log.debug("FormGrids = {}", formGrids);
			log.debug("CollectionView items = {}", items);
			log.debug("CollectionView itemsAdded = {}", itemsAdded);
			log.debug("CollectionView itemsEdited = {}", itemsEdited);
			log.debug("CollectionView itemsRemoved = {}", itemsRemoved);

			// for and tr
			for(int i = 0 ; i < items.size() ; i++ ) {
				FormGrid s = items.get(i);
				switch(s.getTr()) {
				case A:
					mapper.insert(s);
					break;
				case E:
					mapper.update(s);
					break;
				case R:
					mapper.delete(s);
					break;
				}
			}

			// Collection
			itemsAdded.forEach(mapper::insert);
			resultCount += itemsAdded.size();

			itemsEdited.forEach(mapper::update);
			resultCount += itemsEdited.size();

			itemsRemoved.forEach(mapper::delete);
			resultCount += itemsRemoved.size();
		}

		return resultCount;
	}

	public List<FormGrid> queryUpload(FormGridParam param) {
		return mapper.selectUploadList(param);
	}
	public List<CmmFile> queryUploadFiles(FormGridParam param) {
		return mapper.selectFormGridCmmFileList(param);
	}
	
	/**
	 * 첨부파일 있는 저장
	 * @param param 저장할 정보
	 * @param uploadParam 업로드하는 파일에 대한 부가정보를 공통처리메소드에 전달하는 클래스
	 * @return
	 */
	@Transactional
	public int saveUpload(FormGridParam param, UploadParam uploadParam) {
		// forEach 사용시 int 대신 SaveCounter 사용
		SaveCounter sc = new SaveCounter();
		
		//1) upload request에서 jsonData 부분의 데이터
		CollectionView<FormGrid> formGrids = param.getFlex();
		if (formGrids != null) {
			List<FormGrid> itemsAdded = formGrids.itemsAdded();
			List<FormGrid> itemsEdited = formGrids.itemsEdited();
			List<FormGrid> itemsRemoved = formGrids.itemsRemoved();

			itemsRemoved.forEach(formGrid -> {
				
				mapper.delete(formGrid);
				// 직접 파일삭제 한게 아니고, 관련 데이터 삭제하면 삭제할 파일데이터로 추가해준다.
				fileService.addRemoveSubFileList(uploadParam, "SAMPLE", formGrid.getCorpCd(), formGrid.getIdPk());
			});
			itemsAdded.forEach(formGrid -> {
				String idPk = autoseqService.getAutoseq("999");
				formGrid.setIdPk(idPk);
				sc.count += mapper.insert(formGrid);
				//[첨부파일처리 2]첨부파일 저장( relKey1~ )에 업무 PK를 채움. 첨부파일 테이블에 저장은 마지막에.
				fileService.cmmFile2Key(uploadParam, formGrid.getTempFileKey(), idPk);
			});
			itemsEdited.forEach(formGrid -> {
				if ( formGrid.getIdPk() == null) throw BusinessException.create("idPk가 없어서 수정 할 수 없습니다.");
				fileService.addRemoveSubFileList(uploadParam, "SAMPLE", formGrid.getCorpCd(), formGrid.getIdPk());
				sc.count += mapper.update(formGrid);
			});
		}
		
		//[첨부파일처리 3] 첨부파일 테이블에 파일정보 insert, delete 저장.
		fileService.cmmFile3Save(uploadParam);

		return sc.count;
	}
}

Hello