variety.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package supply_analysis
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. "gorm.io/gorm"
  8. )
  9. // Variety variety 品种表
  10. type Variety struct {
  11. VarietyId int `orm:"column(variety_id);pk" gorm:"primaryKey" `
  12. VarietyName string `description:"品种名称"`
  13. LastUpdateSysUserId int `description:"最后更新人id"`
  14. LastUpdateSysUserRealName string `description:"最后更新人名称"`
  15. ProductionDay int `description:"生产天数"`
  16. SysUserId int `description:"创建人id"`
  17. SysUserRealName string `description:"创建人姓名"`
  18. ModifyTime time.Time `description:"修改时间"`
  19. CreateTime time.Time `description:"创建时间"`
  20. }
  21. // GetVarietyById 根据品种id获取品种详情
  22. func GetVarietyById(id int) (item *Variety, err error) {
  23. sql := `SELECT * FROM variety WHERE variety_id = ?`
  24. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  25. return
  26. }
  27. // GetVarietyByName 根据品种名称获取品种详情
  28. func GetVarietyByName(name string) (item *Variety, err error) {
  29. sql := `SELECT * FROM variety WHERE variety_name = ?`
  30. err = global.DmSQL["data"].Raw(sql, name).First(&item).Error
  31. return
  32. }
  33. // CreateVariety 添加品种
  34. func CreateVariety(item *Variety, adminIdList []int) (err error) {
  35. to := global.DmSQL["data"].Begin()
  36. defer func() {
  37. if err != nil {
  38. _ = to.Rollback()
  39. } else {
  40. _ = to.Commit()
  41. }
  42. }()
  43. err = to.Create(item).Error
  44. if err != nil {
  45. return
  46. }
  47. varietyAdminPermissionList := make([]*VarietyAdminPermission, 0)
  48. for _, adminId := range adminIdList {
  49. varietyAdminPermissionList = append(varietyAdminPermissionList, &VarietyAdminPermission{
  50. //Id: 0,
  51. VarietyId: item.VarietyId,
  52. SysUserId: adminId,
  53. CreateTime: time.Now(),
  54. })
  55. }
  56. if len(varietyAdminPermissionList) > 0 {
  57. err = to.CreateInBatches(varietyAdminPermissionList, utils.MultiAddNum).Error
  58. }
  59. return
  60. }
  61. // EditVariety 编辑品种
  62. func EditVariety(item *Variety, adminIdList []int) (err error) {
  63. to := global.DmSQL["data"].Begin()
  64. defer func() {
  65. if err != nil {
  66. _ = to.Rollback()
  67. } else {
  68. _ = to.Commit()
  69. }
  70. }()
  71. err = to.Select("VarietyName", "LastUpdateSysUserId", "LastUpdateSysUserRealName", "ModifyTime").Updates(item).Error
  72. if err != nil {
  73. return
  74. }
  75. // 删除历史的权限配置
  76. sql := `DELETE FROM variety_admin_permission where variety_id = ? `
  77. err = to.Exec(sql, item.VarietyId).Error
  78. if err != nil {
  79. return
  80. }
  81. // 添加新的权限配置
  82. varietyAdminPermissionList := make([]*VarietyAdminPermission, 0)
  83. for _, adminId := range adminIdList {
  84. varietyAdminPermissionList = append(varietyAdminPermissionList, &VarietyAdminPermission{
  85. //Id: 0,
  86. VarietyId: item.VarietyId,
  87. SysUserId: adminId,
  88. CreateTime: time.Now(),
  89. })
  90. }
  91. if len(varietyAdminPermissionList) > 0 {
  92. err = to.CreateInBatches(varietyAdminPermissionList, utils.MultiAddNum).Error
  93. }
  94. return
  95. }
  96. // VarietyItem 列表页返回的数据结构
  97. type VarietyItem struct {
  98. VarietyId int `orm:"column(variety_id);pk" gorm:"primaryKey" `
  99. VarietyName string `description:"品种名称"`
  100. ProductionDay int `description:"生产天数"`
  101. LastUpdateSysUserId int `description:"最后更新人id"`
  102. LastUpdateSysUserRealName string `description:"最后更新人名称"`
  103. PermissionUserId string `description:"有操作权限的用户id"`
  104. ModifyTime string `description:"修改时间"`
  105. CreateTime string `description:"创建时间"`
  106. Button VarietyButton `gorm:"-" description:"操作按钮权限"`
  107. }
  108. type VarietyButton struct {
  109. Edit bool `description:"操作权限"`
  110. Delete bool `description:"删除权限"`
  111. Analyse bool `description:"分析权限"`
  112. }
  113. func (v *VarietyItem) AfterFind(db *gorm.DB) error {
  114. tmpCreateTime, err := time.Parse(utils.FormatDateWallWithLoc, v.CreateTime)
  115. if err != nil {
  116. return err
  117. }
  118. tmpModifyTime, err := time.Parse(utils.FormatDateWallWithLoc, v.ModifyTime)
  119. if err != nil {
  120. return err
  121. }
  122. v.CreateTime = utils.TimeTransferString(utils.FormatDateTime, tmpCreateTime)
  123. v.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, tmpModifyTime)
  124. return nil
  125. }
  126. // GetListBySuperAdminPage 不区分是否有分析权限的获取分页数据
  127. func (item Variety) GetListBySuperAdminPage(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*VarietyItem, err error) {
  128. baseSql := ` FROM ( SELECT a.variety_id,a.variety_name,a.last_update_sys_user_id,
  129. a.last_update_sys_user_real_name,a.production_day,a.sys_user_id,a.sys_user_real_name,a.modify_time,a.create_time,LISTAGG(b.sys_user_id, ',') WITHIN GROUP (ORDER BY b.sys_user_id ASC) AS permission_user_id
  130. FROM variety a
  131. LEFT JOIN variety_admin_permission b on a.variety_id=b.variety_id
  132. LEFT JOIN variety_edb_info c on a.variety_id=c.variety_id WHERE 1=1 `
  133. if condition != "" {
  134. baseSql += condition
  135. }
  136. baseSql += ` GROUP BY a.variety_id,a.variety_name,a.last_update_sys_user_id,
  137. a.last_update_sys_user_real_name,a.production_day,a.sys_user_id,a.sys_user_real_name,a.modify_time,a.create_time ) d `
  138. // 数据总数
  139. totalSql := `SELECT COUNT(1) total ` + baseSql
  140. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error
  141. if err != nil {
  142. fmt.Println("Count Err:", err)
  143. return
  144. }
  145. // 列表页数据
  146. listSql := `SELECT * ` + baseSql + ` ORDER BY modify_time DESC,variety_id DESC LIMIT ?,?`
  147. pars = append(pars, startSize)
  148. pars = append(pars, pageSize)
  149. err = global.DmSQL["data"].Raw(listSql, pars...).Find(&items).Error
  150. return
  151. }
  152. // GetListByPage 获取分页数据
  153. func (item Variety) GetListByPage(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*VarietyItem, err error) {
  154. baseSql := ` FROM ( SELECT a.variety_id,a."variety_name",a."production_day",a."last_update_sys_user_id",a."last_update_sys_user_real_name",a."modify_time",a."create_time", LISTAGG(DISTINCT b.sys_user_id , ',') WITHIN GROUP (ORDER BY b.sys_user_id asc) AS permission_user_id
  155. FROM variety a
  156. JOIN variety_admin_permission b on a.variety_id=b.variety_id
  157. JOIN variety_edb_info c on a.variety_id=c.variety_id WHERE 1=1 `
  158. if condition != "" {
  159. baseSql += condition
  160. }
  161. baseSql += ` GROUP BY a.variety_id,a."variety_name",a."production_day",a."last_update_sys_user_id",a."last_update_sys_user_real_name",a."modify_time",a."create_time" ) d `
  162. // 数据总数
  163. totalSql := `SELECT COUNT(1) total ` + baseSql
  164. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error
  165. if err != nil {
  166. return
  167. }
  168. // 列表页数据
  169. listSql := `SELECT * ` + baseSql + ` ORDER BY modify_time DESC,variety_id DESC LIMIT ?,?`
  170. pars = append(pars, startSize)
  171. pars = append(pars, pageSize)
  172. err = global.DmSQL["data"].Raw(listSql, pars...).Scan(&items).Error
  173. return
  174. }
  175. // Update 更新基础信息
  176. func (variety *Variety) Update(cols []string) (err error) {
  177. err = global.DmSQL["data"].Select(cols).Updates(variety).Error
  178. return
  179. }
  180. // Delete 删除
  181. func (variety *Variety) Delete() (err error) {
  182. err = global.DmSQL["data"].Delete(variety).Error
  183. return
  184. }
  185. type VarietyView struct {
  186. VarietyId int `orm:"column(future_good_chart_classify_id);pk" gorm:"primaryKey" `
  187. VarietyName string `description:"分类名称"`
  188. ParentId int `description:"父级id"`
  189. }