ApproveService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\CommonConstant;
  4. use app\common\model\Approve;
  5. use app\common\model\ApproveInfo;
  6. use app\common\model\User;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * 审批服务类
  11. */
  12. class ApproveService
  13. {
  14. /**
  15. * 审批待处理统计
  16. *
  17. * @param mixed $user 用户信息
  18. **/
  19. public static function get_count(User $user)
  20. {
  21. $get_module_list = CommonConstant::get_module_list();
  22. $modules = array_keys($get_module_list);
  23. $userid = $user['userid'];
  24. $ids = Approve::where('status', CommonConstant::STATUS_2)
  25. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  26. ->where('approve_user', $userid)
  27. ->column('info_id');
  28. $object = [];
  29. if ($ids) {
  30. $list = ApproveInfo::field('module,count(id) as number')
  31. ->where('id', 'in', $ids)
  32. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  33. ->where('module', 'in', $modules)
  34. ->where('status', CommonConstant::STATUS_2)
  35. ->group('module')
  36. ->select();
  37. $object = $list ? array_column($list->toArray(), null, 'module') : [];
  38. }
  39. $data = [];
  40. foreach ($get_module_list as $module => $value) {
  41. $module_text = $value;
  42. $number = 0;
  43. if (array_key_exists($module, $object)) {
  44. $number = $object[$module]['number'];
  45. }
  46. $params = compact('module', "module_text", 'number');
  47. $data[] = $params;
  48. }
  49. return $data;
  50. }
  51. /**
  52. * 审批列表
  53. *
  54. * @param integer $status 处理状态
  55. * @param integer $module 模块类型
  56. * @param string $start_time 申请开始时间
  57. * @param string $end_time 申请结束时间
  58. * @param string $search 搜索
  59. * @param integer $offset 起始位置
  60. * @param integer $length 查询数量
  61. * @param mixed $user 用户信息
  62. **/
  63. public static function get_list($status, $module, $start_time, $end_time, $search, $offset, $length, $user)
  64. {
  65. if (!in_array($status, [1, 2, 3])) {
  66. return [];
  67. }
  68. if ($module && !array_key_exists($module, CommonConstant::get_module_list())) {
  69. return [];
  70. }
  71. $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
  72. $userid = $user['userid'];
  73. $aliasName = 'approve.';
  74. $jobName = 'approveInfo.';
  75. $field = 'id as approve_id,info_id,status,create_at';
  76. $joinField = 'module,user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
  77. $field = self::getAliasField($field, $aliasName);
  78. $joinField = self::getAliasField($joinField, $jobName);
  79. $field = implode(',', array_merge($field, $joinField));
  80. $list = Approve::alias('approve')
  81. ->field($field)
  82. ->when($status, function ($query) use ($aliasName, $status) {
  83. if ($status == 1) {
  84. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2]);
  85. }
  86. if ($status == 2) {
  87. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
  88. }
  89. })
  90. ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
  91. ->where($aliasName . 'approve_user', $userid)
  92. ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
  93. ->when($module, function ($query) use ($jobName, $module) {
  94. $query->where($jobName . 'module', 'in', $module);
  95. })
  96. ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
  97. $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
  98. })
  99. ->when($userids, function ($query) use ($jobName, $userids) {
  100. $query->where($jobName . 'user_id', 'in', $userids);
  101. })
  102. ->when($search, function ($query) use ($search, $userids) {
  103. if (!$userids) {
  104. if (!is_numeric($search)) {
  105. $query->where('order_no|reason', 'like', '%' . $search . '%');
  106. } else {
  107. $query->where('order_no', 'like', '%' . $search . '%');
  108. }
  109. }
  110. })
  111. ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
  112. if ($status == 1) {
  113. // 待处理(待审批 审批中) 才关联 审批人信息
  114. $list->with(['approveInfoUser' => function ($query) {
  115. $query->field('userid,name');
  116. }, 'approveOne' => function ($query) {
  117. $query->field('id,info_id,status,approve_user')
  118. ->where('status', CommonConstant::STATUS_2)
  119. ->with(['user' => function ($query) {
  120. $query->field('userid,name');
  121. }]);
  122. }]);
  123. } else {
  124. $list->with(['approveInfoUser' => function ($query) {
  125. $query->field('userid,name');
  126. }]);
  127. }
  128. $list = $list->limit($offset, $length)
  129. ->order('approve_id desc')
  130. ->select();
  131. // $info_ids = [];
  132. // foreach ($list as $value){
  133. // if($value['status'] == CommonConstant::STATUS_1) {
  134. // $info_ids[] = $value['info_id'];
  135. // }
  136. // }
  137. // if($info_ids){
  138. // $approve_one_list = Approve::field('id,info_id,status,approve_user')
  139. // ->where('info_id','in',$info_ids)
  140. // ->where('status',CommonConstant::STATUS_2)
  141. // ->where('is_deleted', CommonConstant::IS_DELETED_0)
  142. // ->with(['user' => function ($query) {
  143. // $query->field('userid,name');
  144. // }])
  145. // ->select();
  146. // $approve_one_data = array_column($approve_one_list, null, 'info_id');
  147. // foreach ($list as $val){
  148. // if(array_key_exists($val['info_id'],$approve_one_data)){
  149. // $val['approve_one'] = $approve_one_data[$val['info_id']];
  150. // }
  151. // }
  152. // }
  153. return $list;
  154. }
  155. /**
  156. * 详情
  157. *
  158. * @param integer $id 审批ID
  159. * @param mixed $user 用户信息
  160. * @param string $type 类型:detail=详情,info=信息
  161. **/
  162. public static function get_detail($id, $user, $type)
  163. {
  164. $userid = $user['userid'];
  165. $info = Approve::field('id,info_id,status')
  166. ->where('approve_user', $userid)
  167. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  168. ->find($id);
  169. $data = [];
  170. if ($info) {
  171. $data = ApproveInfoService::get_detail($info['info_id'], '', $type);
  172. $data['approve_id'] = $id;
  173. $data['approve_status'] = $info['status'];
  174. }
  175. return $data;
  176. }
  177. /**
  178. * 操作方法
  179. *
  180. * @param integer $id 审批ID
  181. * @param array $params 数组
  182. * @param mixed $user 用户信息
  183. * @param string $type 类型:audit=审批,edit=修改
  184. **/
  185. public static function make($id, $params, $user, $type)
  186. {
  187. $userid = $user['userid'];
  188. $info = Approve::field('id,info_id,status,approve_flow,approve_time')
  189. ->where('approve_user', $userid)
  190. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  191. ->with([
  192. 'approveInfo' => function ($query) {
  193. $query->field('user_id,is_deleted', true);
  194. }
  195. ])
  196. ->find($id);
  197. if (!$info) {
  198. throw new Exception('审批记录不存在或已删除');
  199. }
  200. if ($info->status != CommonConstant::STATUS_2) {
  201. throw new Exception('非待处理状态无法操作');
  202. }
  203. $approve_info = $info->approve_info;
  204. if (!$approve_info) {
  205. throw new Exception(CommonConstant::get_module_list()[$approve_info->module] . '记录不存在或已删除');
  206. }
  207. if ($approve_info->status != CommonConstant::STATUS_2) {
  208. throw new Exception('非待处理状态无法操作!');
  209. }
  210. switch ($type) {
  211. case 'audit':
  212. self::audit($info, $params['status'], $params['remark']);
  213. break;
  214. case 'edit':
  215. self::edit($info, $params['module'], $params);
  216. break;
  217. }
  218. }
  219. /**
  220. * 审批
  221. *
  222. * @param mixed $info 审批信息
  223. * @param string $status 审批状态
  224. * @param string $remark 审批意见
  225. **/
  226. public static function audit($info, $status, $remark)
  227. {
  228. $approve_info = $info->approve_info;
  229. $approve_time = date('Y-m-d H:i:s');
  230. $duration = time() - strtotime($info['approve_time']);
  231. // 更新审批
  232. $info->status = $status;
  233. $info->approve_time = $approve_time;
  234. $info->remark = $remark;
  235. $info->time = $duration;
  236. $info->time_text = get_stay_time($duration);
  237. $info->save();
  238. // 更新审批申请
  239. if ($status == CommonConstant::STATUS_3) {
  240. if ($info['approve_flow'] < $approve_info['approve_num']) {
  241. // 更新下一级审批状态
  242. Approve::where(['info_id' => $info['info_id'], 'approve_flow' => $info['approve_flow'] + 1])->update(['status' => CommonConstant::STATUS_2, 'approve_time' => $approve_time]);
  243. } else {
  244. $approve_info->status = $status;
  245. }
  246. } else {
  247. $approve_info->status = $status;
  248. }
  249. $approve_info->cur_num = $approve_info['cur_num'] + 1;
  250. $approve_info->save();
  251. }
  252. /**
  253. * 修改
  254. *
  255. * @param mixed $info 审批信息
  256. * @param integer $module 模块类型
  257. * @param array $params 数组
  258. **/
  259. public static function edit($info, $module, $params)
  260. {
  261. $approve_info = $info->approve_info;
  262. $approve_info->module_info;
  263. $data = [
  264. 'reason' => $params['reason'],
  265. 'type' => $params['type'],
  266. 'desc' => $params['desc'],
  267. 'start_time' => $params['start_time'],
  268. 'end_time' => $params['end_time'],
  269. ];
  270. // 编辑出差申请
  271. $approve_info->save($data);
  272. // 编辑对应模块
  273. ApproveInfoService::create_module($approve_info->id, $module, $params, $approve_info, 'update');
  274. }
  275. /**
  276. * 拼接查询字段
  277. */
  278. public static function getAliasField($field, $aliasName)
  279. {
  280. $field = explode(',', $field);
  281. foreach ($field as &$value) {
  282. $value = $aliasName . $value;
  283. }
  284. return $field;
  285. }
  286. }