ApproveInfoService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\CommonConstant;
  4. use app\common\model\Approve;
  5. use app\common\model\ApproveCopy;
  6. use app\common\model\ApproveEvectionPeerUser;
  7. use app\common\model\ApproveInfo;
  8. use app\common\model\Department;
  9. use app\common\model\User;
  10. use think\Db;
  11. use think\Exception;
  12. /**
  13. * 审批申请服务类
  14. */
  15. class ApproveInfoService
  16. {
  17. /**
  18. * 申请/重新发起
  19. *
  20. * @param integer $id 申请ID
  21. * @param integer $module 模块类型
  22. * @param array $params
  23. * @param mixed $user 用户信息
  24. **/
  25. public static function create($id, $module, $params, $user)
  26. {
  27. $userid = $user['userid'];
  28. // 编辑
  29. if ($id > 0) {
  30. $info = ApproveInfo::field('user_id,is_deleted', true)
  31. ->where('user_id', $userid)
  32. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  33. ->with(['moduleInfo'])
  34. ->find($id);
  35. if (!$info) {
  36. throw new Exception('申请记录不存在或已删除');
  37. }
  38. if (!$info['module_info']) {
  39. throw new Exception(CommonConstant::get_module_list()[$module] . '记录不存在或已删除');
  40. }
  41. if ($info->status != CommonConstant::STATUS_4) {
  42. throw new Exception('非审批驳回状态无法操作');
  43. }
  44. }
  45. $order_no = get_order_sn($user['id']);
  46. $approve_user = explode(',', $params['approve_user']);
  47. $copy_user = explode(',', $params['copy_user']);
  48. $approve_num = count($approve_user);
  49. $data = [
  50. 'module' => $module,
  51. 'user_id' => $userid,
  52. 'department' => $user['department'],
  53. 'status' => CommonConstant::STATUS_2,
  54. 'approve_num' => $approve_num,
  55. 'cur_num' => 0,
  56. 'order_no' => $order_no,
  57. 'apply_date' => date("Y-m-d"),
  58. 'reason' => $params['reason'],
  59. 'start_time' => $params['start_time'],
  60. 'end_time' => $params['end_time'],
  61. ];
  62. if ($id > 0) {
  63. // 编辑出差申请
  64. $save_data = [
  65. 'create_at' => date('Y-m-d H:i:s'),
  66. ];
  67. $data = array_merge($data, $save_data);
  68. $info->save($data);
  69. // 编辑对应模块
  70. self::create_module($id, $module, $params, $info, 'update');
  71. // 编辑审批抄送
  72. self::create_approve($id, $approve_user, $copy_user, 'update');
  73. } else {
  74. // 添加审批申请
  75. $info = ApproveInfo::create($data);
  76. $info_id = $info->id;
  77. // 添加对应模块
  78. $module_data = self::create_module($info_id, $module, $params, [], 'create');
  79. $info->save(['module_id' => $module_data->id]);
  80. // 添加审批抄送
  81. self::create_approve($info_id, $approve_user, $copy_user, 'create');
  82. }
  83. }
  84. // 添加对应模块
  85. public static function create_module($info_id, $module, $params, $info, $type)
  86. {
  87. $data['info_id'] = $info_id;
  88. $data['reason'] = $params['reason'];
  89. $data['start_time'] = $params['start_time'];
  90. $data['end_time'] = $params['end_time'];
  91. $data['document'] = $params['document'];
  92. $data['images'] = $params['images'];
  93. $data['type'] = $params['type'];
  94. $data['is_who'] = $params['is_who'];
  95. $data['remark'] = $params['remark'];
  96. if ($type == 'update') {
  97. $info->module_info->save($data);
  98. } else {
  99. $models = CommonConstant::get_module_model_list();
  100. $model = $models[$module];
  101. $module_data = $model::create($data);
  102. }
  103. switch ($module) {
  104. case CommonConstant::MODULE_5:
  105. self::create_peer_user($info_id, $params['peer_user'], $type);
  106. break;
  107. }
  108. return $module_data;
  109. }
  110. // 添加同行人员
  111. public static function create_peer_user($info_id, $params, $type)
  112. {
  113. if (!$params) {
  114. return true;
  115. }
  116. if ($type == 'update') {
  117. ApproveEvectionPeerUser::where('info_id', $info_id)->delete();
  118. }
  119. $data = [];
  120. foreach ($params as $value) {
  121. $data[] = [
  122. 'info_id' => $info_id,
  123. 'is_who' => $value['is_who'],
  124. 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
  125. 'name' => $value['name'],
  126. 'desc' => isset($value['desc']) ? $value['desc'] : '',
  127. ];
  128. }
  129. ApproveEvectionPeerUser::insertAll($data);
  130. }
  131. // 添加审批抄送
  132. public static function create_approve($info_id, $approve_user, $copy_user, $type)
  133. {
  134. if ($type == 'update') {
  135. Approve::where('info_id', $info_id)->delete();
  136. ApproveCopy::where('info_id', $info_id)->delete();
  137. }
  138. $data = [];
  139. $copy_data = [];
  140. $flow_num = 0;
  141. $copy_num = 0;
  142. $create_at = date('Y-m-d H:i:s');
  143. // 审批人
  144. foreach ($approve_user as $key => $value) {
  145. $flow_num++;
  146. $data[] = [
  147. 'info_id' => $info_id,
  148. 'create_at' => $create_at,
  149. 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1,
  150. 'approve_flow' => $flow_num,
  151. 'approve_user' => $value,
  152. 'approve_time' => $create_at,
  153. ];
  154. }
  155. // 抄送人
  156. foreach ($copy_user as $key => $value) {
  157. $copy_num++;
  158. $copy_data[] = [
  159. 'info_id' => $info_id,
  160. 'create_at' => $create_at,
  161. 'approve_flow' => $copy_num,
  162. 'approve_user' => $value,
  163. ];
  164. }
  165. if ($data) {
  166. Approve::insertAll($data);
  167. }
  168. if ($copy_data) {
  169. ApproveCopy::insertAll($copy_data);
  170. }
  171. }
  172. /**
  173. * 我的申请记录
  174. *
  175. * @param integer $module 模块类型
  176. * @param integer $status 审批状态
  177. * @param string $search 搜索
  178. * @param integer$offset 起始位置
  179. * @param integer $length 查询数量
  180. * @param mixed $user 用户信息
  181. **/
  182. public static function get_list($module, $status, $search, $offset, $length, $user)
  183. {
  184. if (!array_key_exists($module, CommonConstant::get_module_list())) {
  185. return [];
  186. }
  187. if (!array_key_exists($status, CommonConstant::get_status_list())) {
  188. return [];
  189. }
  190. $userid = $user['userid'];
  191. $list = ApproveInfo::field('module,module_id,user_id,is_deleted', true)
  192. ->where('module', $module)
  193. ->where('user_id', $userid)
  194. ->where('status', $status)
  195. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  196. ->when($search, function ($query) use ($search) {
  197. $query->where('order_no|reason', 'like', '%' . $search . '%');
  198. });
  199. $list = self::get_with($list, $module, $status);
  200. $list = $list->limit($offset, $length)
  201. ->order('id desc')
  202. ->select();
  203. return $list;
  204. }
  205. /**
  206. * 详情/信息
  207. *
  208. * @param integer $id 申请ID
  209. * @param mixed $user 用户信息 有数据=审批申请详情,没有数据=审批详情
  210. * @param string $type 类型:detail=详情,info=信息
  211. **/
  212. public static function get_detail($id, $user, $type)
  213. {
  214. $where = $user ? ['user_id' => $user['userid']] : [];
  215. $info = ApproveInfo::field('is_deleted', true)
  216. ->where($where)
  217. ->where('is_deleted', CommonConstant::IS_DELETED_0);
  218. if ($type == 'detail') {
  219. $info->with([
  220. 'moduleInfo',
  221. 'approve' => function ($query) {
  222. $query->field('is_deleted,create_at', true)
  223. ->with(['user' => function ($query) {
  224. $query->field('userid,headimg,name');
  225. }]);
  226. },
  227. 'approveCopy' => function ($query) {
  228. $query->field('is_deleted,create_at', true)
  229. ->with(['user' => function ($query) {
  230. $query->field('userid,headimg,name');
  231. }]);
  232. }
  233. ]);
  234. $info = $info->find($id);
  235. if ($info) {
  236. if (!$user) {
  237. // 审批申请人信息
  238. $approve_info_user = User::field('userid,name')->where('userid', 'in', $info['user_id'])->find();
  239. $info['approve_info_user'] = $approve_info_user;
  240. }
  241. $department_data = Department::field('dept_id,name')->where('dept_id', 'in', $info['department'])->select();
  242. $info['department_data'] = $department_data;
  243. }
  244. } else {
  245. $info->with(['moduleInfo']);
  246. $info = $info->find($id);
  247. }
  248. $info = self::get_item($info, $type);
  249. return $info;
  250. }
  251. public static function get_with($list, $module, $status)
  252. {
  253. switch ($module) {
  254. case CommonConstant::MODULE_1:
  255. break;
  256. case CommonConstant::MODULE_3:
  257. break;
  258. case CommonConstant::MODULE_4:
  259. break;
  260. case CommonConstant::MODULE_5:
  261. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  262. // 审批中或审批驳回 才关联 审批人信息
  263. $list = $list->with([
  264. 'peer_user' => function ($query) {
  265. $query->field('id,info_id,name');
  266. },
  267. 'approveOne' => function ($query) use ($status) {
  268. $query->field('id,info_id,status,approve_user')
  269. ->where('status', $status)
  270. ->with(['user' => function ($query) {
  271. $query->field('userid,name');
  272. }]);
  273. }
  274. ]);
  275. } else {
  276. $list = $list->with([
  277. 'peer_user' => function ($query) {
  278. $query->field('id,info_id,name');
  279. },
  280. ]);
  281. }
  282. break;
  283. }
  284. return $list;
  285. }
  286. public static function get_item($info, $type)
  287. {
  288. $module = $info['module'];
  289. switch ($module) {
  290. case CommonConstant::MODULE_1:
  291. break;
  292. case CommonConstant::MODULE_3:
  293. break;
  294. case CommonConstant::MODULE_4:
  295. break;
  296. case CommonConstant::MODULE_5:
  297. if ($type == 'detail') {
  298. $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
  299. } else {
  300. $info->peerUser;
  301. }
  302. break;
  303. }
  304. return $info;
  305. }
  306. /**
  307. * 操作方法
  308. *
  309. * @param integer $id 申请ID
  310. * @param mixed $user 用户信息
  311. * @param string $type 类型:urging=催办,cancel=撤销
  312. */
  313. public static function make($id, $user, $type)
  314. {
  315. $userid = $user['userid'];
  316. $info = ApproveInfo::field('id,module,status')
  317. ->where('user_id', $userid)
  318. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  319. ->find($id);
  320. if (!$info) {
  321. throw new Exception(CommonConstant::get_module_list()[$info->module] . '记录不存在或已删除');
  322. }
  323. switch ($type) {
  324. case 'urging':
  325. if ($info->status != CommonConstant::STATUS_2) {
  326. throw new Exception('其他人已操作');
  327. }
  328. // 钉钉接口 TODO
  329. break;
  330. case 'cancel':
  331. if ($info->status != CommonConstant::STATUS_4) {
  332. throw new Exception('非审批驳回状态无法操作');
  333. }
  334. $info->status = CommonConstant::STATUS_5;
  335. $info->save();
  336. break;
  337. }
  338. }
  339. }