EvectionInfoService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\CommonConstant;
  4. use app\common\model\Department;
  5. use app\common\model\EvectionApprove;
  6. use app\common\model\EvectionInfo;
  7. use app\common\model\EvectionPeerUser;
  8. use app\common\model\User;
  9. use think\Exception;
  10. /**
  11. * 出差申请服务类
  12. */
  13. class EvectionInfoService
  14. {
  15. /**
  16. * 申请出差/重新发起
  17. *
  18. * @param $id
  19. * @param $params
  20. * @param $user
  21. **/
  22. public static function create($id,$params, $user)
  23. {
  24. $userid = $user['userid'];
  25. // 编辑
  26. if ($id > 0) {
  27. $info = EvectionInfo::field('user_id,is_deleted', true)
  28. ->where('user_id', $userid)
  29. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  30. ->find($id);
  31. if (!$info) {
  32. throw new Exception('出差记录不存在或已删除');
  33. }
  34. if($info->status != CommonConstant::STATUS_3){
  35. throw new Exception('非审批驳回状态无法操作');
  36. }
  37. }
  38. $order_no = get_order_sn($user->id);
  39. $approve_user = explode(',', $params['approve_user']);
  40. $copy_user = explode(',', $params['copy_user']);
  41. $approve_num = count($approve_user);
  42. $data = [
  43. 'user_id' => $userid,
  44. 'order_no' => $order_no,
  45. 'apply_date' => date("Y-m-d"),
  46. 'reason' => $params['reason'],
  47. 'start_time' => $params['start_time'],
  48. 'end_time' => $params['end_time'],
  49. 'document' => $params['document'],
  50. 'images' => $params['images'],
  51. 'type' => $params['type'],
  52. 'is_who' => $params['is_who'],
  53. 'remark' => $params['remark'],
  54. 'approve_num' => $approve_num,
  55. 'cur_num' => 0,
  56. ];
  57. if ($id > 0) {
  58. // 编辑出差申请
  59. $save_data = [
  60. 'status' => CommonConstant::STATUS_1,
  61. 'create_at' => date('Y-m-d H:i:s'),
  62. ];
  63. $data = array_merge($data, $save_data);
  64. $info->save($data);
  65. // 编辑同行人员
  66. self::create_peer_user($id, $params['peer_user'],'update');
  67. // 编辑出差审批
  68. self::create_evection_approve($id, $approve_user, $copy_user,'create');
  69. } else {
  70. // 添加出差申请
  71. $info = EvectionInfo::create($data);
  72. // 添加同行人员
  73. self::create_peer_user($info->id, $params['peer_user']);
  74. // 添加出差审批
  75. self::create_evection_approve($info->id, $approve_user, $copy_user);
  76. }
  77. }
  78. protected static function create_peer_user($info_id, $params,$type = 'create')
  79. {
  80. if (!$params) {
  81. return true;
  82. }
  83. if($type == 'update'){
  84. EvectionPeerUser::where('info_id', $info_id)->delete();
  85. }
  86. $data = [];
  87. foreach ($params as $value) {
  88. $data[] = [
  89. 'info_id' => $info_id,
  90. 'is_who' => $value['is_who'],
  91. 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
  92. 'name' => $value['name'],
  93. 'desc' => isset($value['desc']) ? $value['desc'] : '',
  94. ];
  95. }
  96. EvectionPeerUser::insertAll($data);
  97. }
  98. protected static function create_evection_approve($info_id, $approve_user, $copy_user,$type = 'create')
  99. {
  100. if($type == 'update'){
  101. EvectionApprove::where('info_id', $info_id)->delete();
  102. }
  103. $data = [];
  104. $flow_num = 0;
  105. $create_at = date('Y-m-d H:i:s');
  106. // 审批人
  107. foreach ($approve_user as $key => $value) {
  108. $flow_num++;
  109. $data[] = [
  110. 'info_id' => $info_id,
  111. 'approve_flow' => $flow_num,
  112. 'approve_type' => CommonConstant::TYPE_1,
  113. 'approve_user' => $value,
  114. 'approve_time' => $create_at,
  115. 'create_at' => $create_at,
  116. 'status' => $flow_num == 1 ? CommonConstant::STATUS_1 : CommonConstant::STATUS_0,
  117. ];
  118. }
  119. // 抄送人
  120. foreach ($copy_user as $key => $value) {
  121. $data[] = [
  122. 'info_id' => $info_id,
  123. 'approve_flow' => 0,
  124. 'approve_type' => CommonConstant::TYPE_2,
  125. 'approve_user' => $value,
  126. 'approve_time' => $create_at,
  127. 'create_at' => $create_at,
  128. 'status' => CommonConstant::STATUS_0,
  129. ];
  130. }
  131. EvectionApprove::insertAll($data);
  132. }
  133. /**
  134. * 列表
  135. *
  136. * @param $status
  137. * @param $search
  138. * @param $offset
  139. * @param $length
  140. * @param $user
  141. **/
  142. public static function get_list($status, $search, $offset, $length, $user)
  143. {
  144. $userid = $user['userid'];
  145. $list = EvectionInfo::field('id,status,create_at,order_no,reason,start_time,end_time')
  146. ->where('user_id', $userid)
  147. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  148. ->when(array_key_exists($status, CommonConstant::get_status_list()), function ($query) use ($status) {
  149. $query->where('status', $status);
  150. })
  151. ->when($search, function ($query) use ($search) {
  152. $query->where('order_no|reason', 'like', '%' . $search . '%');
  153. })
  154. ->with(['peer_user' => function ($query) {
  155. $query->field('id,info_id,name');
  156. }, 'approve_info' => function ($query) use ($status) {
  157. $query->field('id,info_id,status,approve_user')
  158. ->where('status', $status)
  159. ->with(['user' => function ($query) {
  160. $query->field('userid,name');
  161. }]);
  162. }])
  163. ->limit($offset, $length)
  164. ->order('id desc')
  165. ->select();
  166. return $list;
  167. }
  168. /**
  169. * 详情/信息
  170. *
  171. * @param $id
  172. * @param $user
  173. * @param $type
  174. **/
  175. public static function get_detail($id, $user,$type)
  176. {
  177. $userid = $user['userid'];
  178. if($type == 'detail'){
  179. $info = EvectionInfo::field('user_id,is_deleted', true)
  180. ->where('user_id', $userid)
  181. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  182. ->with(['peer_user' => function ($query) {
  183. $query->field('id,info_id,name');
  184. }, 'approve' => function ($query) {
  185. $query->field('is_deleted,create_at', true)
  186. ->with(['user' => function ($query) {
  187. $query->field('userid,headimg,name');
  188. }]);
  189. }])
  190. ->find($id);
  191. if ($info) {
  192. $info['department'] = Department::where('dept_id', 'in', $user['department'])->field('dept_id,name')->select();
  193. }
  194. } else{
  195. $info = EvectionInfo::field('user_id,is_deleted', true)
  196. ->where('user_id', $userid)
  197. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  198. ->with(['peer_user'])
  199. ->find($id);
  200. }
  201. return $info;
  202. }
  203. /**
  204. * 操作方法
  205. *
  206. * @param $id
  207. * @param $user
  208. * @param $type
  209. */
  210. public static function make($id, $user,$type)
  211. {
  212. $userid = $user['userid'];
  213. $info = EvectionInfo::field('id,status')
  214. ->where('user_id', $userid)
  215. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  216. ->find($id);
  217. if(!$info) {
  218. throw new Exception('出差记录不存在或已删除');
  219. }
  220. switch ($type){
  221. case 'urging':
  222. if($info->status != CommonConstant::STATUS_1){
  223. throw new Exception('其他人已操作');
  224. }
  225. // 钉钉接口 TODO
  226. break;
  227. case 'cancel':
  228. if($info->status != CommonConstant::STATUS_3){
  229. throw new Exception('非审批驳回状态无法操作');
  230. }
  231. $info->status = CommonConstant::STATUS_4;
  232. $info->save();
  233. break;
  234. }
  235. }
  236. }