앞 게시글 Axboot 멀티 테이블 수정/저장 예제 (1/2) 에서 연속으로 진행하도록 하겠습니다.
이번 시간에는 4)제품 History 조회와 제품 정보의 판매가 수정 시 제품 History 테이블에 Insert하는 JAVA로직을
처리하도록 하겠습니다.
4) 제품 History 조회와 제품 정보의 판매가 수정 시 제품 History 테이블에 Insert
- PrdtPriceHist.java 수정
package com.dasdes.shopmng.domain.prdt; import com.chequer.axboot.core.annotations.ColumnPosition; import lombok.*; import org.apache.ibatis.type.Alias; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.Type; import com.chequer.axboot.core.annotations.Comment; import com.dasdes.shopmng.domain.BaseJpaModel; import com.dasdes.shopmng.domain.program.Program; import java.sql.Timestamp; import java.util.Date; import javax.persistence.*; import javax.validation.constraints.NotNull; @Setter @Getter @DynamicInsert @DynamicUpdate @Entity @EqualsAndHashCode(callSuper = true) @IdClass(PrdtPriceHist.class) @Table(name = "prdt_price_hist") @Comment(value = "제품가격History") @Alias("prdtPriceHist") public class PrdtPriceHist extends BaseJpaModel<String> { @Id @Column(name = "id", length = 20, nullable = false) @Comment(value = "id") private String id;
@Column(name = "prdt_cd", length = 20, nullable = false) @Comment(value = "제품코드") private String prdtCd; @Column(name = "ch_date", nullable = false) @Comment(value = "변경일시") @Temporal(TemporalType.TIMESTAMP) private Date chDate; @Column(name = "sales_price", precision = 10, nullable = false) @Comment(value = "판매가격") private Integer salesPrice;
@Override public String getId() { return id; } } |
- PrdtPriceHistRepository.java
package com.dasdes.shopmng.domain.prdt; import com.chequer.axboot.core.domain.base.AXBootJPAQueryDSLRepository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface PrdtPriceHistRepository extends AXBootJPAQueryDSLRepository<PrdtPriceHist, String> { } |
- PrdtPriceHistService.java
package com.dasdes.shopmng.domain.prdt; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.dasdes.shopmng.domain.BaseService; import com.dasdes.shopmng.exception.DBException; import com.querydsl.core.BooleanBuilder; import javax.inject.Inject; import com.chequer.axboot.core.parameter.RequestParams; import java.util.ArrayList; import java.util.List; @Service public class PrdtPriceHistService extends BaseService<PrdtPriceHist, String> { private PrdtPriceHistRepository prdtPriceHistRepository; @Inject public PrdtPriceHistService(PrdtPriceHistRepository prdtPriceHistRepository) { super(prdtPriceHistRepository); this.prdtPriceHistRepository = prdtPriceHistRepository; } //제품 판매가 History조회 public List<PrdtPriceHist> gets(RequestParams<PrdtPriceHist> requestParams) { String prdtCd=requestParams.getString("prdtCd", ""); String filter = requestParams.getString("filter"); BooleanBuilder builder = new BooleanBuilder(); if (isNotEmpty(prdtCd)) { builder.and(qProduct.prdtCd.eq(prdtCd)); } List<PrdtPriceHist> prdtpriceHistList = select().from(qPrdtPriceHist).where(builder).orderBy(qPrdtPriceHist.prdtCd.asc()).fetch(); if (isNotEmpty(filter)) { prdtpriceHistList = filter(prdtpriceHistList, filter); } return prdtpriceHistList; }
//제품 정보에서 호출하는 판매가 insert method @Transactional public void savePrdPrice(List<Product> product) { PrdtPriceHist prdtHist=null; List<PrdtPriceHist> hlist=new ArrayList(); Product prdt=null;
for(int i=0;i<product.size();i++) { prdt=(Product)product.get(i);
if(prdt.is__modified__()) { prdtHist=new PrdtPriceHist(); prdtHist.setPrdtCd(prdt.getPrdtCd()); prdtHist.setSalesPrice(prdt.getSalesPrice()); prdtHist.setCreatedBy(prdt.getCreatedBy()); hlist.add(prdtHist); } }
save(hlist); } }
|
- ProductService.java : 제품 정보에서 판매가를 수정할 때 제품 History 테이블에 insert되는 method 호출
package com.dasdes.shopmng.domain.prdt; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; //추가 import com.querydsl.core.BooleanBuilder; //추가 import javax.inject.Inject; import com.chequer.axboot.core.parameter.RequestParams; import com.dasdes.shopmng.domain.BaseService; import com.dasdes.shopmng.exception.DBException; import java.util.List; //import org.slf4j.Logger; //import org.slf4j.LoggerFactory; @Service public class ProductService extends BaseService<Product, String> { private ProductRepository productRepository;
@Inject private PrdtPriceHistService prdtPriceHistService; @Inject public ProductService(ProductRepository productRepository) { super(productRepository); this.productRepository = productRepository; } public List<Product> gets(RequestParams<Product> requestParams) { String prdtCd=requestParams.getString("prdtCd", ""); String prdtNm=requestParams.getString("prdtNm", ""); String filter = requestParams.getString("filter");
BooleanBuilder builder = new BooleanBuilder(); if (isNotEmpty(prdtCd)) { builder.and(qProduct.prdtCd.eq(prdtCd)); } if (isNotEmpty(prdtNm)) { builder.and(qProduct.prdtNm.eq(prdtNm)); } List<Product> prdtList = select().from(qProduct).where(builder).orderBy(qProduct.prdtCd.asc(), qProduct.prdtNm.asc()).fetch(); if (isNotEmpty(filter)) { prdtList = filter(prdtList, filter); }
return prdtList; }
@Transactional public void savePrdt(List<Product> product) { Product prdt=null; for(int i=0;i<product.size();i++) { prdt=(Product)product.get(i); //logger.error(i+": {} :"+prdt.toString());
if(prdt.is__created__() && exists(prdt.getId())) { throw new DBException("신규로 추가하신 "+prdt.getId()+"코드는 이미 등록된 제품코드입니다."); } } save(product); //제품 판매가 History Table에 Insert되는 method호출 prdtPriceHistService.savePrdPrice(product); } }
|
5) 테스트
- 서버 구동
- 제품 정보에서 판매가 수정을 해본다.
- 제품 판매가 History 조회 : 하다보니 누가 변경했는지가 필요할 것 같은데 빠졌네요.^^
그 부분은 여러분들이 한번 추가해 보세요..==>과제.
어때요? 도움이 많이 되셨나요? ~~~~~~~
'오픈소스 > AXBoot프로그래밍' 카테고리의 다른 글
Axboot 멀티 테이블 수정/저장 예제 (1/2) (0) | 2019.06.05 |
---|---|
Axboot 신규화면 만들기 7 - 중복등록체크로직 추가 -2 소스구현 (0) | 2018.11.30 |
Axboot 신규화면 만들기 7 - 중복등록체크로직 추가 -1 현상/방안 (0) | 2018.11.28 |
Axboot 신규화면 만들기 6 - 행추가시 초기값(default) 셋팅하기 (0) | 2018.11.28 |