diff --git a/src/main/java/com/ffii/fpsms/modules/master/service/ItemsService.kt b/src/main/java/com/ffii/fpsms/modules/master/service/ItemsService.kt index 3232e1a..65947c7 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/service/ItemsService.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/service/ItemsService.kt @@ -10,6 +10,8 @@ import com.ffii.fpsms.modules.master.web.models.NewItemRequest import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.io.IOException +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter @Service open class ItemsService( @@ -25,6 +27,48 @@ open class ItemsService( return items } + open fun getRoughScheduleList(): List> { + val now = LocalDateTime.now() + val lastMonthStart = now.minusMonths(1).withDayOfMonth(1) // Start of last month + val lastMonthEnd = now.minusDays(now.dayOfMonth.toLong()).withHour(23).withMinute(59).withSecond(59) // End of last month + + val curMonthStart = now.withDayOfMonth(1) // Start of last month + val curMonthEnd = now.withDayOfMonth(31).withHour(23).withMinute(59).withSecond(59) // End of last month + + // Format dates if needed + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") + + //TODO: change back to use last month for production + val args = mapOf( + "lastMonthStart" to curMonthStart.format(formatter), + "lastMonthEnd" to curMonthEnd.format(formatter) + ) + + println("lastMonthStart:" + args.get("lastMonthStart")) + println("lastMonthEnd:" + args.get("lastMonthEnd")) + + val sql = StringBuilder( + " SELECT " + + " pol.itemId, " + + " i.name AS itemName, " + + " pol.itemNo, " + + " 0 AS openBalance, " + + " COUNT(DISTINCT pol.purchaseOrderId) AS orderCount, " + + " SUM(IFNULL(pol.qty, 0)) AS totalQty, " + + " SUM(IFNULL(pol.qty, 0)) / COUNT(DISTINCT pol.purchaseOrderId) AS lastMonthAvgSalesCount, " + + " 22000 - IFNULL(i.maxQty , 0) AS fgProductionLimit " + + " FROM purchase_order po " + + " LEFT JOIN purchase_order_line pol ON pol.purchaseOrderId = po.id " + + " LEFT JOIN items i ON i.id = pol.itemId " + + " WHERE po.deleted = false " + + " AND po.estimatedArrivalDate >= :lastMonthStart " + + " AND po.estimatedArrivalDate <= :lastMonthEnd " + + " AND po.`type` LIKE \"shop\" " + + " GROUP BY pol.itemId, pol.itemNo, i.name " + ); + return jdbcDao.queryForList(sql.toString(), args); + } + open fun getItemsByPage(args: Map): List> { // Extract parameters from the DTO val sql = StringBuilder( diff --git a/src/main/java/com/ffii/fpsms/modules/master/service/SchedulingService.kt b/src/main/java/com/ffii/fpsms/modules/master/service/SchedulingService.kt index 3618175..479c0a3 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/service/SchedulingService.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/service/SchedulingService.kt @@ -3,30 +3,39 @@ package com.ffii.fpsms.modules.master.service import com.ffii.core.support.AbstractBaseEntityService import com.ffii.core.support.JdbcDao import com.ffii.fpsms.modules.master.entity.* -import com.ffii.fpsms.modules.master.web.models.MessageResponse -import org.springframework.core.io.ClassPathResource import org.springframework.stereotype.Service -import com.google.gson.Gson -import org.springframework.transaction.annotation.Transactional -import java.io.BufferedReader -import java.io.IOException -import java.io.InputStreamReader -import java.nio.charset.StandardCharsets -import java.sql.DriverManager.println -import java.time.LocalDateTime -import java.time.format.DateTimeFormatter import java.util.* -import java.util.regex.Pattern -import kotlin.math.roundToLong +import kotlin.collections.component1 +import kotlin.collections.component2 import kotlin.math.ceil @Service open class SchedulingService( private val jdbcDao: JdbcDao, private val uomConversionRepository: UomConversionRepository, + private val itemService: ItemsService, ): AbstractBaseEntityService(jdbcDao, uomConversionRepository) { // do mapping with projection + open fun convertToFinishedGoodList(): ArrayList { + val roughScheduleList = itemService.getRoughScheduleList() // Retrieve the list from your existing method + val finishedGoodList = ArrayList() + + for (item in roughScheduleList) { + val finishedGood = FinishedGood( + id = item["itemId"].toString().toLong(), // Cast to Long + name = item["itemName"] as String?, // Cast to String + receipeId = 0, // Assuming itemNo is a Long + openBalance = 0.0, // Set default value or calculate if needed + lastMonthAvgSalesCount = item["lastMonthAvgSalesCount"].toString().toDouble(), // Cast to Double + fgProductionLimit = item["fgProductionLimit"].toString().toDouble() // Default production limit + ) + finishedGoodList.add(finishedGood) + } + + return finishedGoodList + } + open fun generateRoughScheduleByWeek(fgList : ArrayList): HashMap { val roughScheduleOutput: HashMap = HashMap(); @@ -110,10 +119,10 @@ open class SchedulingService( //debug use for ((roughScheduleRecord, totalDifference) in sortedEntries){ - kotlin.io.println("[RUN" + i + "][index:" + totalDifference + "] - " + roughScheduleRecord.fgDetail?.name + " - " + roughScheduleRecord.productionSchedule[i]); + println("[RUN" + i + "][index:" + totalDifference + "] - " + roughScheduleRecord.fgDetail?.name + " - " + roughScheduleRecord.productionSchedule[i]); } - kotlin.io.println("Total Production Count $accProductionCount"); - kotlin.io.println("========================================"); + println("Total Production Count $accProductionCount"); + println("========================================"); } diff --git a/src/main/java/com/ffii/fpsms/modules/master/web/UomConversionController.kt b/src/main/java/com/ffii/fpsms/modules/master/web/UomConversionController.kt index 25a69e0..6a26861 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/web/UomConversionController.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/web/UomConversionController.kt @@ -17,7 +17,7 @@ import kotlin.collections.component2 @RequestMapping("/uomConversion") class UomConversionController( private val uomConversionService: UomConversionService, - private val schedulingService: SchedulingService + private val schedulingService: SchedulingService, ) { // @GetMapping // fun allItems(): List { @@ -45,7 +45,7 @@ class UomConversionController( @RequestMapping(value = ["/testRoughSchedule"], method = [RequestMethod.GET]) fun generateRoughSchedule(request: HttpServletRequest?): List> { try { - val demoFGList = arrayListOf( + val demoFGList_old = arrayListOf( FinishedGood( 1, "豆腐花 - Tofu pudding", @@ -112,6 +112,8 @@ class UomConversionController( ), ); + val demoFGList = schedulingService.convertToFinishedGoodList(); + val result: HashMap = schedulingService.generateRoughScheduleByWeek(demoFGList) val sortedEntries = result.entries.sortedBy { it.value }