| @@ -10,6 +10,8 @@ import com.ffii.fpsms.modules.master.web.models.NewItemRequest | |||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
| import org.springframework.transaction.annotation.Transactional | import org.springframework.transaction.annotation.Transactional | ||||
| import java.io.IOException | import java.io.IOException | ||||
| import java.time.LocalDateTime | |||||
| import java.time.format.DateTimeFormatter | |||||
| @Service | @Service | ||||
| open class ItemsService( | open class ItemsService( | ||||
| @@ -25,6 +27,48 @@ open class ItemsService( | |||||
| return items | return items | ||||
| } | } | ||||
| open fun getRoughScheduleList(): List<Map<String, Any>> { | |||||
| 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<String, Any>): List<Map<String, Any>> { | open fun getItemsByPage(args: Map<String, Any>): List<Map<String, Any>> { | ||||
| // Extract parameters from the DTO | // Extract parameters from the DTO | ||||
| val sql = StringBuilder( | val sql = StringBuilder( | ||||
| @@ -3,30 +3,39 @@ package com.ffii.fpsms.modules.master.service | |||||
| import com.ffii.core.support.AbstractBaseEntityService | import com.ffii.core.support.AbstractBaseEntityService | ||||
| import com.ffii.core.support.JdbcDao | import com.ffii.core.support.JdbcDao | ||||
| import com.ffii.fpsms.modules.master.entity.* | 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 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.* | ||||
| import java.util.regex.Pattern | |||||
| import kotlin.math.roundToLong | |||||
| import kotlin.collections.component1 | |||||
| import kotlin.collections.component2 | |||||
| import kotlin.math.ceil | import kotlin.math.ceil | ||||
| @Service | @Service | ||||
| open class SchedulingService( | open class SchedulingService( | ||||
| private val jdbcDao: JdbcDao, | private val jdbcDao: JdbcDao, | ||||
| private val uomConversionRepository: UomConversionRepository, | private val uomConversionRepository: UomConversionRepository, | ||||
| private val itemService: ItemsService, | |||||
| ): AbstractBaseEntityService<UomConversion, Long, UomConversionRepository>(jdbcDao, uomConversionRepository) { | ): AbstractBaseEntityService<UomConversion, Long, UomConversionRepository>(jdbcDao, uomConversionRepository) { | ||||
| // do mapping with projection | // do mapping with projection | ||||
| open fun convertToFinishedGoodList(): ArrayList<FinishedGood> { | |||||
| val roughScheduleList = itemService.getRoughScheduleList() // Retrieve the list from your existing method | |||||
| val finishedGoodList = ArrayList<FinishedGood>() | |||||
| 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<FinishedGood>): HashMap<RoughScheduleObj, Double> { | open fun generateRoughScheduleByWeek(fgList : ArrayList<FinishedGood>): HashMap<RoughScheduleObj, Double> { | ||||
| val roughScheduleOutput: HashMap<RoughScheduleObj, Double> = HashMap(); | val roughScheduleOutput: HashMap<RoughScheduleObj, Double> = HashMap(); | ||||
| @@ -110,10 +119,10 @@ open class SchedulingService( | |||||
| //debug use | //debug use | ||||
| for ((roughScheduleRecord, totalDifference) in sortedEntries){ | 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("========================================"); | |||||
| } | } | ||||
| @@ -17,7 +17,7 @@ import kotlin.collections.component2 | |||||
| @RequestMapping("/uomConversion") | @RequestMapping("/uomConversion") | ||||
| class UomConversionController( | class UomConversionController( | ||||
| private val uomConversionService: UomConversionService, | private val uomConversionService: UomConversionService, | ||||
| private val schedulingService: SchedulingService | |||||
| private val schedulingService: SchedulingService, | |||||
| ) { | ) { | ||||
| // @GetMapping | // @GetMapping | ||||
| // fun allItems(): List<Items> { | // fun allItems(): List<Items> { | ||||
| @@ -45,7 +45,7 @@ class UomConversionController( | |||||
| @RequestMapping(value = ["/testRoughSchedule"], method = [RequestMethod.GET]) | @RequestMapping(value = ["/testRoughSchedule"], method = [RequestMethod.GET]) | ||||
| fun generateRoughSchedule(request: HttpServletRequest?): List<HashMap<RoughScheduleObj, Double>> { | fun generateRoughSchedule(request: HttpServletRequest?): List<HashMap<RoughScheduleObj, Double>> { | ||||
| try { | try { | ||||
| val demoFGList = arrayListOf<FinishedGood>( | |||||
| val demoFGList_old = arrayListOf<FinishedGood>( | |||||
| FinishedGood( | FinishedGood( | ||||
| 1, | 1, | ||||
| "豆腐花 - Tofu pudding", | "豆腐花 - Tofu pudding", | ||||
| @@ -112,6 +112,8 @@ class UomConversionController( | |||||
| ), | ), | ||||
| ); | ); | ||||
| val demoFGList = schedulingService.convertToFinishedGoodList(); | |||||
| val result: HashMap<RoughScheduleObj, Double> = schedulingService.generateRoughScheduleByWeek(demoFGList) | val result: HashMap<RoughScheduleObj, Double> = schedulingService.generateRoughScheduleByWeek(demoFGList) | ||||
| val sortedEntries = result.entries.sortedBy { it.value } | val sortedEntries = result.entries.sortedBy { it.value } | ||||