item.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package budget
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/utils"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type Item struct {
  11. ID int64 `json:"id" prop:"add:false"`
  12. TypeId int64 `json:"typeId" type:"int" prop:"add edit"`
  13. Name string `json:"name" label:"项目名称" type:"string" prop:"add edit" search:"like"`
  14. ParentIds string `json:"parentIds" label:"继承" type:"string" prop:"edit" default:""`
  15. PathIds string `json:"pathIds" type:"string" prop:"add:false"`
  16. Property string `json:"property" label:"属性" type:"string" prop:"edit"`
  17. OrderAt int64 `json:"order_at" prop:"add:false select:false"`
  18. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  19. CreatedAt int64 `json:"created_at" prop:"add:false select:false"`
  20. UpdatedAt int64 `json:"updated_at" prop:"add:false select:false"`
  21. Parents []Item `json:"parents" prop:"ignore"`
  22. Prop map[string]string `json:"prop" prop:"ignore"`
  23. ParentProp map[string]string `json:"parentProp" prop:"ignore"`
  24. ParentNames string `json:"parentNames" prop:"ignore"`
  25. db.BaseModel
  26. }
  27. func (Item) TableName() string {
  28. return "zy_budget_item"
  29. }
  30. func (model Item) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  31. if typeid, ok := data["typeId"]; ok {
  32. typid, _ := db.ToInt64(typeid)
  33. ids := AllItemTypes([]int64{typid})
  34. if len(ids) != 0 {
  35. s.Where = append(s.Where, fmt.Sprintf("`%s`.`typeId` in %s", model.TableName(), s.Param(ids)))
  36. }
  37. }
  38. typeIds := SilceIds(db.ToString(data["typeIds"]))
  39. if len(typeIds) > 0 {
  40. ids := AllItemTypes(typeIds)
  41. if len(ids) != 0 {
  42. s.Where = append(s.Where, fmt.Sprintf("`%s`.`typeId` in %s", model.TableName(), s.Param(ids)))
  43. }
  44. }
  45. excludeIds := SilceIds(db.ToString(data["exclude_ids"]))
  46. if len(excludeIds) != 0 {
  47. s.Where = append(s.Where, fmt.Sprintf("`%s`.`id` not in %s", model.TableName(), s.Param(excludeIds)))
  48. }
  49. return true
  50. }
  51. func (model Item) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} {
  52. //ids := make([]int64, 0)
  53. pathIds := make([]int64, 0)
  54. for n, item := range list {
  55. //id, _ := db.ToInt64(item["id"])
  56. //ids = append(ids, id)
  57. pathIds = append(pathIds, SilceIds(db.ToString(item["pathIds"]))...)
  58. prop := make(map[string]string)
  59. utils.JsonDecode(db.ToString(item["property"])).To(&prop)
  60. list[n]["prop"] = prop
  61. }
  62. var parents []Item
  63. db.GetModel(map[string]interface{}{
  64. "id in": pathIds,
  65. "deleted_at": 0,
  66. }, &parents)
  67. for n, item := range list {
  68. list[n]["parents"], list[n]["parentProp"], list[n]["parentNames"] = findParentItems(db.ToString(item["parentIds"]), parents)
  69. }
  70. return list
  71. }
  72. func (Item) OnePrivilege(c *gin.Context, id int64) bool {
  73. return true
  74. }
  75. func (Item) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  76. if pathIds, ok := ItemParentIdsToPathIds(db.ToString(data["parentIds"]), true); ok {
  77. data["pathIds"] = pathIds
  78. } else {
  79. return errors.New("没有权限")
  80. }
  81. return nil
  82. }
  83. func (Item) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  84. if _, ok := data["parentIds"]; ok {
  85. if pathIds, ok := ItemParentIdsToPathIds(db.ToString(data["parentIds"]), true); ok {
  86. for _, pid := range SilceIds(pathIds) {
  87. if pid == id {
  88. return errors.New("没有权限")
  89. }
  90. }
  91. data["pathIds"] = pathIds
  92. } else {
  93. return errors.New("没有权限")
  94. }
  95. }
  96. return nil
  97. }
  98. func (Item) EditAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
  99. if _, ok := data["parentIds"]; ok {
  100. var items []Item
  101. db.GetModel(map[string]interface{}{
  102. "pathIds find_in_set": id,
  103. "deleted_at": 0,
  104. }, &items)
  105. UpdateItemPathIds(id, items)
  106. }
  107. }
  108. func (Item) DelPrivilege(c *gin.Context, id int64) error {
  109. return nil
  110. }
  111. func (Item) OrderField() string {
  112. return "order_at"
  113. }
  114. func (Item) Page() bool {
  115. return false
  116. }
  117. func (Item) Count() bool {
  118. return true
  119. }
  120. func (model Item) GetPathIds() []int64 {
  121. return SilceIds(model.PathIds)
  122. }
  123. func (model Item) GetParentIds() []int64 {
  124. return SilceIds(model.ParentIds)
  125. }
  126. func (model Item) GetProperty() map[string]string {
  127. props := make(map[string]string)
  128. utils.JsonDecode(model.Property).To(&props)
  129. return props
  130. }
  131. func (model Item) GetID() int64 {
  132. return model.ID
  133. }
  134. func (model Item) GetProp(name string) (expression string, ok bool) {
  135. expression, ok = model.Prop[name]
  136. if !ok {
  137. expression, ok = model.ParentProp[name]
  138. }
  139. return
  140. }
  141. func (model Item) GetSubs() *[]QuoteData {
  142. return nil
  143. }
  144. func (model Item) GetName() string {
  145. return model.Name
  146. }
  147. func (model Item) GetHeaders() *[]QuoteData {
  148. return nil
  149. }
  150. func (model Item) GetItem() *Item {
  151. return nil
  152. }
  153. func (model Item) GetValue() string {
  154. return ""
  155. }
  156. func (model Item) GetQuote() *Quote {
  157. return nil
  158. }
  159. func (model Item) GetTable() *Table {
  160. return nil
  161. }
  162. func (model Item) GetHeader() *Header {
  163. return nil
  164. }
  165. func (model Item) GetModule() *Module {
  166. return nil
  167. }
  168. func (model Item) GetGroup() *Group {
  169. return nil
  170. }
  171. func (model Item) GetRow() *Row {
  172. return nil
  173. }
  174. func (model Item) GetMyItem() *Item {
  175. return &model
  176. }
  177. func SilceIds(sids string) []int64 {
  178. ids := make([]int64, 0)
  179. for _, v := range strings.Split(sids, ",") {
  180. if p, ok := db.ToInt64(v); ok && p != 0 {
  181. ids = append(ids, p)
  182. }
  183. }
  184. return ids
  185. }
  186. func ItemParentIdsToPathIds(parentIds string, isOk bool) (string, bool) {
  187. pids := SilceIds(parentIds)
  188. if len(pids) == 0 {
  189. return "", true
  190. }
  191. var pitems []Item
  192. db.GetModel(map[string]interface{}{
  193. "id in": pids,
  194. "deleted_at": 0,
  195. }, &pitems)
  196. if len(pids) != len(pitems) && isOk {
  197. return "", false
  198. }
  199. pathids := make([]int64, 0)
  200. pathids = append(pathids, pids...)
  201. for _, v := range pitems {
  202. for _, p := range v.GetPathIds() {
  203. mid := int64(0)
  204. for i, id := range pathids {
  205. mid = id
  206. if p == id {
  207. break
  208. }
  209. if p < id {
  210. pathids = append(pathids[:i], append([]int64{p}, pathids[i:]...)...)
  211. break
  212. }
  213. }
  214. if p > mid {
  215. pathids = append(pathids, p)
  216. }
  217. }
  218. }
  219. pathidss := make([]string, 0)
  220. for _, id := range pathids {
  221. pathidss = append(pathidss, db.ToString(id))
  222. }
  223. return strings.Join(pathidss, ","), true
  224. }
  225. func UpdateItemPathIds(id int64, items []Item) {
  226. for _, v := range items {
  227. isChild := false
  228. for _, pid := range SilceIds(v.ParentIds) {
  229. if pid == id {
  230. isChild = true
  231. break
  232. }
  233. }
  234. if isChild {
  235. pathIds, _ := ItemParentIdsToPathIds(v.ParentIds, false)
  236. db.UpdateModel(db.Type(items), v.ID, map[string]interface{}{
  237. "pathIds": pathIds,
  238. })
  239. UpdateItemPathIds(v.ID, items)
  240. }
  241. }
  242. }
  243. func findParentItems(parentIds string, list []Item) ([]Item, map[string]string, string) {
  244. items := make([]Item, 0)
  245. prop := make(map[string]string)
  246. names := make([]string, 0)
  247. for _, pid := range SilceIds(parentIds) {
  248. for _, v := range list {
  249. if v.ID == pid {
  250. v.Parents, v.ParentProp, v.ParentNames = findParentItems(v.ParentIds, list)
  251. v.Prop = v.GetProperty()
  252. items = append(items, v)
  253. for k, v := range v.ParentProp {
  254. prop[k] = v
  255. }
  256. for k, v := range v.Prop {
  257. prop[k] = v
  258. }
  259. names = append(names, v.Name)
  260. }
  261. }
  262. }
  263. return items, prop, strings.Join(names, ",")
  264. }
  265. func GetItemModels(where map[string]interface{}) (items []Item, datas []QuoteData) {
  266. db.GetModel(where, &items)
  267. pathIds := make([]int64, 0)
  268. for _, item := range items {
  269. pathIds = append(pathIds, SilceIds(item.PathIds)...)
  270. }
  271. var parents []Item
  272. db.GetModel(map[string]interface{}{
  273. "id in": pathIds,
  274. "deleted_at": 0,
  275. }, &parents)
  276. for i, item := range items {
  277. items[i].Parents, items[i].ParentProp, items[i].ParentNames = findParentItems(item.ParentIds, parents)
  278. items[i].Prop = item.GetProperty()
  279. datas = append(datas, items[i])
  280. }
  281. return
  282. }