variety.go 7.0 KB

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