user.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package gn
  2. import (
  3. "eta_gn/eta_bridge/controller/resp"
  4. gnLogic "eta_gn/eta_bridge/logic/gn"
  5. "eta_gn/eta_bridge/models/gn"
  6. gnRequest "eta_gn/eta_bridge/models/request/gn"
  7. "eta_gn/eta_bridge/utils"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. "time"
  12. )
  13. // 同步用户锁,防止重复同步,不管是全量还是增量,都是同一时间只能一个同步
  14. //var lockSyncUser sync.Mutex
  15. // SyncUser 临时-全量同步用户
  16. //func (gc *GuoNengController) SyncUser(c *gin.Context) {
  17. // lockSyncUser.Lock()
  18. // defer func() {
  19. // lockSyncUser.Unlock()
  20. // }()
  21. // err := gnLogic.SyncUser()
  22. // if err != nil {
  23. // resp.FailData("同步失败", err.Error(), c)
  24. // return
  25. // }
  26. // resp.Ok("同步成功", c)
  27. //}
  28. // AddAccount 创建新账号
  29. func (gc *GuoNengController) AddAccount(c *gin.Context) {
  30. var req gnRequest.AddAccountReq
  31. if e := c.Bind(&req); e != nil {
  32. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", fmt.Sprintf("参数解析失败, %v", e), c)
  33. return
  34. }
  35. if req.UserId == nil || (req.UserId != nil && *req.UserId == "") {
  36. resp.Gn4AResultData(resp.FAIL_CODE, "登录账号标识不可为空", "登录账号标识不可为空", c)
  37. return
  38. }
  39. if req.FirstName == nil || (req.FirstName != nil && *req.FirstName == "") {
  40. resp.Gn4AResultData(resp.FAIL_CODE, "姓名不可为空", "姓名不可为空", c)
  41. return
  42. }
  43. if req.LastName == nil || (req.LastName != nil && *req.LastName == "") {
  44. resp.Gn4AResultData(resp.FAIL_CODE, "姓名不可为空", "姓名不可为空", c)
  45. return
  46. }
  47. if req.UserStatus == nil || (req.UserStatus != nil && *req.UserStatus == "") {
  48. resp.Gn4AResultData(resp.FAIL_CODE, "账号状态不可为空", "账号状态不可为空", c)
  49. return
  50. }
  51. if req.Password == nil || (req.Password != nil && *req.Password == "") {
  52. resp.Gn4AResultData(resp.FAIL_CODE, "密码不可为空", "密码不可为空", c)
  53. return
  54. }
  55. if req.UserStatus != nil {
  56. statsMap := map[string]int{gn.UserStatusActive: 1, gn.UserStatusDisable: 0}
  57. if _, ok := statsMap[*req.UserStatus]; !ok {
  58. resp.Gn4AResultData(resp.FAIL_CODE, "枚举值异常: UserStatus", fmt.Sprintf("枚举值异常, UserStatus: %s", *req.UserStatus), c)
  59. return
  60. }
  61. }
  62. if req.EmpNumber == nil || (req.EmpNumber != nil && *req.EmpNumber == "") {
  63. resp.Gn4AResultData(resp.FAIL_CODE, "员工编号不可为空", "员工编号不可为空", c)
  64. return
  65. }
  66. // 新增账号
  67. errMsg, e := gnLogic.AddAccount(req.UserEntity)
  68. if e != nil {
  69. resp.Gn4AResultData(resp.FAIL_CODE, errMsg, fmt.Sprintf("AddAccount: %v", e), c)
  70. return
  71. }
  72. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  73. }
  74. // ModifyAccount 修改账号信息
  75. func (gc *GuoNengController) ModifyAccount(c *gin.Context) {
  76. var req gnRequest.ModifyAccountReq
  77. if e := c.Bind(&req); e != nil {
  78. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", fmt.Sprintf("参数解析失败, %v", e), c)
  79. return
  80. }
  81. if req.UserId == nil || (req.UserId != nil && *req.UserId == "") {
  82. resp.Gn4AResultData(resp.FAIL_CODE, "登录账号标识不可为空", "登录账号标识不可为空", c)
  83. return
  84. }
  85. //if req.FirstName == nil || (req.FirstName != nil && *req.FirstName == "") {
  86. // resp.Gn4AResultData(resp.FAIL_CODE, "姓名不可为空", "姓名不可为空", c)
  87. // return
  88. //}
  89. //if req.LastName == nil || (req.LastName != nil && *req.LastName == "") {
  90. // resp.Gn4AResultData(resp.FAIL_CODE, "姓名不可为空", "姓名不可为空", c)
  91. // return
  92. //}
  93. //if req.UserStatus == nil || (req.UserStatus != nil && *req.UserStatus == "") {
  94. // resp.Gn4AResultData(resp.FAIL_CODE, "账号状态不可为空", "账号状态不可为空", c)
  95. // return
  96. //}
  97. //if req.Password == nil || (req.Password != nil && *req.Password == "") {
  98. // resp.Gn4AResultData(resp.FAIL_CODE, "密码不可为空", "密码不可为空", c)
  99. // return
  100. //}
  101. if req.UserStatus != nil {
  102. statsMap := map[string]int{gn.UserStatusActive: 1, gn.UserStatusDisable: 0}
  103. if _, ok := statsMap[*req.UserStatus]; !ok {
  104. resp.Gn4AResultData(resp.FAIL_CODE, "枚举值异常: UserStatus", fmt.Sprintf("枚举值异常, UserStatus: %s", *req.UserStatus), c)
  105. return
  106. }
  107. }
  108. //if req.EmpNumber == nil || (req.EmpNumber != nil && *req.EmpNumber == "") {
  109. // resp.Gn4AResultData(resp.FAIL_CODE, "员工编号不可为空", "员工编号不可为空", c)
  110. // return
  111. //}
  112. // 更新账号
  113. errMsg, e := gnLogic.ModifyAccount(req.UserEntity)
  114. if e != nil {
  115. resp.Gn4AResultData(resp.FAIL_CODE, errMsg, fmt.Sprintf("ModifyAccount: %v", e), c)
  116. return
  117. }
  118. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  119. }
  120. // DeleteAccount 删除账号
  121. func (gc *GuoNengController) DeleteAccount(c *gin.Context) {
  122. var req gnRequest.DeleteAccountReq
  123. if e := c.Bind(&req); e != nil {
  124. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", fmt.Sprintf("参数解析失败, %v", e), c)
  125. return
  126. }
  127. if req.UserId == "" {
  128. resp.Gn4AResultData(resp.FAIL_CODE, "登录账号标识不可为空", "登录账号标识不可为空", c)
  129. return
  130. }
  131. //errMsg, e := gnLogic.DeleteAccount(req.UserId)
  132. errMsg, e := gnLogic.SuspendAccount(req.UserId)
  133. if e != nil {
  134. resp.Gn4AResultData(resp.FAIL_CODE, errMsg, fmt.Sprintf("DeleteAccount: %v", e), c)
  135. return
  136. }
  137. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  138. }
  139. // SuspendAccount 禁用账号
  140. func (gc *GuoNengController) SuspendAccount(c *gin.Context) {
  141. var req gnRequest.SuspendAccountReq
  142. if e := c.Bind(&req); e != nil {
  143. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", fmt.Sprintf("参数解析失败, %v", e), c)
  144. return
  145. }
  146. if req.UserId == "" {
  147. resp.Gn4AResultData(resp.FAIL_CODE, "登录账号标识不可为空", "登录账号标识不可为空", c)
  148. return
  149. }
  150. errMsg, e := gnLogic.SuspendAccount(req.UserId)
  151. if e != nil {
  152. resp.Gn4AResultData(resp.FAIL_CODE, errMsg, fmt.Sprintf("SuspendAccount: %v", e), c)
  153. return
  154. }
  155. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  156. }
  157. // RestoreAccount 禁用账号
  158. func (gc *GuoNengController) RestoreAccount(c *gin.Context) {
  159. var req gnRequest.RestoreAccountReq
  160. if e := c.Bind(&req); e != nil {
  161. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", fmt.Sprintf("参数解析失败, %v", e), c)
  162. return
  163. }
  164. if req.UserId == "" {
  165. resp.Gn4AResultData(resp.FAIL_CODE, "登录账号标识不可为空", "登录账号标识不可为空", c)
  166. return
  167. }
  168. errMsg, e := gnLogic.RestoreAccount(req.UserId)
  169. if e != nil {
  170. resp.Gn4AResultData(resp.FAIL_CODE, errMsg, fmt.Sprintf("RestoreAccount: %v", e), c)
  171. return
  172. }
  173. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  174. }
  175. // ChangePassword 修改密码
  176. func (gc *GuoNengController) ChangePassword(c *gin.Context) {
  177. var req gnRequest.ChangePasswordReq
  178. if e := c.Bind(&req); e != nil {
  179. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", fmt.Sprintf("参数解析失败, %v", e), c)
  180. return
  181. }
  182. if req.UserId == "" {
  183. resp.Gn4AResultData(resp.FAIL_CODE, "登录账号标识不可为空", "登录账号标识不可为空", c)
  184. return
  185. }
  186. req.NewPassword = strings.TrimSpace(req.NewPassword)
  187. if req.NewPassword == "" {
  188. resp.Gn4AResultData(resp.FAIL_CODE, "新密码不可为空", "新密码不可为空", c)
  189. return
  190. }
  191. errMsg, e := gnLogic.ChangePassword(req.UserId, req.NewPassword)
  192. if e != nil {
  193. resp.Gn4AResultData(resp.FAIL_CODE, errMsg, fmt.Sprintf("ChangePassword: %v", e), c)
  194. return
  195. }
  196. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  197. }
  198. // SyncMdmOrganization 同步mdm组织部门
  199. func (gc *GuoNengController) SyncMdmOrganization(c *gin.Context) {
  200. var req gnRequest.SyncMdmOrganizationReq
  201. if e := c.Bind(&req); e != nil {
  202. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", "参数解析失败", c)
  203. return
  204. }
  205. req.MainFieldComstruCode = strings.TrimSpace(req.MainFieldComstruCode)
  206. if req.MainFieldComstruCode == "" {
  207. resp.Gn4AResultData(resp.FAIL_CODE, "组织机构编码不可为空", "组织机构编码不可为空", c)
  208. return
  209. }
  210. // 根据主编码判断是否已同步过, 同步过则更新
  211. ob := new(gn.MDM_COMSTRU)
  212. cond := ` AND Main_Field_Comstru_Code = ?`
  213. pars := make([]interface{}, 0)
  214. pars = append(pars, req.MainFieldComstruCode)
  215. item, e := ob.GetItemByCondition(cond, pars)
  216. if e != nil && !utils.IsErrNoRow(e) {
  217. resp.Gn4AResultData(resp.FAIL_CODE, "组织信息异常", fmt.Sprintf("获取MDM组织信息失败, %v", e), c)
  218. return
  219. }
  220. // 更新
  221. if item != nil && item.Id > 0 {
  222. updateCols := []string{"Main_Field_Comstru_Code_HR", "Main_Field_Comstru_Type_HR", "ZDWLX", "Main_Field_Comstru_Name_HR", "Main_Field_Comstru_Short_Name_HR", "Main_Field_ParentAdmin_Comstru_Code_HR", "Main_Field_ParentAdmin_Comstru_Code", "Main_Field_ParentAdmin_Comstru_Name", "Main_Field_Company_Level", "Main_Field_Secondcomstrucodehr", "Main_Field_Sh_Business_Type", "Main_Field_YZGDWZCNDGX", "Main_Field_Order_HR", "Main_Field_Company_Code", "Main_Field_Company_Chain", "Main_Field_PARENTCOMPANYVIRTUAL", "ZQYZT", "ModifyTime"}
  223. item.MainFieldComstruCodeHR = req.MainFieldComstruCodeHR
  224. item.MainFieldComstruTypeHR = req.MainFieldComstruTypeHR
  225. item.ZDWLX = req.ZDWLX
  226. item.MainFieldComstruNameHR = req.MainFieldComstruNameHR
  227. item.MainFieldComstruShortNameHR = req.MainFieldComstruShortNameHR
  228. item.MainFieldParentAdminComstruCodeHR = req.MainFieldParentAdminComstruCodeHR
  229. item.MainFieldParentAdminComstruCode = req.MainFieldParentAdminComstruCode
  230. item.MainFieldParentAdminComstruName = req.MainFieldParentAdminComstruName
  231. item.MainFieldCompanyLevel = req.MainFieldCompanyLevel
  232. item.MainFieldSecondcomstrucodehr = req.MainFieldSecondcomstrucodehr
  233. item.MainFieldShBusinessType = req.MainFieldShBusinessType
  234. item.MainFieldYZGDWZCNDGX = req.MainFieldYZGDWZCNDGX
  235. item.MainFieldOrderHR = req.MainFieldOrderHR
  236. item.MainFieldCompanyCode = req.MainFieldCompanyCode
  237. item.MainFieldCompanyChain = req.MainFieldCompanyChain
  238. item.MainFieldPARENTCOMPANYVIRTUAL = req.MainFieldPARENTCOMPANYVIRTUAL
  239. item.ZQYZT = req.ZQYZT
  240. item.ModifyTime = time.Now()
  241. if e = item.Update(updateCols); e != nil {
  242. resp.Gn4AResultData(resp.FAIL_CODE, "同步失败", fmt.Sprintf("更新MDM组织信息失败, %v", e), c)
  243. return
  244. }
  245. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  246. return
  247. }
  248. // 新增
  249. newOrganizer := gn.MDM_COMSTRU{
  250. MainFieldComstruCode: req.MainFieldComstruCode,
  251. MainFieldComstruCodeHR: req.MainFieldComstruCodeHR,
  252. MainFieldComstruTypeHR: req.MainFieldComstruTypeHR,
  253. ZDWLX: req.ZDWLX,
  254. MainFieldComstruNameHR: req.MainFieldComstruNameHR,
  255. MainFieldComstruShortNameHR: req.MainFieldComstruShortNameHR,
  256. MainFieldParentAdminComstruCodeHR: req.MainFieldParentAdminComstruCodeHR,
  257. MainFieldParentAdminComstruCode: req.MainFieldParentAdminComstruCode,
  258. MainFieldParentAdminComstruName: req.MainFieldParentAdminComstruName,
  259. MainFieldCompanyLevel: req.MainFieldCompanyLevel,
  260. MainFieldSecondcomstrucodehr: req.MainFieldSecondcomstrucodehr,
  261. MainFieldShBusinessType: req.MainFieldShBusinessType,
  262. MainFieldYZGDWZCNDGX: req.MainFieldYZGDWZCNDGX,
  263. MainFieldOrderHR: req.MainFieldOrderHR,
  264. MainFieldCompanyCode: req.MainFieldCompanyCode,
  265. MainFieldCompanyChain: req.MainFieldCompanyChain,
  266. MainFieldPARENTCOMPANYVIRTUAL: req.MainFieldPARENTCOMPANYVIRTUAL,
  267. ZQYZT: req.ZQYZT,
  268. CreateTime: time.Now(),
  269. ModifyTime: time.Now(),
  270. }
  271. if e = newOrganizer.Create(); e != nil {
  272. resp.Gn4AResultData(resp.FAIL_CODE, "同步失败", fmt.Sprintf("新增MDM组织信息失败, %v", e), c)
  273. return
  274. }
  275. // 同步ETA部门
  276. go func() {
  277. _ = gnLogic.SyncSysDepartment(&newOrganizer)
  278. }()
  279. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  280. }
  281. // SyncMdmEmployee 同步mdm员工
  282. func (gc *GuoNengController) SyncMdmEmployee(c *gin.Context) {
  283. var req gnRequest.SyncMdmEmployeeReq
  284. if e := c.Bind(&req); e != nil {
  285. resp.Gn4AResultData(resp.FAIL_CODE, "参数有误", "参数解析失败", c)
  286. return
  287. }
  288. req.MainFieldEmployeeCode = strings.TrimSpace(req.MainFieldEmployeeCode)
  289. if req.MainFieldEmployeeCode == "" {
  290. resp.Gn4AResultData(resp.FAIL_CODE, "员工编码不可为空", "员工编码不可为空", c)
  291. return
  292. }
  293. // 根据主编码判断是否已同步过, 同步过则更新
  294. ob := new(gn.MDM_EMPLOYEE)
  295. cond := ` AND Main_Field_Employee_Code = ?`
  296. pars := make([]interface{}, 0)
  297. pars = append(pars, req.MainFieldEmployeeCode)
  298. item, e := ob.GetItemByCondition(cond, pars)
  299. if e != nil && !utils.IsErrNoRow(e) {
  300. resp.Gn4AResultData(resp.FAIL_CODE, "员工信息异常", fmt.Sprintf("获取MDM员工信息失败, %v", e), c)
  301. return
  302. }
  303. // 员工组织关系
  304. relations := make([]*gn.MDM_RELA_COMSTRU_EMPLOYEE, 0)
  305. if len(req.MDM_MATERIAL_PROPS) > 0 {
  306. for _, v := range req.MDM_MATERIAL_PROPS {
  307. relations = append(relations, &gn.MDM_RELA_COMSTRU_EMPLOYEE{
  308. Main_Field_Employee_Code: req.MainFieldEmployeeCode,
  309. TuplesFieldCompanyCode: v.TuplesFieldCompanyCode,
  310. TuplesFieldCompanyName: v.TuplesFieldCompanyName,
  311. TuplesFieldDepartmentCode: v.TuplesFieldDepartmentCode,
  312. TuplesFieldDepartmentName: v.TuplesFieldDepartmentName,
  313. CreateTime: time.Now(),
  314. ModifyTime: time.Now(),
  315. })
  316. }
  317. }
  318. // 更新
  319. var isUpdate bool
  320. employeeOb := new(gn.MDM_EMPLOYEE)
  321. if item != nil && item.Id > 0 {
  322. isUpdate = true
  323. updateCols := []string{"Main_Field_Employee_FirstName", "Main_Field_Employee_LastName", "Main_Field_Gender", "Main_Field_Empolyee_Group", "Main_Field_Office_Telephone", "Main_Field_Date_Type", "Main_Field_Employee_Status", "Main_Field_Delete_Flag", "Main_Field_Email", "Main_Field_Company_code", "Main_Field_Secondcomstrucodehr", "Main_Field_Company_Chain", "ModifyTime"}
  324. item.MainFieldEmployeeFirstName = req.MainFieldEmployeeFirstName
  325. item.MainFieldEmployeeLastName = req.MainFieldEmployeeLastName
  326. item.MainFieldGender = req.MainFieldGender
  327. item.MainFieldEmpolyeeGroup = req.MainFieldEmpolyeeGroup
  328. item.MainFieldOfficeTelephone = req.MainFieldOfficeTelephone
  329. item.MainFieldDateType = req.MainFieldDateType
  330. item.MainFieldEmployeeStatus = req.MainFieldEmployeeStatus
  331. item.MainFieldDeleteFlag = req.MainFieldDeleteFlag
  332. item.MainFieldEmail = req.MainFieldEmail
  333. item.MainFieldCompanyCode = req.MainFieldCompanyCode
  334. item.MainFieldSecondcomstrucodehr = req.MainFieldSecondcomstrucodehr
  335. item.MainFieldCompanyChain = req.MainFieldCompanyChain
  336. item.ModifyTime = time.Now()
  337. if e = employeeOb.UpdateEmployee(item, updateCols, relations); e != nil {
  338. resp.Gn4AResultData(resp.FAIL_CODE, "同步失败", fmt.Sprintf("更新MDM组织信息失败, %v", e), c)
  339. return
  340. }
  341. }
  342. // 新增
  343. if !isUpdate {
  344. newEmployee := &gn.MDM_EMPLOYEE{
  345. MainFieldEmployeeCode: req.MainFieldEmployeeCode,
  346. MainFieldEmployeeFirstName: req.MainFieldEmployeeFirstName,
  347. MainFieldEmployeeLastName: req.MainFieldEmployeeLastName,
  348. MainFieldGender: req.MainFieldGender,
  349. MainFieldEmpolyeeGroup: req.MainFieldEmpolyeeGroup,
  350. MainFieldOfficeTelephone: req.MainFieldOfficeTelephone,
  351. MainFieldDateType: req.MainFieldDateType,
  352. MainFieldEmployeeStatus: req.MainFieldEmployeeStatus,
  353. MainFieldDeleteFlag: req.MainFieldDeleteFlag,
  354. MainFieldEmail: req.MainFieldEmail,
  355. MainFieldCompanyCode: req.MainFieldCompanyCode,
  356. MainFieldSecondcomstrucodehr: req.MainFieldSecondcomstrucodehr,
  357. MainFieldCompanyChain: req.MainFieldCompanyChain,
  358. CreateTime: time.Now(),
  359. ModifyTime: time.Now(),
  360. }
  361. if e = employeeOb.CreateEmployee(newEmployee, relations); e != nil {
  362. resp.Gn4AResultData(resp.FAIL_CODE, "同步失败", fmt.Sprintf("新增MDM员工信息失败, %v", e), c)
  363. return
  364. }
  365. }
  366. resp.Gn4AResultData(resp.GN_OK_CODE, "成功", "", c)
  367. }