variety_plant.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package supply_analysis
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "gorm.io/gorm"
  6. "time"
  7. )
  8. // VarietyPlant variety_plant 品种装置表
  9. type VarietyPlant struct {
  10. VarietyPlantId int `orm:"column(variety_plant_id);pk" gorm:"primaryKey" `
  11. VarietyId int `description:"品种id"`
  12. Province string `description:"所属省份"`
  13. City string `description:"所属城市"`
  14. FactoryName string `description:"工厂名称"`
  15. PlantName string `description:"装置/产线名称"`
  16. MaintenanceDate time.Time `description:"检修日期"`
  17. ResumptionDate time.Time `description:"复产日期"`
  18. AnnualCapacity float64 `description:"年产能"`
  19. Coefficient float64 `description:"降负系数"`
  20. AverageDailyCapacityReduction float64 `description:"日均产量减量"`
  21. IsStop int `description:"是否停产,0:未停产;1:停产;默认未停产"`
  22. Sort int `description:"排序字段,越小越靠前"`
  23. SysUserId int `description:"添加人id"`
  24. SysUserRealName string `description:"添加人真实姓名"`
  25. ModifyTime time.Time `description:"最近一次更新时间"`
  26. CreateTime time.Time `description:"添加时间"`
  27. }
  28. // AddVarietyPlant 添加variety_plant装置
  29. func AddVarietyPlant(item *VarietyPlant) (err error) {
  30. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  31. return
  32. }
  33. // MultiAddVarietyPlant 批量添加variety_plant装置
  34. func MultiAddVarietyPlant(items []*VarietyPlant) (err error) {
  35. err = global.DbMap[utils.DbNameIndex].CreateInBatches(items, utils.MultiAddNum).Error
  36. return
  37. }
  38. // Update 更新基础信息
  39. func (varietyPlant *VarietyPlant) Update(cols []string) (err error) {
  40. err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(varietyPlant).Error
  41. return
  42. }
  43. // GetVarietyPlantById 根据品种装置id获取品种装置详情
  44. func GetVarietyPlantById(id int) (item *VarietyPlant, err error) {
  45. sql := `SELECT * FROM variety_plant WHERE variety_plant_id = ?`
  46. err = global.DbMap[utils.DbNameIndex].Raw(sql, id).First(&item).Error
  47. return
  48. }
  49. // GetVarietyPlantByIdList 根据品种装置id列表获取品种装置详情列表
  50. func GetVarietyPlantByIdList(idList []int) (items []*VarietyPlant, err error) {
  51. num := len(idList)
  52. if num <= 0 {
  53. return
  54. }
  55. sql := `SELECT * FROM variety_plant WHERE variety_plant_id in (` + utils.GetOrmInReplace(num) + `)`
  56. err = global.DbMap[utils.DbNameIndex].Raw(sql, idList).Find(&items).Error
  57. return
  58. }
  59. // VarietyPlantItem 品种装置数据(展示使用)
  60. type VarietyPlantItem struct {
  61. VarietyPlantId int `orm:"column(variety_plant_id);pk" gorm:"primaryKey" `
  62. VarietyId int `description:"品种id"`
  63. Province string `description:"所属省份"`
  64. City string `description:"所属城市"`
  65. FactoryName string `description:"工厂名称"`
  66. PlantName string `description:"装置/产线名称"`
  67. MaintenanceDate string `description:"检修日期"`
  68. ResumptionDate string `description:"复产日期"`
  69. AnnualCapacity float64 `description:"年产能"`
  70. Coefficient float64 `description:"降负系数"`
  71. AverageDailyCapacityReduction float64 `description:"日均产量减量"`
  72. Day int `description:"时长"`
  73. CapacityReduction float64 `description:"期间减量"`
  74. IsStop int `description:"是否停产,0:未停产;1:停产;默认未停产"`
  75. Sort int `description:"排序字段,越小越靠前"`
  76. SysUserId int `description:"添加人id"`
  77. SysUserRealName string `description:"添加人真实姓名"`
  78. ModifyTime string `description:"最近一次更新时间"`
  79. CreateTime string `description:"添加时间"`
  80. Button VarietyPlantButton `gorm:"-" description:"操作按钮权限"`
  81. }
  82. func (v *VarietyPlantItem) AfterFind(tx *gorm.DB) (err error) {
  83. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  84. if v.CreateTime != "" {
  85. v.CreateTime = utils.GormDateStrToDateTimeStr(v.CreateTime)
  86. }
  87. if v.ModifyTime != "" {
  88. v.ModifyTime = utils.GormDateStrToDateTimeStr(v.ModifyTime)
  89. }
  90. if v.MaintenanceDate != "" {
  91. v.MaintenanceDate = utils.GormDateStrToDateStr(v.MaintenanceDate)
  92. }
  93. if v.ResumptionDate != "" {
  94. v.ResumptionDate = utils.GormDateStrToDateStr(v.ResumptionDate)
  95. }
  96. }
  97. return
  98. }
  99. type VarietyPlantButton struct {
  100. Edit bool `description:"操作权限"`
  101. Delete bool `description:"删除权限"`
  102. }
  103. // GetAllVarietyPlantByVarietyId 根据品种id获取所有的装置
  104. func GetAllVarietyPlantByVarietyId(varietyId int) (items []*VarietyPlantItem, err error) {
  105. sql := `SELECT * FROM variety_plant a WHERE variety_id = ? ORDER BY sort ASC, variety_plant_id ASC `
  106. err = global.DbMap[utils.DbNameIndex].Raw(sql, varietyId).Find(&items).Error
  107. return
  108. }
  109. // GetCountVarietyPlantByVarietyId 根据品种id获取所有的装置数量
  110. func GetCountVarietyPlantByVarietyId(varietyId int) (total int, err error) {
  111. sql := `SELECT count(1) total FROM variety_plant a WHERE variety_id = ? `
  112. err = global.DbMap[utils.DbNameIndex].Raw(sql, varietyId).Scan(&total).Error
  113. return
  114. }
  115. // DeleteVarietyPlantById 根据装置id删除
  116. func DeleteVarietyPlantById(id int) (err error) {
  117. sql := `DELETE FROM variety_plant WHERE variety_plant_id = ?`
  118. err = global.DbMap[utils.DbNameIndex].Exec(sql, id).Error
  119. return
  120. }
  121. // UpdateEdbInfoSortByPrevId 根据上一层级id更新排序
  122. func UpdateEdbInfoSortByPrevId(varietyId, prevVarietyId, nowSort int, updateSort string) (err error) {
  123. sql := `UPDATE variety_plant set sort = ` + updateSort + ` WHERE variety_id=? AND `
  124. if prevVarietyId > 0 {
  125. sql += ` ( sort > ? or ( sort=? AND variety_plant_id > ?)) `
  126. }
  127. err = global.DbMap[utils.DbNameIndex].Exec(sql, varietyId, nowSort, nowSort, prevVarietyId).Error
  128. return
  129. }
  130. // UpdateEdbInfoSortByNextId 根据下一层级id更新排序
  131. func UpdateEdbInfoSortByNextId(varietyId, prevVarietyId, nowSort int, updateSort string) (err error) {
  132. sql := `UPDATE variety_plant set sort = ` + updateSort + ` WHERE variety_id=? AND `
  133. if prevVarietyId > 0 {
  134. sql += ` ( sort < ? or ( sort=? AND variety_plant_id < ? )) `
  135. }
  136. err = global.DbMap[utils.DbNameIndex].Exec(sql, varietyId, nowSort, nowSort, prevVarietyId).Error
  137. return
  138. }