participateStatistics.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "encoding/json"
  6. "fmt"
  7. "jyBXCore/rpc/bxcore"
  8. IC "jyBXCore/rpc/init"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. //角色
  14. Role_admin_system = 1 //系统管理员
  15. Role_admin_department = 2 //部门管理员
  16. )
  17. type ParticipateStatistics struct {
  18. PositionId int64
  19. EntId int64
  20. DeptId int64
  21. EntUserId int64
  22. }
  23. func (in *ParticipateStatistics) PushStatistics(entUserIdArr []string, startTime, endTime int64) (result []*bxcore.PushStatisticsData) {
  24. //判断是企业、部门还是个人
  25. isAdmin, personArrStr, users := in.PersonHandle(entUserIdArr)
  26. //时间处理
  27. query := QueryHandle(isAdmin, startTime, endTime, personArrStr)
  28. //推送数据查询
  29. dataList := IC.BaseMysql.SelectBySql(fmt.Sprintf("select * from participate_push_statistics where %s order by ymd", strings.Join(query, " and ")))
  30. if users != nil && len(*users) > 0 {
  31. result = PushHandle(dataList, users, isAdmin)
  32. }
  33. return
  34. }
  35. func (in *ParticipateStatistics) ProjectStatistics(entUserIdArr []string, startTime, endTime int64) (result []*bxcore.ProjectStatisticsData) {
  36. //判断是企业、部门还是个人
  37. isAdmin, personArrStr, users := in.PersonHandle(entUserIdArr)
  38. query := QueryHandle(isAdmin, startTime, endTime, personArrStr)
  39. //订阅推送数处理
  40. //推送数据查询
  41. dataList := IC.BaseMysql.SelectBySql(fmt.Sprintf("select * from participate_project_statistics where %s order by ymd", strings.Join(query, "and ")))
  42. if users != nil && len(*users) > 0 {
  43. result = ProjectHandle(dataList, users, isAdmin)
  44. }
  45. return
  46. }
  47. func (in *ParticipateStatistics) PersonHandle(entUserIdArr []string) (isAdmin bool, idStr string, users *[]map[string]interface{}) {
  48. userEnt := EntInfo(common.IntAll(in.EntId), common.IntAll(in.EntUserId))
  49. if len(entUserIdArr) > 0 {
  50. //查询所选人的部门以及人员信息
  51. users = GetUser(entUserIdArr)
  52. //返回所选人的信息
  53. isAdmin = true
  54. idStr = strings.Join(entUserIdArr, ",")
  55. } else {
  56. if userEnt.Role_admin_department || userEnt.Role_admin_system { //
  57. // 部门管理员 获取所有部门和子部门员工
  58. users = GetDisUsers(common.IntAll(in.EntId), userEnt.Dept.Id)
  59. var staffs []string
  60. if users != nil && len(*users) > 0 {
  61. for _, v := range *users {
  62. staffs = append(staffs, common.InterfaceToStr(v["id"]))
  63. }
  64. }
  65. isAdmin = true
  66. idStr = strings.Join(staffs, ",")
  67. } else {
  68. if userEnt.Dept.Id != 0 {
  69. entUserIdArr = append(entUserIdArr, common.InterfaceToStr(in.EntUserId))
  70. users = GetUser(entUserIdArr)
  71. //返回所选人的信息
  72. isAdmin = true
  73. idStr = strings.Join(entUserIdArr, ",")
  74. } else {
  75. isAdmin = false
  76. idStr = common.InterfaceToStr(in.PositionId)
  77. users = &[]map[string]interface{}{
  78. map[string]interface{}{
  79. "id": in.PositionId,
  80. "name": "个人",
  81. },
  82. }
  83. }
  84. }
  85. }
  86. return
  87. }
  88. type PushData struct {
  89. PersonName string
  90. entUserId string
  91. DepartmentName string
  92. PushNumb map[string]interface{}
  93. ParticipateNumb map[string]interface{}
  94. BidNumb map[string]interface{}
  95. WinNumb map[string]interface{}
  96. BrowseNumb map[string]interface{}
  97. }
  98. type ProjectData struct {
  99. PersonName string
  100. DepartmentName string
  101. entUserId string
  102. BidNumb map[string]interface{} //投标数量
  103. DirectBidNumb map[string]interface{} //直接投标数
  104. ChannelBidNumb map[string]interface{} //渠道投标数
  105. WinNumb map[string]interface{}
  106. DirectWinNumb map[string]interface{} //直接中标数
  107. ChannelWinNumb map[string]interface{} //渠道中标数
  108. NotBidNumber map[string]interface{} //未中标数量
  109. EndNumb map[string]interface{} //终止数量
  110. }
  111. func PushHandle(data *[]map[string]interface{}, users *[]map[string]interface{}, isAdmin bool) []*bxcore.PushStatisticsData {
  112. result := &map[int64]*PushData{}
  113. for k, v := range *users {
  114. (*result)[common.Int64All(v["id"])] = &PushData{
  115. PersonName: fmt.Sprintf("%s_%d", common.InterfaceToStr(v["name"]), k),
  116. DepartmentName: common.InterfaceToStr(v["dept_name"]),
  117. entUserId: common.InterfaceToStr(v["id"]),
  118. }
  119. }
  120. if data != nil && len(*data) > 0 {
  121. for _, v := range *data {
  122. userId := int64(0)
  123. if isAdmin {
  124. userId = common.Int64All(v["ent_user_id"])
  125. } else {
  126. userId = common.Int64All(v["position_id"])
  127. }
  128. if (*result)[userId] != nil {
  129. //浏览总数处理处理
  130. project_id := common.InterfaceToStr(common.InterfaceToStr(v["project_id"]))
  131. if common.Int64All(v["isvisit"]) > 0 {
  132. (*result)[userId].BrowseNumb = DataHanle((*result)[userId].BrowseNumb, project_id)
  133. }
  134. //推送总数处理
  135. (*result)[userId].PushNumb = DataHanle((*result)[userId].PushNumb, project_id)
  136. //参标总数处理
  137. if common.Int64All(v["isparticipate"]) > 0 {
  138. (*result)[userId].ParticipateNumb = DataHanle((*result)[userId].ParticipateNumb, project_id)
  139. }
  140. //投标总数处理
  141. if common.Int64All(v["isbid"]) > 0 {
  142. (*result)[userId].BidNumb = DataHanle((*result)[userId].BidNumb, project_id)
  143. }
  144. //中标总数处理
  145. if common.Int64All(v["win_status"]) != 0 {
  146. win_status := common.Int64All(v["win_status"])
  147. if win_status == 1 {
  148. (*result)[userId].WinNumb = DataHanle((*result)[userId].WinNumb, project_id)
  149. } else {
  150. delete((*result)[userId].WinNumb, project_id)
  151. }
  152. }
  153. }
  154. }
  155. }
  156. pushStatisticsList := make([]*bxcore.PushStatisticsData, len(*result))
  157. for _, v := range *result {
  158. personName := strings.Split(v.PersonName, "_")[0]
  159. k := common.Int64All(strings.Split(v.PersonName, "_")[1])
  160. pushStatisticsList[k] = &bxcore.PushStatisticsData{
  161. PersonName: personName,
  162. DepartmentName: v.DepartmentName,
  163. EntUserId: encrypt.SE.Encode2Hex(v.entUserId),
  164. PushNumb: common.Int64All(len(v.PushNumb)),
  165. ParticipateNumb: common.Int64All(len(v.ParticipateNumb)),
  166. BidNumb: common.Int64All(len(v.BidNumb)),
  167. WinNumb: common.Int64All(len(v.WinNumb)),
  168. BrowseNumb: common.Int64All(len(v.BrowseNumb)),
  169. }
  170. }
  171. return pushStatisticsList
  172. }
  173. func ProjectHandle(data *[]map[string]interface{}, users *[]map[string]interface{}, isAdmin bool) []*bxcore.ProjectStatisticsData {
  174. result := &map[int64]*ProjectData{}
  175. for k, v := range *users {
  176. (*result)[common.Int64All(v["id"])] = &ProjectData{
  177. PersonName: fmt.Sprintf("%s_%d", common.InterfaceToStr(v["name"]), k),
  178. DepartmentName: common.InterfaceToStr(v["dept_name"]),
  179. entUserId: common.InterfaceToStr(v["id"]),
  180. }
  181. }
  182. if data != nil && len(*data) > 0 {
  183. for _, v := range *data {
  184. userId := int64(0)
  185. if isAdmin {
  186. userId = common.Int64All(v["ent_user_id"])
  187. } else {
  188. userId = common.Int64All(v["position_id"])
  189. }
  190. project_id := common.InterfaceToStr(common.InterfaceToStr(v["project_id"]))
  191. if (*result)[userId] != nil {
  192. //投标数量
  193. if common.Int64All(v["isbid"]) > 0 {
  194. (*result)[userId].BidNumb = DataHanle((*result)[userId].BidNumb, project_id)
  195. }
  196. //直接投标数
  197. if common.Int64All(v["bid_way"]) > 0 {
  198. if common.Int64All(v["bid_way"]) == 2 {
  199. (*result)[userId].ChannelBidNumb = DataHanle((*result)[userId].ChannelBidNumb, project_id)
  200. delete((*result)[userId].DirectBidNumb, project_id)
  201. } else {
  202. (*result)[userId].DirectBidNumb = DataHanle((*result)[userId].DirectBidNumb, project_id)
  203. delete((*result)[userId].ChannelBidNumb, project_id)
  204. }
  205. }
  206. //中标总数处理
  207. if common.Int64All(v["win_status"]) != 0 {
  208. if common.Int64All(v["win_status"]) > 0 {
  209. //中标数量
  210. (*result)[userId].WinNumb = DataHanle((*result)[userId].WinNumb, project_id)
  211. delete((*result)[userId].NotBidNumber, project_id)
  212. } else {
  213. //未中标数量
  214. (*result)[userId].NotBidNumber = DataHanle((*result)[userId].NotBidNumber, project_id)
  215. delete((*result)[userId].WinNumb, project_id)
  216. }
  217. }
  218. //中标方式处理
  219. if common.Int64All(v["win_bidway"]) != 0 {
  220. if common.Int64All(v["win_bidway"]) == 1 {
  221. //直接投标数量
  222. (*result)[userId].DirectWinNumb = DataHanle((*result)[userId].DirectWinNumb, project_id)
  223. delete((*result)[userId].ChannelWinNumb, project_id)
  224. } else {
  225. //渠道投标数量
  226. (*result)[userId].ChannelWinNumb = DataHanle((*result)[userId].ChannelWinNumb, project_id)
  227. delete((*result)[userId].DirectWinNumb, project_id)
  228. }
  229. }
  230. //终止参保统计
  231. if common.Int64All(v["isend"]) != 0 {
  232. (*result)[userId].EndNumb = DataHanle((*result)[userId].EndNumb, project_id)
  233. }
  234. }
  235. }
  236. }
  237. projectStatisticsList := make([]*bxcore.ProjectStatisticsData, len(*result))
  238. for _, v := range *result {
  239. personName := strings.Split(v.PersonName, "_")[0]
  240. k := common.Int64All(strings.Split(v.PersonName, "_")[1])
  241. projectStatisticsList[k] = &bxcore.ProjectStatisticsData{
  242. PersonName: personName,
  243. DepartmentName: v.DepartmentName,
  244. EntUserId: encrypt.SE.Encode2Hex(v.entUserId),
  245. BidNumb: common.Int64All(len(v.BidNumb)),
  246. DirectBidNumb: common.Int64All(len(v.DirectBidNumb)),
  247. ChannelBidNumb: common.Int64All(len(v.ChannelBidNumb)),
  248. WinNumb: common.Int64All(len(v.WinNumb)),
  249. DirectWinNumb: common.Int64All(len(v.DirectWinNumb)),
  250. ChannelWinNumb: common.Int64All(len(v.ChannelWinNumb)),
  251. NotBidNumber: common.Int64All(len(v.NotBidNumber)),
  252. EndNumb: common.Int64All(len(v.EndNumb)),
  253. }
  254. }
  255. return projectStatisticsList
  256. }
  257. func EntInfo(entId, entUserId int) *CurrentUser {
  258. currentUser := &CurrentUser{
  259. Dept: &Department{},
  260. }
  261. user := IC.MainMysql.SelectBySql(`SELECT a.name as user_name from entniche_user a INNER JOIN entniche_user_role b on (a.id=b.user_id) where a.id=? and b.role_id=? limit 1`, entUserId, Role_admin_system)
  262. if user != nil && len(*user) > 0 {
  263. currentUser.Role_admin_system = true
  264. currentUser.User_name, _ = (*user)[0]["user_name"].(string)
  265. currentUser.User_power = 1
  266. r := IC.MainMysql.SelectBySql(`SELECT id,name,subdis,nodiff from entniche_department where ent_id=? and pid=0 limit 1`, entId)
  267. if r != nil && len(*r) == 1 {
  268. department := JsonUnmarshal((*r)[0], &Department{}).(*Department)
  269. if department != nil {
  270. department.Pid = department.Id
  271. currentUser.Dept = department
  272. }
  273. }
  274. } else {
  275. //角色、权限
  276. r := IC.MainMysql.SelectBySql(`SELECT a.name as user_name,a.power as user_power,b.role_id,d.id as dept_id,d.name as dept_name,d.subdis as dept_subdis,d.nodiff as dept_nodiff,e.id as dept_pid from entniche_user a
  277. LEFT JOIN entniche_user_role b on (b.user_id=?)
  278. INNER JOIN entniche_department_user c on (a.id=? and a.id=c.user_id)
  279. INNER JOIN entniche_department d on (c.dept_id=d.id)
  280. INNER JOIN entniche_department e on (e.ent_id=? and e.pid=0)
  281. order by a.id desc limit 1`, entUserId, entUserId, entId)
  282. if r != nil && len(*r) == 1 {
  283. currentUser.User_name, _ = (*r)[0]["user_name"].(string)
  284. currentUser.User_power = common.IntAll((*r)[0]["user_power"])
  285. if common.IntAll((*r)[0]["role_id"]) == Role_admin_department {
  286. currentUser.Role_admin_department = true
  287. }
  288. currentUser.Dept.Id = common.IntAll((*r)[0]["dept_id"])
  289. currentUser.Dept.Pid = common.IntAll((*r)[0]["dept_pid"])
  290. currentUser.Dept.Name = common.ObjToString((*r)[0]["dept_name"])
  291. currentUser.Dept.Subdis = common.IntAll((*r)[0]["dept_subdis"])
  292. currentUser.Dept.Nodiff = common.IntAll((*r)[0]["dept_nodiff"])
  293. }
  294. }
  295. return currentUser
  296. }
  297. // map转结构体
  298. func JsonUnmarshal(m interface{}, s interface{}) interface{} {
  299. var b []byte
  300. if v, ok := m.(string); ok {
  301. b = []byte(v)
  302. } else if v, ok := m.([]byte); ok {
  303. b = v
  304. } else {
  305. b, _ = json.Marshal(m)
  306. }
  307. json.Unmarshal(b, &s)
  308. return s
  309. }
  310. // 获取部门下可以进行分发的人员(不包含部门名称和部门id)
  311. func GetDisUsers(entId, deptId int) *[]map[string]interface{} {
  312. r := IC.MainMysql.SelectBySql(`select DISTINCT c.id,c.name,c.phone,c.power,b.dept_id,d.name as dept_name
  313. from entniche_department_parent a
  314. INNER JOIN entniche_department_user b on (b.dept_id=? or (a.pid=? and a.id=b.dept_id))
  315. INNER JOIN entniche_user c on (c.ent_id=? and b.user_id=c.id)
  316. INNER JOIN entniche_department d on d.id=b.dept_id
  317. order by b.dept_id , convert(c.name using gbk) COLLATE gbk_chinese_ci asc`, deptId, deptId, entId)
  318. return r
  319. }
  320. func GetUser(entUserIdArr []string) *[]map[string]interface{} {
  321. r := IC.MainMysql.SelectBySql("SELECT DISTINCT a.id, a.name, a.phone, d.id AS dept_id, d.NAME AS dept_name " +
  322. "FROM entniche_user a " +
  323. " INNER JOIN entniche_department_user b ON FIND_IN_SET(a.id,'" + strings.Join(entUserIdArr, ",") +
  324. "') and b.user_id = a.id " +
  325. " INNER JOIN entniche_department d ON d.id = b.dept_id " +
  326. " ORDER BY d.id, CONVERT ( a.NAME USING gbk ) COLLATE gbk_chinese_ci ASC",
  327. )
  328. return r
  329. }
  330. type User struct {
  331. Id int
  332. Name string //员工姓名
  333. Mail string //邮箱
  334. Phone string //手机号
  335. Dept_id int //部门id
  336. Dept_name string //部门名称
  337. //Role string //角色
  338. Power int //权限
  339. }
  340. type CurrentUser struct {
  341. Role_admin_department bool //是否是部门管理员
  342. Role_admin_system bool //是否是系统管理员
  343. Dept *Department //部门信息
  344. BondPhone string //手机号
  345. NickName string //昵称
  346. HeadImageUrl string //头像
  347. PersonalAuth int //个人认证
  348. PersonalAuthReason string //个人认证不通过原因
  349. User_power int //是否分配权限
  350. User_name string //用户姓名
  351. }
  352. type Department struct {
  353. Id int
  354. Name string //部门名
  355. Pid int //上级部门id
  356. Pname string //上级部门名称
  357. Nodiff int //全员无差别接收 0:关闭 1:打开
  358. Subdis int //订阅分发 0:关闭 1:打开
  359. Aid int //管理员id
  360. Aname string //管理员姓名
  361. Rname string //角色名
  362. User_count int //该部门下员工的总数
  363. Dept_count int //该部门下子部门总数
  364. Ent_id int //公司id
  365. }
  366. func DataHanle(data map[string]interface{}, project_id string) map[string]interface{} {
  367. if data == nil {
  368. data = map[string]interface{}{project_id: 1}
  369. } else {
  370. data[project_id] = 1
  371. }
  372. return data
  373. }
  374. func QueryHandle(isAdmin bool, startTime, endTime int64, personArrStr string) []string {
  375. //时间处理
  376. query := []string{}
  377. if isAdmin {
  378. //是管理员
  379. query = append(query, fmt.Sprintf(" ent_user_id in (%s) ", personArrStr))
  380. } else {
  381. //不是管理员
  382. query = append(query, fmt.Sprintf(" position_id = %s ", personArrStr))
  383. }
  384. if startTime == 0 && endTime == 0 {
  385. //没有传时间,默认时间处理
  386. var start = time.Now().AddDate(0, 0, -30)
  387. query = append(query, fmt.Sprintf(" ymd >= %s ", start.Format("20060102")))
  388. }
  389. if startTime != 0 {
  390. query = append(query, fmt.Sprintf(" ymd >= %d ", startTime))
  391. }
  392. if endTime != 0 {
  393. query = append(query, fmt.Sprintf(" ymd <= %d ", endTime))
  394. }
  395. return query
  396. }