participateStatistics.go 15 KB

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