mdm.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package gn
  2. import (
  3. "eta_gn/eta_bridge/global"
  4. "eta_gn/eta_bridge/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // MDM_COMSTRU MDM组织机构
  10. type MDM_COMSTRU struct {
  11. Id int `gorm:"column:Id;primaryKey;autoIncrement" json:"-"`
  12. MainFieldComstruCode string `gorm:"column:Main_Field_Comstru_Code" json:"Main_Field_Comstru_Code"`
  13. MainFieldComstruCodeHR string `gorm:"column:Main_Field_Comstru_Code_HR" json:"Main_Field_Comstru_Code_HR"`
  14. MainFieldComstruTypeHR string `gorm:"column:Main_Field_Comstru_Type_HR" json:"Main_Field_Comstru_Type_HR"`
  15. ZDWLX string `gorm:"column:ZDWLX" json:"ZDWLX"`
  16. MainFieldComstruNameHR string `gorm:"column:Main_Field_Comstru_Name_HR" json:"Main_Field_Comstru_Name_HR"`
  17. MainFieldComstruShortNameHR string `gorm:"column:Main_Field_Comstru_Short_Name_HR" json:"Main_Field_Comstru_Short_Name_HR"`
  18. MainFieldParentAdminComstruCodeHR string `gorm:"column:Main_Field_ParentAdmin_Comstru_Code_HR" json:"Main_Field_ParentAdmin_Comstru_Code_HR"`
  19. MainFieldParentAdminComstruCode string `gorm:"column:Main_Field_ParentAdmin_Comstru_Code" json:"Main_Field_ParentAdmin_Comstru_Code"`
  20. MainFieldParentAdminComstruName string `gorm:"column:Main_Field_ParentAdmin_Comstru_Name" json:"Main_Field_ParentAdmin_Comstru_Name"`
  21. MainFieldCompanyLevel string `gorm:"column:Main_Field_Company_Level" json:"Main_Field_Company_Level"`
  22. MainFieldSecondcomstrucodehr string `gorm:"column:Main_Field_Secondcomstrucodehr" json:"Main_Field_Secondcomstrucodehr"`
  23. MainFieldShBusinessType string `gorm:"column:Main_Field_Sh_Business_Type" json:"Main_Field_Sh_Business_Type"`
  24. MainFieldYZGDWZCNDGX string `gorm:"column:Main_Field_YZGDWZCNDGX" json:"Main_Field_YZGDWZCNDGX"`
  25. MainFieldOrderHR string `gorm:"column:Main_Field_Order_HR" json:"Main_Field_Order_HR"`
  26. MainFieldCompanyCode string `gorm:"column:Main_Field_Company_Code" json:"Main_Field_Company_Code"`
  27. MainFieldCompanyChain string `gorm:"column:Main_Field_Company_Chain" json:"Main_Field_Company_Chain"`
  28. MainFieldPARENTCOMPANYVIRTUAL string `gorm:"column:Main_Field_PARENTCOMPANYVIRTUAL" json:"Main_Field_PARENTCOMPANYVIRTUAL"`
  29. ZQYZT string `gorm:"column:ZQYZT" json:"ZQYZT"`
  30. CreateTime time.Time `gorm:"column:create_time" json:"create_time"`
  31. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"`
  32. }
  33. func (m *MDM_COMSTRU) TableName() string {
  34. return "MDM_COMSTRU"
  35. }
  36. func (m *MDM_COMSTRU) Create() (err error) {
  37. err = global.MYSQL["hz_eta"].Create(m).Error
  38. return
  39. }
  40. func (m *MDM_COMSTRU) Update(cols []string) (err error) {
  41. err = global.MYSQL["hz_eta"].Select(cols).Updates(m).Error
  42. return
  43. }
  44. func (m *MDM_COMSTRU) GetItemByCondition(condition string, pars []interface{}) (item *MDM_COMSTRU, err error) {
  45. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  46. err = global.MYSQL["hz_eta"].Raw(sql, pars...).First(&item).Error
  47. return
  48. }
  49. func (m *MDM_COMSTRU) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*MDM_COMSTRU, err error) {
  50. fields := strings.Join(fieldArr, ",")
  51. if len(fieldArr) == 0 {
  52. fields = `*`
  53. }
  54. order := ``
  55. if orderRule != "" {
  56. order = ` ORDER BY ` + orderRule
  57. }
  58. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  59. err = global.MYSQL["hz_eta"].Raw(sql, pars...).Find(&items).Error
  60. return
  61. }
  62. // MDM_EMPLOYEE MDM员工表
  63. type MDM_EMPLOYEE struct {
  64. Id int `gorm:"column:Id;primaryKey;autoIncrement" json:"-"`
  65. MainFieldEmployeeCode string `gorm:"column:Main_Field_Employee_Code" json:"Main_Field_Employee_Code"`
  66. MainFieldEmployeeFirstName string `gorm:"column:Main_Field_Employee_FirstName" json:"Main_Field_Employee_FirstName"`
  67. MainFieldEmployeeLastName string `gorm:"column:Main_Field_Employee_LastName" json:"Main_Field_Employee_LastName"`
  68. MainFieldGender string `gorm:"column:Main_Field_Gender" json:"Main_Field_Gender"`
  69. MainFieldEmpolyeeGroup string `gorm:"column:Main_Field_Empolyee_Group" json:"Main_Field_Empolyee_Group"`
  70. MainFieldOfficeTelephone string `gorm:"column:Main_Field_Office_Telephone" json:"Main_Field_Office_Telephone"`
  71. MainFieldDateType string `gorm:"column:Main_Field_Date_Type" json:"Main_Field_Date_Type"`
  72. MainFieldEmployeeStatus string `gorm:"column:Main_Field_Employee_Status" json:"Main_Field_Employee_Status"`
  73. MainFieldDeleteFlag string `gorm:"column:Main_Field_Delete_Flag" json:"Main_Field_Delete_Flag"`
  74. MainFieldEmail string `gorm:"column:Main_Field_Email" json:"Main_Field_Email"`
  75. MainFieldCompanyCode string `gorm:"column:Main_Field_Company_code" json:"Main_Field_Company_code"`
  76. MainFieldSecondcomstrucodehr string `gorm:"column:Main_Field_Secondcomstrucodehr" json:"Main_Field_Secondcomstrucodehr"`
  77. MainFieldCompanyChain string `gorm:"column:Main_Field_Company_Chain" json:"Main_Field_Company_Chain"`
  78. CreateTime time.Time `gorm:"column:create_time" json:"create_time"`
  79. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"`
  80. }
  81. func (m *MDM_EMPLOYEE) TableName() string {
  82. return "MDM_EMPLOYEE"
  83. }
  84. func (m *MDM_EMPLOYEE) Create() (err error) {
  85. err = global.MYSQL["hz_eta"].Create(m).Error
  86. return
  87. }
  88. func (m *MDM_EMPLOYEE) Update(cols []string) (err error) {
  89. err = global.MYSQL["hz_eta"].Select(cols).Updates(m).Error
  90. return
  91. }
  92. func (m *MDM_EMPLOYEE) GetItemByCondition(condition string, pars []interface{}) (item *MDM_EMPLOYEE, err error) {
  93. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  94. err = global.MYSQL["hz_eta"].Raw(sql, pars...).First(&item).Error
  95. return
  96. }
  97. func (m *MDM_EMPLOYEE) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*MDM_EMPLOYEE, err error) {
  98. fields := strings.Join(fieldArr, ",")
  99. if len(fieldArr) == 0 {
  100. fields = `*`
  101. }
  102. order := ``
  103. if orderRule != "" {
  104. order = ` ORDER BY ` + orderRule
  105. }
  106. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  107. err = global.MYSQL["hz_eta"].Raw(sql, pars...).Find(&items).Error
  108. return
  109. }
  110. // CreateEmployee 新增员工及组织关系
  111. func (m *MDM_EMPLOYEE) CreateEmployee(employee *MDM_EMPLOYEE, employeeRelation []*MDM_RELA_COMSTRU_EMPLOYEE) (err error) {
  112. if employee == nil {
  113. err = fmt.Errorf("employee is nil")
  114. return
  115. }
  116. tx := global.MYSQL["hz_eta"].Begin()
  117. defer func() {
  118. if err != nil {
  119. _ = tx.Rollback()
  120. return
  121. }
  122. _ = tx.Commit()
  123. }()
  124. if e := tx.Create(employee).Error; e != nil {
  125. err = fmt.Errorf("create employee err, %v", e)
  126. return
  127. }
  128. if len(employeeRelation) == 0 {
  129. return
  130. }
  131. if e := tx.CreateInBatches(employeeRelation, utils.MultiAddNum).Error; e != nil {
  132. err = fmt.Errorf("create employee relations err, %v", e)
  133. return
  134. }
  135. return
  136. }
  137. // UpdateEmployee 更新员工及组织关系
  138. func (m *MDM_EMPLOYEE) UpdateEmployee(employee *MDM_EMPLOYEE, updateCols []string, employeeRelation []*MDM_RELA_COMSTRU_EMPLOYEE) (err error) {
  139. if employee == nil {
  140. err = fmt.Errorf("employee is nil")
  141. return
  142. }
  143. tx := global.MYSQL["hz_eta"].Begin()
  144. defer func() {
  145. if err != nil {
  146. _ = tx.Rollback()
  147. return
  148. }
  149. _ = tx.Commit()
  150. }()
  151. if e := tx.Select(updateCols).Updates(employee).Error; e != nil {
  152. err = fmt.Errorf("update employee err, %v", e)
  153. return
  154. }
  155. sql := `DELETE FROM MDM_RELA_COMSTRU_EMPLOYEE WHERE Main_Field_Employee_Code = ?`
  156. if e := tx.Exec(sql, employee.MainFieldEmployeeCode); e != nil {
  157. err = fmt.Errorf("delete employee relations err, %v", e)
  158. return
  159. }
  160. if len(employeeRelation) == 0 {
  161. return
  162. }
  163. if e := tx.CreateInBatches(employeeRelation, utils.MultiAddNum).Error; e != nil {
  164. err = fmt.Errorf("create employee relations err, %v", e)
  165. return
  166. }
  167. return
  168. }
  169. // MDM_RELA_COMSTRU_EMPLOYEE MDM员工组织关系表
  170. type MDM_RELA_COMSTRU_EMPLOYEE struct {
  171. Id int `gorm:"column:Id;primaryKey;autoIncrement" json:"-"`
  172. Main_Field_Employee_Code string `gorm:"column:Main_Field_Employee_Code" json:"Main_Field_Employee_Code"`
  173. TuplesFieldCompanyCode string `gorm:"column:Tuples_Field_Company_Code" json:"Tuples_Field_Company_Code"`
  174. TuplesFieldCompanyName string `gorm:"column:Tuples_Field_Company_Name" json:"Tuples_Field_Company_Name"`
  175. TuplesFieldDepartmentCode string `gorm:"column:Tuples_Field_Department_Code" json:"Tuples_Field_Department_Code"`
  176. TuplesFieldDepartmentName string `gorm:"column:Tuples_Field_Department_Name" json:"Tuples_Field_Department_Name"`
  177. TuplesFieldPositionCode string `gorm:"column:Tuples_Field_Position_Code" json:"Tuples_Field_Position_Code"`
  178. TuplesFieldPositionName string `gorm:"column:Tuples_Field_Position_Name" json:"Tuples_Field_Position_Name"`
  179. TuplesFieldPostFlag string `gorm:"column:Tuples_Field_Post_Flag" json:"Tuples_Field_Post_Flag"`
  180. CreateTime time.Time `gorm:"column:create_time" json:"create_time"`
  181. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"`
  182. }
  183. func (m *MDM_RELA_COMSTRU_EMPLOYEE) TableName() string {
  184. return "MDM_RELA_COMSTRU_EMPLOYEE"
  185. }