ApproveService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 integer $status 处理状态
  17. * @param integer $module 模块类型
  18. * @param string $start_time 申请开始时间
  19. * @param string $end_time 申请结束时间
  20. * @param string $search 搜索
  21. * @param integer $offset 起始位置
  22. * @param integer $length 查询数量
  23. * @param mixed $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 as approve_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('approve_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 审批ID
  121. * @param mixed $user 用户信息
  122. * @param string $type 类型:detail=详情,info=信息
  123. **/
  124. public static function get_detail($id, $user, $type)
  125. {
  126. $userid = $user['userid'];
  127. $info = Approve::field('id,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. * @param integer $id 审批ID
  143. * @param string $status 审批状态
  144. * @param string $remark 审批意见
  145. * @param mixed $user 用户信息
  146. **/
  147. public static function audit($id, $status, $remark, $user)
  148. {
  149. $userid = $user['userid'];
  150. $info = Approve::field('id,info_id,status,approve_flow,approve_time')
  151. ->where('approve_user', $userid)
  152. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  153. ->with([
  154. 'approveInfo' => function ($query) {
  155. $query->field('is_deleted', true);
  156. }
  157. ])
  158. ->find($id);
  159. if (!$info) {
  160. throw new Exception('审批记录不存在或已删除');
  161. }
  162. if ($info->status != CommonConstant::STATUS_2) {
  163. throw new Exception('非待处理状态无法操作');
  164. }
  165. $approve_info = $info->approve_info;
  166. if (!$approve_info) {
  167. throw new Exception(CommonConstant::get_module_list()[$approve_info->module] . '记录不存在或已删除');
  168. }
  169. if ($approve_info->status != CommonConstant::STATUS_2) {
  170. throw new Exception('非待处理状态无法操作!');
  171. }
  172. $approve_time = date('Y-m-d H:i:s');
  173. $duration = time() - strtotime($info['approve_time']);
  174. // 更新审批
  175. $info->status = $status;
  176. $info->approve_time = $approve_time;
  177. $info->remark = $remark;
  178. $info->time = $duration;
  179. $info->time_text = get_stay_time($duration);
  180. $info->save();
  181. // 更新审批申请
  182. if ($status == CommonConstant::STATUS_3) {
  183. if ($info['approve_flow'] < $approve_info['approve_num']) {
  184. // 更新下一级审批状态
  185. Approve::where(['info_id' => $info['info_id'], 'approve_flow' => $info['approve_flow'] + 1])->update(['status' => CommonConstant::STATUS_2, 'approve_time' => $approve_time]);
  186. } else {
  187. $approve_info->status = $status;
  188. }
  189. } else {
  190. $approve_info->status = $status;
  191. }
  192. $approve_info->cur_num = $approve_info['cur_num'] + 1;
  193. $approve_info->save();
  194. }
  195. /**
  196. * 修改
  197. *
  198. * @param integer $id 审批ID
  199. * @param integer $module 模块类型
  200. * @param array $params 数组
  201. * @param mixed $user 用户信息
  202. **/
  203. public static function edit($id, $module, $params, $user)
  204. {
  205. $userid = $user['userid'];
  206. }
  207. /**
  208. * 拼接查询字段
  209. */
  210. public static function getAliasField($field, $aliasName)
  211. {
  212. $field = explode(',', $field);
  213. foreach ($field as &$value) {
  214. $value = $aliasName . $value;
  215. }
  216. return $field;
  217. }
  218. }