GAAS 广汽安道拓GFrame金属件MOM项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.0 KiB

  1. package models
  2. import (
  3. "errors"
  4. "leit.com/LAPP_GAAS_GFrame/db"
  5. "leit.com/LAPP_GAAS_GFrame/utils"
  6. "xorm.io/core"
  7. )
  8. type PmLocation struct {
  9. Finr int `json:"pm_location-finr" xorm:"not null pk INT(4)"`
  10. Locaid int `json:"pm_location-locaid" xorm:"not null pk INT(8)"`
  11. Locaname string `json:"pm_location-locaname" xorm:"not null VARCHAR(40)"`
  12. LocaCust1 string `json:"pm_location-loca_cust1" xorm:"not null VARCHAR(100)"`
  13. LocaCust2 string `json:"pm_location-loca_cust2" xorm:"not null VARCHAR(100)"`
  14. LocaCust3 string `json:"pm_location-loca_cust3" xorm:"not null VARCHAR(100)"`
  15. LocaCust4 string `json:"pm_location-loca_cust4" xorm:"not null VARCHAR(100)"`
  16. Lastmodif string `json:"pm_location-lastmodif" xorm:"not null VARCHAR(14)"`
  17. Lastuser string `json:"pm_location-lastuser" xorm:"not null VARCHAR(20)"`
  18. Credatuz string `json:"pm_location-credatuz" xorm:"not null VARCHAR(14)"`
  19. Valst []PmAsset `json:"valst" xorm:"-"`
  20. UnValst []PmAsset `json:"unvalst" xorm:"-"`
  21. Children []PmAsset `json:"children" xorm:"-"`
  22. }
  23. func (t *PmLocation) TableName() string {
  24. return "pm_location"
  25. }
  26. // 清除string字段的右侧空格
  27. func (t *PmLocation) Clipped() {
  28. utils.TrimStruct(t, *t)
  29. }
  30. //增
  31. func (t *PmLocation) Add() error {
  32. e := db.Eloquent.Master()
  33. countrole := new(PmLocation)
  34. affw, err := e.Table("pm_location").ID(core.PK{t.Finr, t.Locaid}).Count(countrole)
  35. if err != nil {
  36. return err
  37. }
  38. if affw > 0 {
  39. return errors.New("数据已经存在!")
  40. }
  41. _, err = e.Table("pm_location").Insert(t)
  42. if err != nil {
  43. return err
  44. }
  45. var Assetids []int
  46. for _, v := range t.Valst {
  47. Assetids = append(Assetids, v.Assetid)
  48. }
  49. if !utils.ValueIsEmpty(Assetids) {
  50. //批量更新
  51. _, err = e.Table("pm_asset").In("assetid", Assetids).Where("finr = ?", t.Finr).Update(map[string]interface{}{"locaid": t.Locaid})
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }
  58. //删
  59. func (t *PmLocation) Del() error {
  60. e := db.Eloquent.Master()
  61. _, err := e.ID(core.PK{t.Finr, t.Locaid}).Delete(&PmLocation{})
  62. if err != nil {
  63. return err
  64. }
  65. countrole := new(PmAsset)
  66. affw, _ := e.Table("pm_asset").Where("finr = ? and locaid = ?", t.Finr, t.Locaid).Count(countrole)
  67. if affw > 0 {
  68. //更新对应的资源定义表
  69. _, err = e.Table("pm_asset").Where("finr = ? and locaid = ?", t.Finr, t.Locaid).Update(map[string]interface{}{"locaid": 0})
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. return nil
  75. }
  76. //改
  77. func (t *PmLocation) Update() error {
  78. e := db.Eloquent.Master()
  79. _, err := e.Table("pm_location").Where("finr = ? and locaid = ?", t.Finr, t.Locaid).Update(t)
  80. if err != nil {
  81. return err
  82. }
  83. var Assetids []int
  84. for _, v := range t.Valst {
  85. Assetids = append(Assetids, v.Assetid)
  86. }
  87. if !utils.ValueIsEmpty(Assetids) {
  88. //批量更新
  89. _, err = e.Table("pm_asset").In("assetid", Assetids).Where("finr = ?", t.Finr).Update(map[string]interface{}{"locaid": t.Locaid})
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. var NAssetids []int
  95. for _, v := range t.UnValst {
  96. NAssetids = append(NAssetids, v.Assetid)
  97. }
  98. if !utils.ValueIsEmpty(NAssetids) {
  99. //批量更新
  100. _, err = e.Table("pm_asset").In("assetid", NAssetids).Where("finr = ?", t.Finr).Update(map[string]interface{}{"locaid": 0})
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. }
  107. //查
  108. func (t *PmLocation) SelectOne() (PmLocation, error) {
  109. e := db.Eloquent.Master()
  110. var data PmLocation
  111. _, err := e.ID(core.PK{t.Finr, t.Locaid}).Get(&data)
  112. if err != nil {
  113. return data, err
  114. }
  115. var valst []PmAsset
  116. err = e.Table("pm_asset").Where("finr = ? and locaid = ?", t.Finr, t.Locaid).Find(&valst)
  117. if err != nil {
  118. return data, err
  119. }
  120. var uvalst []PmAsset
  121. err = e.Table("pm_asset").Where("finr = ? and (locaid =0 or locaid = null)", t.Finr).Find(&uvalst)
  122. if err != nil {
  123. return data, err
  124. }
  125. data.Valst = valst
  126. data.UnValst = uvalst
  127. return data, nil
  128. }
  129. //查所有
  130. func (t *PmLocation) SelectAll() ([]PmAsset, error) {
  131. e := db.Eloquent.Master()
  132. var data []PmAsset
  133. err := e.Table("pm_asset").Where("finr = ? and (locaid =0 or locaid = null)", t.Finr).Find(&data)
  134. if err != nil {
  135. return data, err
  136. }
  137. return data, nil
  138. }
  139. //分页
  140. func (t *PmLocation) GetPage(descr string, pageSize int, pageIndex int) ([]PmLocation, int, error) {
  141. data := make([]PmLocation, 0)
  142. e := db.Eloquent.Master()
  143. table := e.Table("pm_location").Where("finr = ? ", t.Finr)
  144. parameters := make([]interface{}, 0)
  145. where := ""
  146. parameters = append(parameters, t.Finr)
  147. if descr != "" {
  148. table = table.And("locaname like concat('%',?,'%')", descr)
  149. where += " and locaname like concat('%',?,'%')"
  150. parameters = append(parameters, descr)
  151. }
  152. parameters = append(parameters, (pageIndex-1)*pageSize, pageSize)
  153. err := e.SQL("SELECT * FROM (SELECT * FROM pm_location where finr = ?"+where+") t order by locaid offset ? row fetch next ? row only", parameters...).Find(&data)
  154. if err != nil {
  155. return data, 0, err
  156. }
  157. count, err := table.Count(new(PmLocation))
  158. if err != nil {
  159. return data, 0, err
  160. }
  161. for k, _ := range data {
  162. data[k].Clipped()
  163. }
  164. return data, int(count), nil
  165. }
  166. type TreePmLocation struct {
  167. Locaid int `json:"pm_asset-locaid" xorm:"not null pk INT(8)"`
  168. Locaname string `json:"pm_asset-assetname" xorm:"not null VARCHAR(40)"`
  169. Children []PmAsset `json:"children" xorm:"-"`
  170. }
  171. type LocationAsset struct {
  172. PmAsset `xorm:"extends"`
  173. PmLocation `xorm:"extends"`
  174. }
  175. /**
  176. *填充资产树根节点为工厂第一层节点为location第二层节点为资产
  177. **************/
  178. func (t *PmLocation) TreeAsset() ([]TreePmLocation, error) {
  179. m := make([]TreePmLocation, 0)
  180. data := make([]LocationAsset, 0)
  181. e := db.Eloquent.Master()
  182. err := e.Table("pm_location").Join("left", "pm_asset", "pm_asset.finr=pm_location.finr and pm_asset.locaid=pm_location.locaid").Where("pm_location.finr = ? ", t.Finr).Find(&data)
  183. if err != nil {
  184. return m, err
  185. }
  186. tem := make(map[int]interface{})
  187. for i := 0; i < len(data); i++ {
  188. key := data[i].PmLocation.Locaid
  189. if utils.ValueIsEmpty(key) {
  190. continue
  191. }
  192. _, ok := tem[key]
  193. if ok {
  194. continue
  195. } else {
  196. tem[key] = data[i].PmLocation
  197. }
  198. val := TreePmLocation{}
  199. val.Locaid = data[i].PmLocation.Locaid
  200. val.Locaname = data[i].PmLocation.Locaname
  201. menusInfo := DiguiLocation(&data, val)
  202. m = append(m, menusInfo)
  203. }
  204. return m, nil
  205. }
  206. func DiguiLocation(locationlist *[]LocationAsset, location TreePmLocation) TreePmLocation {
  207. list := *locationlist
  208. min := make([]PmAsset, 0)
  209. for j := 0; j < len(list); j++ {
  210. if location.Locaid != list[j].PmLocation.Locaid {
  211. continue
  212. }
  213. mi := PmAsset{}
  214. mi.Locaid = list[j].PmAsset.Locaid
  215. mi.Finr = list[j].PmAsset.Finr
  216. mi.Descr = list[j].PmAsset.Descr
  217. mi.Assetid = list[j].PmAsset.Assetid
  218. mi.Assetname = list[j].PmAsset.Descr
  219. mi.Assetstatus = list[j].PmAsset.Assetstatus
  220. mi.Assettype = list[j].PmAsset.Assettype
  221. mi.Weekmodelid = list[j].PmAsset.Weekmodelid
  222. mi.Operator = list[j].PmAsset.Operator
  223. mi.Costcenterid = list[j].PmAsset.Costcenterid
  224. mi.Maker = list[j].PmAsset.Maker
  225. mi.Year = list[j].PmAsset.Year
  226. min = append(min, mi)
  227. }
  228. location.Children = min
  229. return location
  230. }