cut-to-pack-service/src/main/java/com/utopiaindustries/dao/ctp/StitchingOfflineItemDAO.java

145 lines
7.6 KiB
Java

package com.utopiaindustries.dao.ctp;
import com.utopiaindustries.model.ctp.StitchingOfflineItem;
import com.utopiaindustries.util.KeyHolderFunctions;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Repository
public class StitchingOfflineItemDAO {
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private final String TABLE_NAME = "cut_to_pack.stitching_offline_item";
private final String SELECT_QUERY = String.format( "SELECT * FROM %s WHERE id = :id", TABLE_NAME );
private final String SELECT_ALL_QUERY = String.format( "SELECT * FROM %s ORDER BY id DESC", TABLE_NAME );
private final String SELECT_QUERY_BY_JOB_CARD = String.format( "SELECT * FROM %s WHERE job_card_id = :job_card_id", TABLE_NAME );
private final String DELETE_QUERY = String.format( "DELETE FROM %s WHERE id = :id", TABLE_NAME );
private final String INSERT_QUERY = String.format( "INSERT INTO %s (id, item_id, sku, barcode, created_at, created_by, job_card_id, is_qa, qa_remarks, qa_status,bundle_id) VALUES (:id, :item_id, :sku, :barcode, :created_at, :created_by, :job_card_id, :is_qa, :qa_remarks, :qa_status, :bundle_id) ON DUPLICATE KEY UPDATE item_id = VALUES(item_id), sku = VALUES(sku), barcode = VALUES(barcode), created_at = VALUES(created_at), created_by = VALUES(created_by), job_card_id = VALUES(job_card_id), is_qa = VALUES(is_qa), qa_remarks = VALUES(qa_remarks), qa_status = VALUES(qa_status), bundle_id = VALUES(bundle_id)", TABLE_NAME );
private final String SELECT_BY_LIMIT = String.format("SELECT * FROM %s ORDER BY id DESC LIMIT :limit", TABLE_NAME );
private final String FIND_TOTAL_COUNT = String.format("SELECT COUNT(*) FROM %s where job_card_id = :job_card_id And item_id = :item_id", TABLE_NAME );
private final String SELECT_BY_IDS = String.format( "SELECT * FROM %s WHERE id IN (:ids)", TABLE_NAME );
private final String SELECT_BY_TERM = String.format( "SELECT * FROM %s WHERE barcode LIKE :term ORDER BY ID DESC", TABLE_NAME );
private final String SELECT_BY_MASTER_ID = String.format( "SELECT * FROM %s WHERE job_card_id = :job_card_id", TABLE_NAME );
public StitchingOfflineItemDAO(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}
// prepare query params
private MapSqlParameterSource prepareInsertQueryParams(StitchingOfflineItem stitchingOfflineItem ) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue( "id", stitchingOfflineItem.getId() )
.addValue( "item_id", stitchingOfflineItem.getItemId() )
.addValue( "sku", stitchingOfflineItem.getSku() )
.addValue( "barcode", stitchingOfflineItem.getBarcode() )
.addValue("bundle_id",stitchingOfflineItem.getBundleId())
.addValue( "created_at", stitchingOfflineItem.getCreatedAt() )
.addValue( "created_by", stitchingOfflineItem.getCreatedBy() )
.addValue("job_card_id", stitchingOfflineItem.getJobCardId() )
.addValue("is_qa", stitchingOfflineItem.getIsQa() )
.addValue("qa_remarks", stitchingOfflineItem.getQaRemarks() )
.addValue("qa_status", stitchingOfflineItem.getQaStatus() );
return params;
}
// find
public StitchingOfflineItem find( long id ) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue( "id", id );
return namedParameterJdbcTemplate.query( SELECT_QUERY, params, new StitchingOfflineItemRowMapper() )
.stream()
.findFirst()
.orElse( new StitchingOfflineItem() );
}
// find all
public List<StitchingOfflineItem> findAll() {
return namedParameterJdbcTemplate.query( SELECT_ALL_QUERY, new StitchingOfflineItemRowMapper() );
}
// save
public long save( StitchingOfflineItem finishedItem ) {
KeyHolder keyHolder = new GeneratedKeyHolder();
MapSqlParameterSource params = prepareInsertQueryParams( finishedItem );
namedParameterJdbcTemplate.update( INSERT_QUERY, params, keyHolder );
return KeyHolderFunctions.getKey( finishedItem.getId(), keyHolder );
}
// save all
public int[] saveAll( List<StitchingOfflineItem> stitchingOfflineItems ) {
List<MapSqlParameterSource> batchArgs = new ArrayList<>();
for ( StitchingOfflineItem stitchingOfflineItem: stitchingOfflineItems ) {
MapSqlParameterSource params = prepareInsertQueryParams( stitchingOfflineItem );
batchArgs.add( params );
}
return namedParameterJdbcTemplate.batchUpdate( INSERT_QUERY, batchArgs.toArray(new MapSqlParameterSource[stitchingOfflineItems.size()]) );
}
// delete
public boolean delete( long id ) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue( "id", id );
return namedParameterJdbcTemplate.update( DELETE_QUERY, params ) > 0;
}
public List<StitchingOfflineItem> findByLimit(Long count ){
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue( "limit",count );
return namedParameterJdbcTemplate.query( SELECT_BY_LIMIT , params, new StitchingOfflineItemRowMapper() );
}
public List<StitchingOfflineItem> findByQuery(String query ){
return namedParameterJdbcTemplate.query( query, new StitchingOfflineItemRowMapper() );
}
public List<StitchingOfflineItem> findByIds(List<Long> ids ){
if( ids == null || ids.isEmpty() ) return new ArrayList<>();
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("ids", ids );
return namedParameterJdbcTemplate.query( SELECT_BY_IDS , params, new StitchingOfflineItemRowMapper() );
}
public List<StitchingOfflineItem> findByJobCardId(long jobCardId){
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue( "job_card_id", jobCardId );
return namedParameterJdbcTemplate.query( SELECT_QUERY_BY_JOB_CARD , params, new StitchingOfflineItemRowMapper() );
}
public List<StitchingOfflineItem> findByTerm( String term ){
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("term", "%" + term + "%" );
// params.addValue("is_qa", isQa );
return namedParameterJdbcTemplate.query( SELECT_BY_TERM , params, new StitchingOfflineItemRowMapper() );
}
public List<StitchingOfflineItem> findByMasterId( long masterId ){
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("job_card_id", masterId );
return namedParameterJdbcTemplate.query( SELECT_BY_MASTER_ID , params, new StitchingOfflineItemRowMapper() );
}
public HashMap<Long, Long> findTotalStitchingOfflineItems(List<Long> itemIds, long jobCardId) {
HashMap<Long, Long> totalCounts = new HashMap<>();
MapSqlParameterSource params = new MapSqlParameterSource();
for (long id : itemIds) {
params.addValue("job_card_id", jobCardId);
params.addValue("item_id", id);
Long total = namedParameterJdbcTemplate.queryForObject(FIND_TOTAL_COUNT, params, Long.class);
if (total != null) {
totalCounts.put(id, total);
}
}
return totalCounts;
}
}