company_todo.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models/company"
  5. "hongze/hz_crm_api/models/company_todo"
  6. "hongze/hz_crm_api/models/system"
  7. "hongze/hz_crm_api/utils"
  8. "time"
  9. )
  10. // AddCompanyTodo 添加任务待办
  11. func AddCompanyTodo(companyId, productId int, content string, endTime time.Time, createUserInfo *system.Admin) (errMsg string, err error) {
  12. defer func() {
  13. if err != nil {
  14. if errMsg == `` {
  15. errMsg = "添加任务失败"
  16. }
  17. }
  18. }()
  19. // 获取客户信息
  20. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(companyId, productId)
  21. if err != nil {
  22. errMsg = `获取客户信息异常`
  23. return
  24. }
  25. sellerId := companyProductInfo.SellerId
  26. sellerName := companyProductInfo.SellerName
  27. //是否在允许创建任务的角色里面
  28. if createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN &&
  29. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN &&
  30. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_GROUP &&
  31. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_TEAM &&
  32. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_SELLER &&
  33. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN &&
  34. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_GROUP &&
  35. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_SELLER {
  36. errMsg = `操作人角色异常,不允许新增任务:` + createUserInfo.RoleName
  37. err = fmt.Errorf(errMsg)
  38. return
  39. }
  40. //超管和部门管理员所有客户都能操作,那就不判断了
  41. //如果是普通销售,那么只能操作自己的客户
  42. if createUserInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || createUserInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER {
  43. if sellerId != createUserInfo.AdminId {
  44. errMsg = `该客户不是当前账号所属,不允许新增任务`
  45. err = fmt.Errorf(errMsg)
  46. return
  47. }
  48. }
  49. //获取客户所属销售信息
  50. sellerInfo, err := system.GetSysUserById(companyProductInfo.SellerId)
  51. if err != nil {
  52. errMsg = `获取客户所属销售信息异常`
  53. return
  54. }
  55. // 销售主管、组长权限判断
  56. switch createUserInfo.RoleTypeCode {
  57. case utils.ROLE_TYPE_CODE_FICC_GROUP: //ficc主管
  58. isSame, tmpErr := CheckAdminIsSameBigGroup(createUserInfo, sellerInfo)
  59. if tmpErr != nil {
  60. errMsg = `获取客户所属销售信息异常`
  61. err = fmt.Errorf("判断主管与该客户所属销售是否属于同一大组失败,err:" + tmpErr.Error())
  62. return
  63. }
  64. if !isSame {
  65. errMsg = `该客户不是当前账号所属组员,不允许新增任务`
  66. err = fmt.Errorf(errMsg)
  67. return
  68. }
  69. case utils.ROLE_TYPE_CODE_FICC_TEAM, utils.ROLE_TYPE_CODE_RAI_GROUP: //ficc组长、权益组长
  70. if sellerInfo.GroupId != createUserInfo.GroupId {
  71. errMsg = `该客户不是当前账号所属组员,不允许新增任务`
  72. err = fmt.Errorf(errMsg)
  73. return
  74. }
  75. }
  76. // 获取正在处理中的任务数量
  77. total, err := company_todo.GetCountDoingCompanyTodo(companyId, productId)
  78. if err != nil {
  79. return
  80. }
  81. if total > 0 {
  82. errMsg = "当前存在待处理的任务,不允许添加新的任务待办"
  83. err = fmt.Errorf(errMsg)
  84. return
  85. }
  86. companyTodoInfo := &company_todo.CompanyTodo{
  87. //Id: 0,
  88. CompanyId: companyId,
  89. ProductId: productId,
  90. Content: content,
  91. SellerId: sellerId,
  92. SellerName: sellerName,
  93. CreatedCompanyStatus: companyProductInfo.Status,
  94. CreateUserId: createUserInfo.AdminId,
  95. CreateUserName: createUserInfo.RealName,
  96. //ApproveUserId: 0,
  97. //ApproveUserName: "",
  98. Status: "进行中",
  99. ModifyTime: time.Now(),
  100. CreateTime: time.Now(),
  101. EndTime: endTime,
  102. }
  103. _, err = company_todo.AddCompanyTodo(companyTodoInfo)
  104. return
  105. }
  106. // EditCompanyTodo 修改任务待办
  107. func EditCompanyTodo(id int, content string, endTime time.Time, createUserInfo *system.Admin) (errMsg string, err error) {
  108. defer func() {
  109. if err != nil {
  110. if errMsg == `` {
  111. errMsg = "编辑任务失败"
  112. }
  113. }
  114. }()
  115. // 获取正在处理中的任务数量
  116. companyTodoInfo, err := company_todo.GetCompanyTodoById(id)
  117. if err != nil {
  118. return
  119. }
  120. if companyTodoInfo.Status != "进行中" {
  121. errMsg = `任务状态异常:` + companyTodoInfo.Status + `,不允许编辑该任务待办`
  122. err = fmt.Errorf(errMsg)
  123. return
  124. }
  125. // 获取客户信息
  126. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(companyTodoInfo.CompanyId, companyTodoInfo.ProductId)
  127. if err != nil {
  128. errMsg = "获取客户信息异常,err:" + err.Error()
  129. return
  130. }
  131. sellerId := companyProductInfo.SellerId
  132. sellerName := companyProductInfo.SellerName
  133. //是否在允许创建任务的角色里面
  134. if createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN &&
  135. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN &&
  136. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_GROUP &&
  137. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_TEAM &&
  138. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_SELLER &&
  139. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN &&
  140. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_GROUP &&
  141. createUserInfo.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_SELLER {
  142. errMsg = `操作人角色异常,不允许新增任务:` + createUserInfo.RoleName
  143. err = fmt.Errorf(errMsg)
  144. return
  145. }
  146. //超管和部门管理员所有客户都能操作,那就不判断了
  147. //如果是普通销售,那么只能操作自己的客户
  148. if createUserInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || createUserInfo.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER {
  149. if sellerId != createUserInfo.AdminId {
  150. errMsg = `该客户不是当前账号所属,不允许编辑任务`
  151. err = fmt.Errorf(errMsg)
  152. return
  153. }
  154. }
  155. //获取客户所属销售信息
  156. sellerInfo, err := system.GetSysUserById(companyProductInfo.SellerId)
  157. if err != nil {
  158. errMsg = `获取客户所属销售信息异常`
  159. return
  160. }
  161. // 销售主管、组长权限判断
  162. switch createUserInfo.RoleTypeCode {
  163. case utils.ROLE_TYPE_CODE_FICC_GROUP: //ficc主管
  164. isSame, tmpErr := CheckAdminIsSameBigGroup(createUserInfo, sellerInfo)
  165. if tmpErr != nil {
  166. errMsg = `获取客户所属销售信息异常`
  167. err = fmt.Errorf("判断主管与该客户所属销售是否属于同一大组失败,err:" + tmpErr.Error())
  168. return
  169. }
  170. if !isSame {
  171. errMsg = `该客户不是当前账号所属组员,不允许新增任务`
  172. err = fmt.Errorf(errMsg)
  173. return
  174. }
  175. case utils.ROLE_TYPE_CODE_FICC_TEAM, utils.ROLE_TYPE_CODE_RAI_GROUP: //ficc组长、权益组长
  176. if sellerInfo.GroupId != createUserInfo.GroupId {
  177. errMsg = `该客户不是当前账号所属组员,不允许新增任务`
  178. err = fmt.Errorf(errMsg)
  179. return
  180. }
  181. }
  182. companyTodoInfo.Content = content
  183. companyTodoInfo.SellerId = sellerId
  184. companyTodoInfo.SellerName = sellerName
  185. companyTodoInfo.CreatedCompanyStatus = companyProductInfo.Status
  186. companyTodoInfo.ModifyTime = time.Now()
  187. companyTodoInfo.EndTime = endTime
  188. err = companyTodoInfo.EditCompanyTodo([]string{"Content", "SellerId", "SellerName", "CreatedCompanyStatus", "ModifyTime", "EndTime"})
  189. return
  190. }
  191. // ApproveCompanyTodo 审批任务待办
  192. func ApproveCompanyTodo(id int, remark string, approveUserInfo *system.Admin) (errMsg string, err error) {
  193. defer func() {
  194. if err != nil {
  195. if errMsg == `` {
  196. errMsg = "编辑任务失败"
  197. }
  198. }
  199. }()
  200. // 获取正在处理中的任务数量
  201. companyTodoInfo, err := company_todo.GetCompanyTodoById(id)
  202. if err != nil {
  203. return
  204. }
  205. if companyTodoInfo.Status != "进行中" {
  206. errMsg = `任务状态异常:` + companyTodoInfo.Status + `,不允许编辑该任务待办`
  207. err = fmt.Errorf(errMsg)
  208. return
  209. }
  210. // 获取客户信息
  211. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(companyTodoInfo.CompanyId, companyTodoInfo.ProductId)
  212. if err != nil {
  213. errMsg = "获取客户信息异常,err:" + err.Error()
  214. return
  215. }
  216. //sellerId := companyProductInfo.SellerId
  217. //sellerName := companyProductInfo.SellerName
  218. //获取客户所属销售信息
  219. sellerInfo, err := system.GetSysUserById(companyProductInfo.SellerId)
  220. if err != nil {
  221. errMsg = `获取客户所属销售信息异常`
  222. return
  223. }
  224. // 销售主管、组长权限判断
  225. switch approveUserInfo.RoleTypeCode {
  226. case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RAI_ADMIN:
  227. // ficc管理员可以全部审批通过
  228. case utils.ROLE_TYPE_CODE_FICC_GROUP, utils.ROLE_TYPE_CODE_RAI_GROUP: //ficc主管、权益组长
  229. isSame, tmpErr := CheckAdminIsSameBigGroup(approveUserInfo, sellerInfo)
  230. if tmpErr != nil {
  231. errMsg = `获取客户所属销售信息异常`
  232. err = fmt.Errorf("判断主管与该客户所属销售是否属于同一大组失败,err:" + tmpErr.Error())
  233. return
  234. }
  235. if !isSame {
  236. errMsg = `该客户不是当前账号所属组员,不允许审批任务`
  237. err = fmt.Errorf(errMsg)
  238. return
  239. }
  240. case utils.ROLE_TYPE_CODE_FICC_TEAM: //ficc组长
  241. if sellerInfo.GroupId != approveUserInfo.GroupId {
  242. errMsg = `该客户不是当前账号所属组员,不允许审批任务`
  243. err = fmt.Errorf(errMsg)
  244. return
  245. }
  246. case utils.ROLE_TYPE_CODE_RAI_SELLER: //权益销售
  247. if sellerInfo.AdminId != approveUserInfo.AdminId {
  248. //approveUserInfo.
  249. errMsg = `没有审批权限,不允许审批任务`
  250. err = fmt.Errorf(errMsg)
  251. return
  252. }
  253. has := CheckRaiApprovePermission(approveUserInfo.AdminId)
  254. if !has {
  255. errMsg = `当前账号没有审批权限,不允许审批任务`
  256. err = fmt.Errorf(errMsg)
  257. return
  258. }
  259. default:
  260. errMsg = `操作人角色异常,不允许审批任务:` + approveUserInfo.RoleName
  261. err = fmt.Errorf(errMsg)
  262. return
  263. }
  264. //companyTodoInfo.SellerId = sellerId
  265. //companyTodoInfo.SellerName = sellerName
  266. companyTodoInfo.Status = "已完成"
  267. companyTodoInfo.ApproveUserId = approveUserInfo.AdminId
  268. companyTodoInfo.ApproveUserName = approveUserInfo.RealName
  269. companyTodoInfo.ModifyTime = time.Now()
  270. companyTodoInfo.ApproveTime = time.Now()
  271. companyTodoInfo.Remark = remark
  272. companyTodoInfo.ApprovedCompanyStatus = companyProductInfo.Status
  273. companyTodoInfo.ApprovedSellerId = companyProductInfo.SellerId
  274. companyTodoInfo.ApprovedSellerName = companyProductInfo.SellerName
  275. err = companyTodoInfo.ApproveCompanyTodo([]string{"Status", "ApproveUserId", "ApproveUserName", "ModifyTime", "ApproveTime", "ApprovedCompanyStatus", "ApprovedSellerId", "ApprovedSellerName", "Remark"})
  276. return
  277. }
  278. // CheckRaiApprovePermission 检测权益销售的审批权限
  279. func CheckRaiApprovePermission(id int) (has bool) {
  280. adminIdArr := []int{238, 115, 392, 198, 286, 185, 285, 186, 378, 503, 489}
  281. has = utils.InArrayByInt(adminIdArr, id)
  282. return
  283. }