business.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/manager/model"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // AddBusiness .
  11. func (d *Dao) AddBusiness(c context.Context, b *model.Business) (err error) {
  12. maxBid := int64(-1)
  13. if b.PID != 0 {
  14. if maxBid, err = d.maxBid(c); err != nil {
  15. return
  16. }
  17. }
  18. valueStrings := []string{}
  19. valueArgs := []interface{}{}
  20. for _, name := range b.Names {
  21. maxBid++
  22. valueStrings = append(valueStrings, "(?,?,?,?,?,?)")
  23. valueArgs = append(valueArgs, b.PID, maxBid, name, b.Flow, b.FlowState, b.State)
  24. }
  25. stmt := fmt.Sprintf("INSERT INTO manager_business(pid,bid,name,flow,flow_state,state) VALUES %s ON DUPLICATE KEY UPDATE pid=values(pid),name=values(name),flow=values(flow),flow_state=values(flow_state),state=values(state)", strings.Join(valueStrings, ","))
  26. err = d.db.Exec(stmt, valueArgs...).Error
  27. return
  28. }
  29. // maxBidByPid .
  30. func (d *Dao) maxBid(c context.Context) (maxBid int64, err error) {
  31. err = d.db.Table("manager_business").Select("max(bid)").Row().Scan(&maxBid)
  32. return
  33. }
  34. // UpdateBusiness .
  35. func (d *Dao) UpdateBusiness(c context.Context, b *model.Business) error {
  36. return d.db.Table("manager_business").Where("id = ?", b.ID).
  37. Update(map[string]interface{}{
  38. "name": b.Name,
  39. "flow": b.Flow,
  40. }).Error
  41. }
  42. // BusinessChilds .
  43. func (d *Dao) BusinessChilds(c context.Context, pid int64) (res []*model.Business, err error) {
  44. err = d.db.Table("manager_business").Where("pid = ?", pid).Find(&res).Error
  45. return
  46. }
  47. // AddRole .
  48. func (d *Dao) AddRole(c context.Context, br *model.BusinessRole) (err error) {
  49. stmt := fmt.Sprintf("INSERT INTO manager_business_role(bid,rid,name,type,state) VALUES %s ON DUPLICATE KEY UPDATE bid=values(bid),name=values(name),type=values(type),state=values(state)", "(?,?,?,?,?)")
  50. err = d.db.Exec(stmt, br.BID, br.RID, br.Name, br.Type, br.State).Error
  51. return
  52. }
  53. // MaxRidByBid .
  54. func (d *Dao) MaxRidByBid(c context.Context, bid int64) (maxRid interface{}, err error) {
  55. err = d.db.Table("manager_business_role").Select("max(rid) as mrid").Where("bid = ?", bid).Row().Scan(&maxRid)
  56. return
  57. }
  58. // UpdateRole .
  59. func (d *Dao) UpdateRole(c context.Context, br *model.BusinessRole) error {
  60. return d.db.Table("manager_business_role").Where("id = ?", br.ID).
  61. Update("name", br.Name).Error
  62. }
  63. // RoleByRIDs .
  64. func (d *Dao) RoleByRIDs(c context.Context, bid int64, rids []int64) (res map[int64]*model.BusinessRole, err error) {
  65. t := []*model.BusinessRole{}
  66. res = make(map[int64]*model.BusinessRole)
  67. err = d.db.Where("rid IN (?)", rids).Where("bid = ?", bid).Find(&t).Error
  68. for _, r := range t {
  69. res[r.RID] = r
  70. }
  71. return
  72. }
  73. // AddUser add users with associated roles .
  74. func (d *Dao) AddUser(c context.Context, bur *model.BusinessUserRole) error {
  75. valueStrings := []string{}
  76. valueArgs := []interface{}{}
  77. for _, uid := range bur.UIDs {
  78. valueStrings = append(valueStrings, "(?,?,?,?)")
  79. valueArgs = append(valueArgs, uid, bur.CUID, bur.BID, bur.Role)
  80. }
  81. stmt := fmt.Sprintf("INSERT INTO manager_business_user_role(uid, cuid, bid, role) VALUES %s ON DUPLICATE KEY UPDATE uid=values(uid),cuid=values(cuid),bid=values(bid),role=values(role)", strings.Join(valueStrings, ","))
  82. return d.db.Exec(stmt, valueArgs...).Error
  83. }
  84. // UpdateUser .
  85. func (d *Dao) UpdateUser(c context.Context, bur *model.BusinessUserRole) error {
  86. return d.db.Table("manager_business_user_role").Where("id = ?", bur.ID).
  87. Update("role", bur.Role).Error
  88. }
  89. // UpdateBusinessState .
  90. func (d *Dao) UpdateBusinessState(c context.Context, su *model.StateUpdate) error {
  91. return d.db.Table("manager_business").Where("id = ?", su.ID).
  92. Update("state", su.State).Error
  93. }
  94. // BatchUpdateChildState .
  95. func (d *Dao) BatchUpdateChildState(c context.Context, pid int64, flowStates []int64) error {
  96. return d.db.Table("manager_business").Where("flow_state NOT IN (?)", flowStates).Where("pid = ?", pid).
  97. Update("state", 0).Error
  98. }
  99. // UpdateBusinessRoleState .
  100. func (d *Dao) UpdateBusinessRoleState(c context.Context, su *model.StateUpdate) error {
  101. return d.db.Table("manager_business_role").Where("id = ?", su.ID).
  102. Update("state", su.State).Error
  103. }
  104. // ParentBusiness .
  105. func (d *Dao) ParentBusiness(c context.Context, state int64) (res map[int64]*model.BusinessList, err error) {
  106. temp := []*model.BusinessList{}
  107. res = make(map[int64]*model.BusinessList)
  108. db := d.db.Where("pid = ?", 0)
  109. if state != -1 {
  110. db = db.Where("state = ?", state)
  111. }
  112. if err = db.Order("id asc", true).Find(&temp).Error; err != nil {
  113. return
  114. }
  115. for _, t := range temp {
  116. res[t.ID] = t
  117. }
  118. return
  119. }
  120. // ChildBusiness .
  121. func (d *Dao) ChildBusiness(c context.Context, bp *model.BusinessListParams) (res map[int64]*model.BusinessList, err error) {
  122. temp := []*model.BusinessList{}
  123. res = make(map[int64]*model.BusinessList)
  124. db := d.db.Where("pid != ?", 0)
  125. if bp.State != -1 {
  126. db = db.Where("state = ?", bp.State)
  127. }
  128. if bp.Flow != 0 {
  129. db = db.Where("flow_state = ?", bp.Flow)
  130. }
  131. err = db.Order("ctime desc").Find(&temp).Error
  132. for _, t := range temp {
  133. res[t.ID] = t
  134. }
  135. return
  136. }
  137. // ChildBusinessByPIDs .
  138. func (d *Dao) ChildBusinessByPIDs(c context.Context, pids []int64) (res map[int64][]*model.BusinessList, err error) {
  139. res = make(map[int64][]*model.BusinessList)
  140. temp := []*model.BusinessList{}
  141. if err = d.db.Where("pid IN (?)", pids).Find(&temp).Error; err != nil {
  142. return
  143. }
  144. for _, t := range temp {
  145. res[t.PID] = append(res[t.PID], t)
  146. }
  147. return
  148. }
  149. // FlowList .
  150. func (d *Dao) FlowList(c context.Context, bp *model.BusinessListParams) (res []*model.BusinessList, err error) {
  151. db := d.db.Where("pid != ?", 0)
  152. if bp.Flow > 0 {
  153. db = db.Where("flow_state = ?", bp.Flow)
  154. }
  155. if bp.State != -1 {
  156. db = db.Where("state = ?", bp.State)
  157. }
  158. err = db.Find(&res).Error
  159. return
  160. }
  161. // RoleListByBID .
  162. func (d *Dao) RoleListByBID(c context.Context, br *model.BusinessRole) (res []*model.BusinessRole, err error) {
  163. db := d.db
  164. if br.BID > 0 {
  165. db = db.Where("bid = ?", br.BID)
  166. }
  167. if br.Type != -1 {
  168. db = db.Where("type = ?", br.Type)
  169. }
  170. if br.State != -1 {
  171. db = db.Where("state = ?", br.State)
  172. }
  173. err = db.Order("rid desc").Find(&res).Error
  174. return
  175. }
  176. // RoleListByRIDs .
  177. func (d *Dao) RoleListByRIDs(c context.Context, bid int64, rids []int64) (res []*model.BusinessRole, err error) {
  178. err = d.DB().Table("manager_business_role").Where("bid = ? AND rid IN (?)", bid, rids).Where("state = ?", 1).Find(&res).Error
  179. return
  180. }
  181. // UserList .
  182. func (d *Dao) UserList(c context.Context, u *model.UserListParams) (res []*model.BusinessUserRoleList, err error) {
  183. db := d.db.Table("manager_business_user_role")
  184. if u.BID > 0 {
  185. db = db.Where("bid = ?", u.BID)
  186. }
  187. if u.UID > 0 {
  188. db = db.Where("uid = ?", u.UID)
  189. }
  190. if u.Role != -1 {
  191. db = db.Where("find_in_set(?, role) > 0", strconv.FormatInt(u.Role, 10))
  192. }
  193. err = db.Where("state = ?", model.UserOnState).Find(&res).Error
  194. return
  195. }
  196. // DeleteUser .
  197. func (d *Dao) DeleteUser(c context.Context, bur *model.BusinessUserRole) error {
  198. return d.db.Table("manager_business_user_role").Where("id = ?", bur.ID).Update("state", model.UserOffState).Error
  199. }
  200. // BusinessByID .
  201. func (d *Dao) BusinessByID(c context.Context, id int64) (res *model.BusinessList, err error) {
  202. res = &model.BusinessList{}
  203. if err = d.db.Where("id = ?", id).First(res).Error; err == gorm.ErrRecordNotFound {
  204. err = nil
  205. }
  206. return
  207. }
  208. // UserRoles .
  209. func (d *Dao) UserRoles(c context.Context, uid int64) (res []*model.BusinessUserRoleList, err error) {
  210. err = d.db.Table("manager_business_user_role").Where("uid = ?", uid).Find(&res).Error
  211. return
  212. }
  213. // UserRoleByBIDs .
  214. func (d *Dao) UserRoleByBIDs(c context.Context, uid int64, bids []int64) (res []*model.BusinessUserRoleList, err error) {
  215. err = d.DB().Table("manager_business_user_role").Where("bid IN (?)", bids).Where("uid = ? AND state = ?", uid, 1).Find(&res).Error
  216. return
  217. }