employee.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package maycur
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/models/system"
  7. "hongze/hz_crm_api/services/alarm_msg"
  8. "hongze/hz_crm_api/utils"
  9. "strings"
  10. )
  11. // EmployeeDetailReq 员工详情请求体
  12. type EmployeeDetailReq struct {
  13. EmployeeIds []string `description:"员工工号" json:"employeeIds"`
  14. }
  15. // EmployeeDetailResp 员工详情响应体
  16. type EmployeeDetailResp struct {
  17. Code string `description:"状态码"`
  18. Message string `description:"返回信息"`
  19. Data []EmployeeDetailData `description:"员工详情信息"`
  20. Success bool `description:"是否成功"`
  21. }
  22. // EmployeeDetailData 员工详情
  23. type EmployeeDetailData struct {
  24. EmployeeId string `description:"员工工号"`
  25. EmployeeName string `description:"员工姓名"`
  26. Email string `description:"电子邮箱"`
  27. PhoneNo string `description:"手机号"`
  28. Status string `description:"状态: ENABLE-正常"`
  29. }
  30. // CurlEmployeeDetail 请求员工详情接口
  31. func CurlEmployeeDetail(req EmployeeDetailReq) (employeeList []EmployeeDetailData, err error) {
  32. defer func() {
  33. if err != nil {
  34. utils.FileLog.Error("CurlEmployeeDetail ErrMsg: %s", err.Error())
  35. go alarm_msg.SendAlarmMsg(fmt.Sprintf("每刻报销-获取员工详情信息失败, ErrMsg: %s", err.Error()), 3)
  36. }
  37. }()
  38. for _, v := range req.EmployeeIds {
  39. if v == "" {
  40. err = fmt.Errorf("employeeIds is empty")
  41. return
  42. }
  43. }
  44. // 请求
  45. url := fmt.Sprintf("%s%s", utils.MayCurBaseUrl, ApiEmployeeDetailUrl)
  46. reqByte, e := json.Marshal(req)
  47. if e != nil {
  48. err = fmt.Errorf("request json marshal err: %s", e.Error())
  49. return
  50. }
  51. body, e := buildHttpRequest(url, "POST", string(reqByte), true)
  52. if e != nil {
  53. err = fmt.Errorf("curl api err: %s", e.Error())
  54. return
  55. }
  56. // 响应
  57. resp := new(EmployeeDetailResp)
  58. if e = json.Unmarshal(body, &resp); e != nil {
  59. err = fmt.Errorf("resp json unmarshal err: %s", e.Error())
  60. return
  61. }
  62. if resp == nil {
  63. err = fmt.Errorf("resp nil")
  64. return
  65. }
  66. if resp.Code != ApiSuccessCode {
  67. err = fmt.Errorf("resp err message: %s", resp.Message)
  68. return
  69. }
  70. employeeList = resp.Data
  71. return
  72. }
  73. // ImportExcelEmployee 导入Excel员工信息
  74. type ImportExcelEmployee struct {
  75. EmployeeName string `description:"员工姓名"`
  76. EmployeeId string `description:"员工工号"`
  77. }
  78. // ImportExcelEmployeeId 导入Excel更新员工工号
  79. func ImportExcelEmployeeId() (err error) {
  80. defer func() {
  81. if err != nil {
  82. fmt.Printf("导入失败, ErrMsg: %s\n", err.Error())
  83. return
  84. }
  85. fmt.Println("导入成功")
  86. }()
  87. // 读取文件
  88. path := `./static/employee_id.xlsx`
  89. xlFile, e := xlsx.OpenFile(path)
  90. if e != nil {
  91. err = fmt.Errorf("打开文件失败, Err: %s", e.Error())
  92. return
  93. }
  94. // 读取数据
  95. importData := make([]*ImportExcelEmployee, 0)
  96. for _, sheet := range xlFile.Sheets {
  97. maxRow := sheet.MaxRow
  98. for i := 0; i < maxRow; i++ {
  99. if i == 0 {
  100. row := sheet.Row(i)
  101. cells := row.Cells
  102. for k, cell := range cells {
  103. text := cell.String()
  104. if k == 0 {
  105. if text != "姓名" {
  106. err = fmt.Errorf("模板格式错误: 姓名-%s", text)
  107. return
  108. }
  109. }
  110. if k == 1 {
  111. if text != "工号" {
  112. err = fmt.Errorf("模板格式错误: 工号-%s", text)
  113. return
  114. }
  115. }
  116. }
  117. }
  118. if i >= 1 {
  119. row := sheet.Row(i)
  120. cells := row.Cells
  121. employee := new(ImportExcelEmployee)
  122. for k, cell := range cells {
  123. v := strings.TrimSpace(cell.String())
  124. if v == "" {
  125. err = fmt.Errorf("第%d行存在空数据", i)
  126. return
  127. }
  128. if k == 0 {
  129. employee.EmployeeName = v
  130. }
  131. if k == 1 {
  132. employee.EmployeeId = v
  133. }
  134. }
  135. importData = append(importData, employee)
  136. fmt.Printf("姓名-%s; 工号-%s\n", employee.EmployeeName, employee.EmployeeId)
  137. }
  138. }
  139. }
  140. if len(importData) == 0 {
  141. return
  142. }
  143. employeeMap := make(map[string]string)
  144. for _, d := range importData {
  145. employeeMap[d.EmployeeName] = d.EmployeeId
  146. }
  147. // 更新工号
  148. adminOB := new(system.Admin)
  149. adminPars := make([]interface{}, 0)
  150. admins, e := adminOB.GetItemsByCondition(``, adminPars, []string{}, "")
  151. if e != nil {
  152. err = fmt.Errorf("获取员工列表失败, Err: %s", e.Error())
  153. return
  154. }
  155. updateCols := []string{"EmployeeId"}
  156. for _, a := range admins {
  157. id := employeeMap[a.RealName]
  158. if id == "" {
  159. continue
  160. }
  161. a.EmployeeId = id
  162. if e = a.Update(updateCols); e != nil {
  163. err = fmt.Errorf("更新员工工号失败, Err: %s", e.Error())
  164. return
  165. }
  166. }
  167. return
  168. }
  169. // levelMap 销售组别等级
  170. var levelMap = map[string]string{
  171. utils.ROLE_TYPE_CODE_FICC_SELLER: utils.ROLE_TYPE_CODE_FICC_TEAM,
  172. utils.ROLE_TYPE_CODE_FICC_TEAM: utils.ROLE_TYPE_CODE_FICC_GROUP,
  173. utils.ROLE_TYPE_CODE_FICC_GROUP: utils.ROLE_TYPE_CODE_FICC_DEPARTMENT,
  174. utils.ROLE_TYPE_CODE_FICC_DEPARTMENT: utils.ROLE_TYPE_CODE_FICC_ADMIN,
  175. utils.ROLE_TYPE_CODE_RAI_SELLER: utils.ROLE_TYPE_CODE_RAI_GROUP,
  176. utils.ROLE_TYPE_CODE_RAI_GROUP: utils.ROLE_TYPE_CODE_RAI_DEPARTMENT,
  177. utils.ROLE_TYPE_CODE_RAI_DEPARTMENT: utils.ROLE_TYPE_CODE_RAI_ADMIN,
  178. }
  179. // findSellerLeader 查找销售上级
  180. func findSellerLeader(seller *system.Admin, employeeList []*system.Admin, groupList []*system.Admin, groupParentMap map[int]int, groupParentChildrenMap map[int][]int, existMap map[int]bool) []*system.Admin {
  181. if groupList == nil {
  182. groupList = make([]*system.Admin, 0)
  183. }
  184. // 最上级, 中止递归
  185. leaderRole := levelMap[seller.RoleTypeCode]
  186. if leaderRole == "" {
  187. return groupList
  188. }
  189. // 遍历员工列表
  190. for _, v := range employeeList {
  191. isLeader := false
  192. // 根据当前销售级别查找(没办法...乱七八糟的=_=!)
  193. parentGroupId := groupParentMap[seller.GroupId]
  194. parentChildrenGroupIds := groupParentChildrenMap[seller.GroupId]
  195. switch seller.RoleTypeCode {
  196. case utils.ROLE_TYPE_CODE_FICC_SELLER:
  197. // 小组-组长/主管
  198. if seller.GroupId == v.GroupId && (v.RoleTypeCode == leaderRole || v.RoleTypeCode == levelMap[leaderRole]) {
  199. isLeader = true
  200. break
  201. }
  202. // 大组-主管/同时归属大组和大组下小组的主管
  203. if parentGroupId > 0 && v.RoleTypeCode == levelMap[leaderRole] && (v.GroupId == parentGroupId || utils.InArrayByInt(parentChildrenGroupIds, v.GroupId)) {
  204. isLeader = true
  205. break
  206. }
  207. // FICC部门管理员/FICC管理员
  208. if v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT || v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  209. isLeader = true
  210. break
  211. }
  212. case utils.ROLE_TYPE_CODE_RAI_SELLER:
  213. // 本组的组长/主管
  214. if seller.GroupId == v.GroupId && (v.RoleTypeCode == leaderRole || v.RoleTypeCode == levelMap[leaderRole]) {
  215. isLeader = true
  216. break
  217. }
  218. // 权益销售部门主管
  219. if parentGroupId > 0 && v.RoleTypeCode == levelMap[leaderRole] && v.GroupId == parentGroupId {
  220. isLeader = true
  221. break
  222. }
  223. // 权益部门管理员/权益管理员
  224. if v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT || v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  225. isLeader = true
  226. break
  227. }
  228. case utils.ROLE_TYPE_CODE_FICC_TEAM:
  229. // 主管-同组
  230. if v.RoleTypeCode == leaderRole && seller.GroupId == v.GroupId {
  231. isLeader = true
  232. break
  233. }
  234. // 主管-父级组
  235. if v.RoleTypeCode == leaderRole && parentGroupId > 0 && parentGroupId == v.GroupId {
  236. isLeader = true
  237. break
  238. }
  239. // 主管-同一父级组但同时归属其他小组
  240. if v.RoleTypeCode == leaderRole && parentGroupId > 0 && utils.InArrayByInt(parentChildrenGroupIds, v.GroupId) {
  241. isLeader = true
  242. break
  243. }
  244. // FICC部门管理员/FICC管理员
  245. if v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT || v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  246. isLeader = true
  247. break
  248. }
  249. case utils.ROLE_TYPE_CODE_FICC_GROUP, utils.ROLE_TYPE_CODE_RAI_GROUP:
  250. // 本组/上一组的领导
  251. if (seller.GroupId == v.GroupId || parentGroupId == v.GroupId) && v.RoleTypeCode == leaderRole {
  252. isLeader = true
  253. break
  254. }
  255. // FICC管理员
  256. if seller.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP && (v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_DEPARTMENT || v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN) {
  257. isLeader = true
  258. break
  259. }
  260. // 权益管理员
  261. if seller.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP && (v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_DEPARTMENT || v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN) {
  262. isLeader = true
  263. break
  264. }
  265. }
  266. // 向上递归
  267. if !isLeader {
  268. continue
  269. }
  270. // 去重
  271. if !existMap[v.AdminId] {
  272. existMap[v.AdminId] = true
  273. groupList = append(groupList, v)
  274. }
  275. groupList = findSellerLeader(v, employeeList, groupList, groupParentMap, groupParentChildrenMap, existMap)
  276. }
  277. return groupList
  278. }
  279. // getEmployeeGroupMapFromList 获取员工及上级员工组Map
  280. func getEmployeeGroupMapFromList() (employeeGroupMap map[int][]*system.Admin, err error) {
  281. employeeOB := new(system.Admin)
  282. employeeCond := ` AND enabled = 1 AND employee_id <> ""`
  283. employeePars := make([]interface{}, 0)
  284. employeeList, e := employeeOB.GetItemsByCondition(employeeCond, employeePars, []string{}, "")
  285. if e != nil {
  286. err = fmt.Errorf("获取员工列表失败, Err: %s", e.Error())
  287. return
  288. }
  289. // FICC销售多一层组, 所以需要更详细的分组信息
  290. sysGroupList, e := system.GetFullGroup()
  291. if e != nil {
  292. err = fmt.Errorf("获取部门分组列表失败, Err: %s", e.Error())
  293. return
  294. }
  295. groupParentMap := make(map[int]int) // 组别父级ID
  296. groupChildrenMap := make(map[int][]int) // 组别子级IDs
  297. for _, s := range sysGroupList {
  298. if s.ParentId > 0 {
  299. groupParentMap[s.GroupId] = s.ParentId
  300. groupChildrenMap[s.ParentId] = append(groupChildrenMap[s.ParentId], s.GroupId)
  301. }
  302. }
  303. groupParentChildrenMap := make(map[int][]int) // 组别父级下的至直系子组
  304. for _, s2 := range sysGroupList {
  305. if s2.ParentId > 0 {
  306. groupParentChildrenMap[s2.GroupId] = groupChildrenMap[s2.ParentId]
  307. }
  308. }
  309. //fmt.Println("groupParentMap: ", groupParentMap)
  310. employeeGroupMap = make(map[int][]*system.Admin)
  311. for _, v := range employeeList {
  312. // 非销售角色忽略
  313. //if v.DepartmentId != 2 {
  314. //employeeGroupMap[v.AdminId] = append(employeeGroupMap[v.AdminId], v)
  315. //continue
  316. //}
  317. if employeeGroupMap[v.AdminId] == nil {
  318. employeeGroupMap[v.AdminId] = make([]*system.Admin, 0)
  319. }
  320. // 销售-自下而上找自己组的组长、主管一并加入自己的分组
  321. existMap := make(map[int]bool)
  322. leaders := findSellerLeader(v, employeeList, nil, groupParentMap, groupParentChildrenMap, existMap)
  323. employeeGroupMap[v.AdminId] = append(employeeGroupMap[v.AdminId], v)
  324. if len(leaders) > 0 {
  325. employeeGroupMap[v.AdminId] = append(employeeGroupMap[v.AdminId], leaders...)
  326. }
  327. }
  328. return
  329. }