participateStatistics.go 15 KB

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