ApproveService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\CommonConstant;
  4. use app\common\model\Approve;
  5. use app\common\model\User;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 审批服务类
  10. */
  11. class ApproveService
  12. {
  13. /**
  14. * 审批列表
  15. *
  16. * @param $status
  17. * @param $module
  18. * @param $start_time
  19. * @param $end_time
  20. * @param $search
  21. * @param $offset
  22. * @param $length
  23. * @param $user
  24. **/
  25. public static function get_list($status, $module, $start_time, $end_time, $search, $offset, $length, $user)
  26. {
  27. if (!in_array($status, [1, 2, 3])) {
  28. return [];
  29. }
  30. if ($module && !array_key_exists($module, CommonConstant::get_module_list())) {
  31. return [];
  32. }
  33. $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
  34. $userid = $user['userid'];
  35. $aliasName = 'approve.';
  36. $jobName = 'approveInfo.';
  37. $field = 'id,info_id,status,create_at';
  38. $joinField = 'user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
  39. $field = self::getAliasField($field, $aliasName);
  40. $joinField = self::getAliasField($joinField, $jobName);
  41. $field = implode(',', array_merge($field, $joinField));
  42. $list = Approve::alias('approve')
  43. ->field($field)
  44. ->when($status, function ($query) use ($aliasName, $status) {
  45. if ($status == 1) {
  46. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2]);
  47. }
  48. if ($status == 2) {
  49. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
  50. }
  51. })
  52. ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
  53. ->where($aliasName . 'approve_user', $userid)
  54. ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
  55. ->when($module, function ($query) use ($jobName, $module) {
  56. $query->where($jobName . 'module', 'in', $module);
  57. })
  58. ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
  59. $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
  60. })
  61. ->when($userids, function ($query) use ($jobName, $userids) {
  62. $query->where($jobName . 'user_id', 'in', $userids);
  63. })
  64. ->when($search, function ($query) use ($search, $userids) {
  65. if (!$userids) {
  66. if (!is_numeric($search)) {
  67. $query->where('order_no|reason', 'like', '%' . $search . '%');
  68. } else {
  69. $query->where('order_no', 'like', '%' . $search . '%');
  70. }
  71. }
  72. })
  73. ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
  74. if ($status == 1) {
  75. // 待处理(待审批 审批中) 才关联 审批信息
  76. $list->with(['approveInfoUser' => function ($query) {
  77. $query->field('userid,name');
  78. }, 'approveOne' => function ($query) {
  79. $query->field('id,info_id,status,approve_user')
  80. ->where('status', CommonConstant::STATUS_2)
  81. ->with(['user' => function ($query) {
  82. $query->field('userid,name');
  83. }]);
  84. }]);
  85. } else {
  86. $list->with(['approveInfoUser' => function ($query) {
  87. $query->field('userid,name');
  88. }]);
  89. }
  90. $list = $list->limit($offset, $length)
  91. ->order('id desc')
  92. ->select();
  93. // $info_ids = [];
  94. // foreach ($list as $value){
  95. // if($value['status'] == CommonConstant::STATUS_1) {
  96. // $info_ids[] = $value['info_id'];
  97. // }
  98. // }
  99. // if($info_ids){
  100. // $approve_one_list = Approve::field('id,info_id,status,approve_user')
  101. // ->where('info_id','in',$info_ids)
  102. // ->where('status',CommonConstant::STATUS_2)
  103. // ->where('is_deleted', CommonConstant::IS_DELETED_0)
  104. // ->with(['user' => function ($query) {
  105. // $query->field('userid,name');
  106. // }])
  107. // ->select();
  108. // $approve_one_data = array_column($approve_one_list, null, 'info_id');
  109. // foreach ($list as $val){
  110. // if(array_key_exists($val['info_id'],$approve_one_data)){
  111. // $val['approve_one'] = $approve_one_data[$val['info_id']];
  112. // }
  113. // }
  114. // }
  115. return $list;
  116. }
  117. /**
  118. * 详情
  119. *
  120. * @param integer $id
  121. * @param mixed $user
  122. * @param string $type
  123. **/
  124. public static function get_detail($id, $user,$type)
  125. {
  126. $userid = $user['userid'];
  127. $info = Approve::field('info_id,status')
  128. ->where('approve_user', $userid)
  129. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  130. ->find($id);
  131. $data = [];
  132. if ($info) {
  133. $data = ApproveInfoService::get_detail($info['info_id'], '', $type);
  134. $data['approve_id'] = $id;
  135. $data['approve_status'] = $info['status'];
  136. }
  137. return $data;
  138. }
  139. /**
  140. * 拼接查询字段
  141. */
  142. public static function getAliasField($field, $aliasName)
  143. {
  144. $field = explode(',', $field);
  145. foreach ($field as &$value) {
  146. $value = $aliasName . $value;
  147. }
  148. return $field;
  149. }
  150. }