AchievementLogic.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * 业绩目标逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-29
  7. */
  8. namespace app\crm\logic;
  9. use think\Db;
  10. class AchievementLogic
  11. {
  12. /**
  13. * 获取部门业绩列表
  14. *
  15. * @param $param
  16. * @return bool|\PDOStatement|string|\think\Collection
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function getDepartmentList($param)
  22. {
  23. # 获取部门及全部子部门ID
  24. $departmentIds = $this->getDepartmentIds($param['structure_id']);
  25. $departmentWhere['year'] = $param['year'];
  26. $departmentWhere['status'] = $param['type'];
  27. $departmentWhere['type'] = 2;
  28. $departmentWhere['obj_id'] = ['in', $departmentIds];
  29. # 获取部门数据
  30. $departments = db('admin_structure')->field(['id', 'name'])->select();
  31. # 处理部门数据
  32. $departmentData = [];
  33. foreach ($departments AS $key => $value) {
  34. $departmentData[$value['id']] = $value['name'];
  35. }
  36. # 获取部门业绩数据
  37. $achievements = Db::name('crm_achievement')->where($departmentWhere)->select();
  38. # 处理业绩数据
  39. foreach ($achievements AS $key => $value) {
  40. if (!empty($departmentData[$value['obj_id']])) $achievements[$key]['name'] = $departmentData[$value['obj_id']];
  41. }
  42. return $achievements;
  43. }
  44. /**
  45. * 获取员工业绩列表
  46. *
  47. * @param $param
  48. * @return array|bool|\PDOStatement|string|\think\Collection
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. public function getEmployeeList($param)
  54. {
  55. $userWhere['year'] = $param['year'];
  56. $userWhere['status'] = $param['type'];
  57. $userWhere['type'] = 3;
  58. if (!empty($param['user_id'])) {
  59. $userWhere['obj_id'] = $param['user_id'];
  60. } else {
  61. # 获取部门及全部子部门ID
  62. $departmentIds = $this->getDepartmentIds($param['structure_id']);
  63. # 获取部门下的员工
  64. $userIds = Db::name('admin_user')->whereIn('structure_id', $departmentIds)->column('id');
  65. if (empty($userIds)) return [];
  66. # 设置员工条件
  67. $departmentWhere['obj_id'] = ['in', $userIds];
  68. }
  69. # 获取员工数据
  70. $users = db('admin_user')->field(['id', 'realname'])->select();
  71. # 处理员工数据
  72. $userData = [];
  73. foreach ($users AS $key => $value) {
  74. $userData[$value['id']] = $value['realname'];
  75. }
  76. # 获取业绩数据
  77. $achievements = Db::name('crm_achievement')->where($userWhere)->select();
  78. # 处理业绩数据
  79. foreach ($achievements AS $key => $value) {
  80. if (!empty($userData[$value['obj_id']])) $achievements[$key]['name'] = $userData[$value['obj_id']];
  81. }
  82. return $achievements;
  83. }
  84. /**
  85. * 获取部门及全部子部门ID
  86. *
  87. * @param $id
  88. * @return array
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @throws \think\exception\DbException
  92. */
  93. public function getDepartmentIds($id)
  94. {
  95. $result[] = $id;
  96. # 父级ID数组
  97. $parentIds[] = $id;
  98. # 查询部门数据
  99. $list = Db::name('admin_structure')->select();
  100. foreach ($list AS $key => $value) {
  101. if (!in_array($value['pid'], $parentIds)) continue;
  102. $parentIds[] = $value['id'];
  103. $result[] = $value['id'];
  104. }
  105. return $result;
  106. }
  107. }