(스프링, 마이바티스, Angular연동 쇼핑몰)전자정부2기 팀 프로젝트 쇼핑몰 관리자 페이지_ 최성찬

상품 코드 자동 생성 로직만 넣었습니다

나머지 소스는 git을 참고해주세요

 

스프링

 

https://github.com/TopcreTeam/ShoppingMallAPI/commits?author=pikacsc

 

 

 

 

앵귤러

 

https://github.com/TopcreTeam/TestUi/commits?author=pikacsc 

 

 

 

 

 

 

 

 

package shop.park.service;

 

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import shop.park.model.Products;

import shop.park.repository.ProductMapper;

import shop.utill.ProductCodeMaker;

 

@Service

public class ProductServiceImpl implements ProductService {

@Autowired

ProductMapper productMapper;

@Override

public List<Products> selectAllProducts(){

List<Products> productsList = productMapper.selectAllProducts();

return productsList;

}

 

@Override

public List<Products> selectByProductKind(String p_kind) {

 

List<Products> productsKindList = productMapper.selectByProductKind(p_kind);

return productsKindList;

}

 

@Override

public Products selectByProductCode(String p_code) {

Products product = productMapper.selectByProductCode(p_code);

return product;

}

 

@Override

public void insertProduct(Products product) {

if(product.getP_code()==null || product.getP_code()=="") {

List<Products> productList = productMapper.selectByProductKind(product.getP_kind());

ProductCodeMaker codeMaker = new ProductCodeMaker(productList);

product.setP_code(codeMaker.getNewProductCode());

product.setP_img(codeMaker.getNewProductCode());

System.out.println("────────────────────새상품코드 "+product.getP_code());

productMapper.insertProduct(product);

}else {

System.out.println("──────────────────── "+product.getP_code());

productMapper.insertProduct(product);

}

}

 

@Override

public void updateProduct(Products product) {

productMapper.updateProduct(product);

}

 

@Override

public void deleteProduct(String p_code) {

productMapper.deleteProduct(p_code);

}

 

public void codeMaker() {

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package shop.utill;

 

import java.util.List;

 

import shop.park.model.Products;

 

public class ProductCodeMaker {

private String zeroString;

private String productFrontCode;

private String productBackCode;

private String newProductCode;

public String getNewProductCode() {

return newProductCode;

}

 

 

public ProductCodeMaker(List<Products> productList) {

MakeProductFrontCode(productList.get(0).getP_kind());

System.out.println("────────────────────frontcode : "+ this.productFrontCode);

MakeProductBackCode(productList);

System.out.println("────────────────────backcode : "+ this.productBackCode);

this.newProductCode = productFrontCode+productBackCode;

System.out.println("────────────────────front+back: "+ this.newProductCode);

}

void MakeProductFrontCode(String productKind){

if(productKind.equals("Bakery")) {

this.productFrontCode = "B";

}else if(productKind.equals("Sauce")) {

this.productFrontCode = "S";

}else if(productKind.equals("Drink")) {

this.productFrontCode = "D";

}else if(productKind.equals("Instant")) {

this.productFrontCode = "I";

}else {

this.productFrontCode = "SN";

}

}

void MakeProductBackCode(List<Products> productList) {

Products lastProduct = productList.get(productList.size()-1);

String lastProductCode = lastProduct.getP_code();

lastProductCode = lastProductCode.replaceAll("[^0-9]", "");

System.out.println("───────────────────────lastProcutCode : "+lastProductCode+"───────────────────────────────");

int newProductNumber = Integer.parseInt(lastProductCode)+1;

this.productBackCode = pad(newProductNumber,4);

}

String pad(int number, int width) {

String xnumber = number+ "";

zeroString = "";

int numberlength = xnumber.length();

if(numberlength>=width) {

return xnumber;

}else {                 

int zeroCount = width - numberlength; //0이 들어갈 횟수

for(int i = 0 ;i<zeroCount;i++) {

zeroString += "0";

if(i==zeroCount) {

break;

}

}

System.out.println("───────────────────────zeroString : "+zeroString+"───────────────────────────────");

System.out.println("───────────────────────zeroString+xnumber : "+zeroString+xnumber+"───────────────────────────────");

return zeroString+xnumber;

}

}

}



















Comments