ApproveService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\ApplyConstant;
  4. use app\common\constant\CommonConstant;
  5. use app\common\model\Approve;
  6. use app\common\model\ApproveApplyGoods;
  7. use app\common\model\ApproveCopy;
  8. use app\common\model\ApproveEvectionPeerUser;
  9. use app\common\model\ApproveInfo;
  10. use app\common\model\ApproveInfoLog;
  11. use app\common\model\ApproveMaintain;
  12. use app\common\model\ApproveStockGoods;
  13. use app\common\model\ApproveUseGoods;
  14. use app\common\model\GoodsStock;
  15. use app\common\model\GoodsStockLog;
  16. use app\common\model\User;
  17. use think\cache\driver\Redis;
  18. use think\Db;
  19. use think\Exception;
  20. /**
  21. * 审批服务类
  22. */
  23. class ApproveService
  24. {
  25. /**
  26. * 审批待处理统计
  27. *
  28. * @param mixed $user 用户信息
  29. **/
  30. public static function get_count(User $user)
  31. {
  32. $get_module_list = CommonConstant::get_module_list();
  33. $modules = array_keys($get_module_list);
  34. $userid = $user['userid'];
  35. $ids = Approve::where('status', CommonConstant::STATUS_2)
  36. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  37. ->where('approve_user', $userid)
  38. ->column('info_id');
  39. $key = [];
  40. if ($ids) {
  41. $list = ApproveInfo::field('module,count(id) as number')
  42. ->where('id', 'in', $ids)
  43. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  44. ->where('module', 'in', $modules)
  45. ->where('status', CommonConstant::STATUS_2)
  46. ->group('module')
  47. ->select()
  48. ->toArray();
  49. $key = $list ? array_column($list, null, 'module') : [];
  50. }
  51. $data = [];
  52. foreach ($get_module_list as $module => $value) {
  53. $module_text = $value;
  54. $number = 0;
  55. if (array_key_exists($module, $key)) {
  56. $number = $key[$module]['number'];
  57. }
  58. $params = compact('module', "module_text", 'number');
  59. $data[] = $params;
  60. }
  61. return $data;
  62. }
  63. /**
  64. * 审批列表
  65. *
  66. * @param integer $status 处理状态
  67. * @param integer $module 模块类型
  68. * @param string $start_time 申请开始时间
  69. * @param string $end_time 申请结束时间
  70. * @param string $search 搜索
  71. * @param integer $offset 起始位置
  72. * @param integer $length 查询数量
  73. * @param mixed $user 用户信息
  74. **/
  75. public static function get_list($status, $module, $start_time, $end_time, $search, $offset, $length, $user)
  76. {
  77. if (!in_array($status, [1, 2, 3])) {
  78. return [];
  79. }
  80. if ($module && !array_key_exists($module, CommonConstant::get_module_list())) {
  81. return [];
  82. }
  83. $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
  84. $userid = $user['userid'];
  85. $group = $status == 3 ? 'approve_copy' : 'approve';
  86. if ($group == 'approve') {
  87. $model = Approve::class;
  88. $field = 'id as approve_id,info_id,status,create_at';
  89. } else {
  90. $model = ApproveCopy::class;
  91. $field = 'id as approve_id,info_id,create_at';
  92. }
  93. $aliasName = 'approve.';
  94. $jobName = 'approveInfo.';
  95. $joinField = 'module,user_id,apply_user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
  96. $field = CommonService::getAliasField($field, $aliasName);
  97. $joinField = CommonService::getAliasField($joinField, $jobName);
  98. $field = implode(',', array_merge($field, $joinField));
  99. $list = $model::alias('approve')
  100. ->field($field)
  101. ->when($status, function ($query) use ($aliasName, $status) {
  102. if ($status == 1) {
  103. $query->where($aliasName . 'status', CommonConstant::STATUS_2);
  104. // $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2]);
  105. }
  106. if ($status == 2) {
  107. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
  108. }
  109. })
  110. ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
  111. ->when($group, function ($query) use ($aliasName, $group) {
  112. if ($group == 'approve') {
  113. $query->where($aliasName . 'group', CommonConstant::IS_WHO_0);
  114. }
  115. })
  116. ->when($group, function ($query) use ($aliasName, $group) {
  117. if ($group == 'approve') {
  118. $query->where($aliasName . 'state', CommonConstant::IS_WHO_0);
  119. }
  120. })
  121. ->where($aliasName . 'approve_user', $userid)
  122. ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
  123. ->when($module, function ($query) use ($jobName, $module) {
  124. $query->where($jobName . 'module', 'in', $module);
  125. })
  126. ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
  127. $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
  128. })
  129. ->when($userids, function ($query) use ($jobName, $userids) {
  130. $query->where($jobName . 'user_id', 'in', $userids);
  131. })
  132. ->when($search, function ($query) use ($search, $userids) {
  133. if (!$userids) {
  134. if (!is_numeric($search)) {
  135. $query->where('order_no|reason', 'like', '%' . $search . '%');
  136. } else {
  137. $query->where('order_no', 'like', '%' . $search . '%');
  138. }
  139. }
  140. })
  141. ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
  142. if ($status == 1) {
  143. // 待处理(待审批 审批中) 才关联 审批人信息
  144. $list->with([
  145. 'approveInfoUser' => function ($query) {
  146. $query->field('userid,name');
  147. },
  148. 'approveOne' => function ($query) {
  149. $query->field('id,info_id,status,approve_user')
  150. ->where('status', CommonConstant::STATUS_2)
  151. ->where('group', CommonConstant::IS_WHO_0)
  152. ->where('state', CommonConstant::IS_WHO_0)
  153. ->with([
  154. 'user' => function ($query) {
  155. $query->field('userid,name');
  156. }
  157. ]);
  158. }
  159. ]);
  160. } else {
  161. $list->with([
  162. 'approveInfoUser' => function ($query) {
  163. $query->field('userid,name');
  164. }
  165. ]);
  166. }
  167. $list = $list->order('approve_id desc')
  168. ->limit($offset, $length)
  169. ->select();
  170. return self::get_array($list);
  171. }
  172. public static function get_array($list)
  173. {
  174. $apply_goods_info_ids = [];
  175. $stock_goods_info_ids = [];
  176. $use_goods_info_ids = [];
  177. $peer_user_info_ids = [];
  178. $apply_goods_list = [];
  179. $stock_goods_list = [];
  180. $use_goods_list = [];
  181. $peer_user_list = [];
  182. foreach ($list as $value) {
  183. $module = $value['module'];
  184. switch ($module) {
  185. case CommonConstant::MODULE_1:
  186. if ($value['type'] == ApplyConstant::TYPE_1) {
  187. $apply_goods_info_ids[] = $value['info_id'];
  188. }
  189. break;
  190. case CommonConstant::MODULE_3:
  191. $stock_goods_info_ids[] = $value['info_id'];
  192. break;
  193. case CommonConstant::MODULE_4:
  194. $use_goods_info_ids[] = $value['info_id'];
  195. break;
  196. case CommonConstant::MODULE_5:
  197. $peer_user_info_ids[] = $value['info_id'];
  198. break;
  199. }
  200. }
  201. if ($apply_goods_info_ids) {
  202. $apply_goods_list = ApproveApplyGoods::field('id,info_id,goods_name')
  203. ->where('info_id', 'in', $apply_goods_info_ids)
  204. ->select()
  205. ->toArray();
  206. }
  207. if ($stock_goods_info_ids) {
  208. $stock_goods_list = ApproveStockGoods::field('id,info_id,goods_name')
  209. ->where('info_id', 'in', $stock_goods_info_ids)
  210. ->select()
  211. ->toArray();
  212. }
  213. if ($use_goods_info_ids) {
  214. $use_goods_list = ApproveUseGoods::field('id,info_id,goods_name')
  215. ->where('info_id', 'in', $use_goods_info_ids)
  216. ->select()
  217. ->toArray();
  218. }
  219. if ($peer_user_info_ids) {
  220. $peer_user_list = ApproveEvectionPeerUser::field('id,info_id,name')
  221. ->where('info_id', 'in', $peer_user_info_ids)
  222. ->select()
  223. ->toArray();
  224. }
  225. foreach ($list as $value) {
  226. $module = $value['module'];
  227. $apply_goods = [];
  228. $stock_goods = [];
  229. $use_goods = [];
  230. $peer_user = [];
  231. switch ($module) {
  232. case CommonConstant::MODULE_1:
  233. if ($value['type'] == ApplyConstant::TYPE_1) {
  234. foreach ($apply_goods_list as $val) {
  235. if ($value['info_id'] == $val['info_id']) {
  236. $apply_goods[] = $val;
  237. }
  238. }
  239. }
  240. break;
  241. case CommonConstant::MODULE_3:
  242. foreach ($stock_goods_list as $val) {
  243. if ($value['info_id'] == $val['info_id']) {
  244. $stock_goods[] = $val;
  245. }
  246. }
  247. break;
  248. case CommonConstant::MODULE_4:
  249. foreach ($use_goods_list as $val) {
  250. if ($value['info_id'] == $val['info_id']) {
  251. $use_goods[] = $val;
  252. }
  253. }
  254. break;
  255. case CommonConstant::MODULE_5:
  256. foreach ($peer_user_list as $val) {
  257. if ($value['info_id'] == $val['info_id']) {
  258. $peer_user[] = $val;
  259. }
  260. }
  261. break;
  262. }
  263. $value['apply_goods'] = $apply_goods;
  264. $value['stock_goods'] = $stock_goods;
  265. $value['use_goods'] = $use_goods;
  266. $value['peer_user'] = $peer_user;
  267. }
  268. return $list;
  269. // $info_ids = [];
  270. // foreach ($list as $value){
  271. // if($value['status'] == CommonConstant::STATUS_1) {
  272. // $info_ids[] = $value['info_id'];
  273. // }
  274. // }
  275. // if($info_ids){
  276. // $approve_one_list = Approve::field('id,info_id,status,approve_user')
  277. // ->where('info_id','in',$info_ids)
  278. // ->where('status',CommonConstant::STATUS_2)
  279. // ->where('is_deleted', CommonConstant::IS_DELETED_0)
  280. // ->with(['user' => function ($query) {
  281. // $query->field('userid,name');
  282. // }])
  283. // ->select();
  284. // $approve_one_data = array_column($approve_one_list, null, 'info_id');
  285. // foreach ($list as $val){
  286. // if(array_key_exists($val['info_id'],$approve_one_data)){
  287. // $val['approve_one'] = $approve_one_data[$val['info_id']];
  288. // }
  289. // }
  290. // }
  291. }
  292. /**
  293. * 详情
  294. *
  295. * @param integer $id 审批ID
  296. * @param mixed $user 用户信息
  297. * @param string $group 类别:approve=审批,approve_copy=审批抄送
  298. * @param string $type 类型:detail=详情,info=信息
  299. **/
  300. public static function get_detail($id, $user, $group, $type)
  301. {
  302. $userid = $user['userid'];
  303. if ($group == 'approve') {
  304. $model = Approve::class;
  305. $field = 'id,info_id,status';
  306. } else {
  307. $model = ApproveCopy::class;
  308. $field = 'id,info_id';
  309. }
  310. $info = $model::field($field)
  311. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  312. ->when($group, function ($query) use ($group) {
  313. if ($group == 'approve') {
  314. $query->where('group', CommonConstant::IS_WHO_0);
  315. }
  316. })
  317. ->when($group, function ($query) use ($group) {
  318. if ($group == 'approve') {
  319. $query->where('state', CommonConstant::IS_WHO_0);
  320. }
  321. })
  322. ->where('approve_user', $userid)
  323. ->find($id);
  324. $data = [];
  325. $is_maintain = false;
  326. $is_feedback = false;
  327. if ($info) {
  328. $data = ApproveInfoService::get_detail($info['info_id'], $user, CommonConstant::IS_WHO_0, $type);
  329. if ($data) {
  330. $data['approve_id'] = $id;
  331. $data['approve_status'] = isset($info['status']) ? $info['status'] : CommonConstant::STATUS_1;
  332. if ($group == 'approve') {
  333. // 条件:维修模块 审批的审批状态是审批中 用户是物业主管或信息负责人 是否显示维修人员:false=不显示,true=显示 目的:审批
  334. // 条件:维修模块 审批申请的审批状态是审批同意 用户是物业主管或信息负责人 反馈状态 是否显示上传反馈结果:false=不显示,true=显示 目的:上传反馈结果
  335. if ($data['module'] == CommonConstant::MODULE_8) {
  336. if ($data['approve_status'] == CommonConstant::STATUS_2) {
  337. $is_maintain = CommonService::isMaintain($user);
  338. }
  339. if ($data['status'] == CommonConstant::STATUS_3) {
  340. // if (CommonService::isMaintain($user) && $data['maintain_user_id'] > 0 && $data['module_info']['feedback_status'] == CommonConstant::IS_WHO_0) {
  341. if (CommonService::isMaintain($user) && $data['module_info']['feedback_status'] == CommonConstant::IS_WHO_0) {
  342. $is_feedback = true;
  343. }
  344. }
  345. }
  346. }
  347. $data['is_maintain'] = $is_maintain;
  348. $data['is_feedback'] = $is_feedback;
  349. }
  350. }
  351. return $data;
  352. }
  353. /**
  354. * 操作方法
  355. *
  356. * @param integer $id 审批ID
  357. * @param array $params 数组
  358. * @param mixed $user 用户信息
  359. * @param string $type 类型:audit=审批,edit=修改,feedback=上传反馈结果
  360. **/
  361. public static function make($id, $params, $user, $type)
  362. {
  363. $userid = $user['userid'];
  364. $approve = Approve::field('id,info_id,status,approve_user,approve_flow,approve_time')
  365. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  366. ->where('group', CommonConstant::IS_WHO_0)
  367. ->where('state', CommonConstant::IS_WHO_0)
  368. ->where('approve_user', $userid)
  369. ->with([
  370. 'approveInfo' => function ($query) {
  371. $query->field('is_deleted', true);
  372. }
  373. ])
  374. ->find($id);
  375. switch ($type) {
  376. case 'audit':
  377. if (!$approve) {
  378. except('审批记录不存在或已删除');
  379. }
  380. if ($approve->status != CommonConstant::STATUS_2) {
  381. except('非待处理状态无法操作');
  382. }
  383. $info = $approve->approve_info;
  384. if (!$info) {
  385. except('申请记录不存在或已删除');
  386. }
  387. if ($info->status != CommonConstant::STATUS_2) {
  388. except('非待处理状态无法操作!');
  389. }
  390. self::audit($approve, $info, $params, $user);
  391. break;
  392. case 'edit':
  393. if (!$approve) {
  394. except('审批记录不存在或已删除');
  395. }
  396. if ($approve->status != CommonConstant::STATUS_2) {
  397. except('非待处理状态无法操作');
  398. }
  399. $info = $approve->approve_info;
  400. if (!$info) {
  401. except('申请记录不存在或已删除');
  402. }
  403. if ($info->status != CommonConstant::STATUS_2) {
  404. except('非待处理状态无法操作!');
  405. }
  406. $info->module_info;
  407. if (!$info->module_info) {
  408. except(CommonConstant::get_module_list()[$params['module']] . '记录不存在或已删除');
  409. }
  410. $info = ApproveInfoService::get_item($info, $type);
  411. self::edit($approve, $info, $params);
  412. break;
  413. case 'feedback':
  414. if (!$approve) {
  415. except('审批记录不存在或已删除');
  416. }
  417. if ($approve->status != CommonConstant::STATUS_3) {
  418. except('非审批同意状态无法操作');
  419. }
  420. $info = $approve->approve_info;
  421. if (!$info) {
  422. except('申请记录不存在或已删除');
  423. }
  424. if ($info->status != CommonConstant::STATUS_3) {
  425. except('非审批同意状态无法操作!');
  426. }
  427. if ($info->module != CommonConstant::MODULE_8) {
  428. except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
  429. }
  430. $is_maintain = CommonService::isMaintain($user);
  431. if (!$is_maintain) {
  432. except('您没有权限操作');
  433. }
  434. // TODO 没有判断反馈状态
  435. try {
  436. // 更新维修信息
  437. $params['maintain_user_id'] = $info->maintain_user_id;
  438. $params['feedback_status'] = CommonConstant::IS_WHO_1;
  439. ApproveMaintain::where('info_id', $info->id)->update($params);
  440. } catch (Exception $e) {
  441. except('出现错误:' . $e->getMessage());
  442. }
  443. break;
  444. }
  445. return true;
  446. }
  447. /**
  448. * 审批
  449. *
  450. * @param mixed $approve 审批信息
  451. * @param mixed $info 申请信息
  452. * @param array $params 数组
  453. * @param mixed $user 用户信息
  454. **/
  455. public static function audit($approve, $info, $params, $user)
  456. {
  457. $status = $params['status'];
  458. $remark = $params['remark'];
  459. $maintain_user_id = $params['maintain_user_id'];
  460. $approve_time = date('Y-m-d H:i:s');
  461. $duration = time() - strtotime($approve['approve_time']);
  462. $module = $info->module;
  463. $goodsStockSave = [];
  464. $goodsStockLog = [];
  465. $redisStock = [];
  466. if ($module == CommonConstant::MODULE_4) {
  467. // 4=领用申请
  468. $goods = $info->use_goods;
  469. if (!$goods->toArray()) {
  470. except('领用商品不存在');
  471. }
  472. if ($status == CommonConstant::STATUS_3) {
  473. if ($approve['approve_flow'] < $info['approve_num']) {
  474. } else {
  475. list($goodsStockSave, $goodsStockLog, $redisStock) = ApproveInfoService::composition_data($goods->toArray(), CommonConstant::MODULE_4, CommonConstant::STATUS_3);
  476. }
  477. }
  478. if ($status == CommonConstant::STATUS_4) {
  479. list($goodsStockSave, $goodsStockLog, $redisStock) = ApproveInfoService::composition_data($goods->toArray(), CommonConstant::MODULE_4, CommonConstant::STATUS_4);
  480. }
  481. }
  482. if ($module == CommonConstant::MODULE_3 && $status == CommonConstant::STATUS_3) {
  483. // 3=入库申请
  484. $goods = $info->stock_goods;
  485. if (!$goods->toArray()) {
  486. except('入库商品不存在');
  487. }
  488. if ($approve['approve_flow'] < $info['approve_num']) {
  489. } else {
  490. list($goodsStockSave, $goodsStockLog, $redisStock) = ApproveInfoService::composition_data($goods->toArray(), CommonConstant::MODULE_3, CommonConstant::STATUS_3);
  491. }
  492. }
  493. if ($module == CommonConstant::MODULE_8 && $status == CommonConstant::STATUS_3) {
  494. // 8=维修申请
  495. $is_maintain = CommonService::isMaintain($user);
  496. if ($is_maintain && !$maintain_user_id) {
  497. except('请选择维修人员');
  498. }
  499. // 维修人员ID
  500. if ($maintain_user_id > 0) {
  501. $info->maintain_user_id = $maintain_user_id;
  502. }
  503. }
  504. Db::startTrans();
  505. try {
  506. if ($goodsStockSave) {
  507. // 批量更新商品库存
  508. (new GoodsStock)->saveAll($goodsStockSave);
  509. }
  510. if ($goodsStockLog) {
  511. // 批量添加商品入库出库记录
  512. GoodsStockLog::insertAll($goodsStockLog);
  513. }
  514. // 更新审批申请
  515. if ($status == CommonConstant::STATUS_3) {
  516. // 审批流程批次小于审批次数 更新下一级审批状态
  517. if ($approve['approve_flow'] < $info['approve_num']) {
  518. Approve::where(['info_id' => $approve['info_id'], 'approve_flow' => $approve['approve_flow'] + 1])->update(['status' => CommonConstant::STATUS_2, 'approve_time' => $approve_time]);
  519. } else {
  520. // 审批通过
  521. $info->status = $status;
  522. }
  523. }
  524. if ($status == CommonConstant::STATUS_4) {
  525. // 审批驳回
  526. $info->status = $status;
  527. }
  528. $info->cur_num = $info['cur_num'] + 1;
  529. $info->save();
  530. // 更新审批
  531. $approve->status = $status;
  532. $approve->approve_time = $approve_time;
  533. $approve->remark = $remark;
  534. $approve->time = $duration;
  535. $approve->time_text = get_stay_time($duration);
  536. $approve->save();
  537. Db::commit();
  538. } catch (Exception $e) {
  539. except('出现错误:' . $e->getMessage(), 2, $e);
  540. }
  541. // 4=领用申请 扣除冻结库存
  542. if ($module == CommonConstant::MODULE_4) {
  543. if ($redisStock) {
  544. // 库存缓存记录
  545. $redis = new Redis(config('redis.'));
  546. foreach ($redisStock as $key => $value) {
  547. $redis->dec($value['id'], $value['stock']);
  548. }
  549. }
  550. }
  551. return true;
  552. }
  553. /**
  554. * 修改
  555. *
  556. * @param mixed $approve 审批信息
  557. * @param mixed $info 申请信息
  558. * @param array $params 数组
  559. **/
  560. public static function edit($approve, $info, $params)
  561. {
  562. $module = $info->module;
  563. $before = $info->toArray();
  564. $copy_user = $params['copy_user'] ? explode(',', $params['copy_user']) : [];
  565. $data = [
  566. 'reason' => $params['reason'],
  567. 'type' => $params['type'],
  568. 'desc' => $params['desc'],
  569. 'start_time' => $params['module'] == CommonConstant::MODULE_6 ? $params['start_time'] . ' ' . $params['start_am'] : $params['start_time'],
  570. 'end_time' => $params['module'] == CommonConstant::MODULE_6 ? $params['end_time'] . ' ' . $params['end_am'] : $params['end_time'],
  571. 'apply_id' => $params['apply_id'],
  572. ];
  573. Db::startTrans();
  574. try {
  575. // 编辑对应模块
  576. $result = ApproveInfoService::create_module($module, $params, $info->id, $info, $module == CommonConstant::MODULE_4 ? 'edit' : 'update');
  577. // 编辑抄送人
  578. ApproveInfoService::create_approve_copy($copy_user, $info->id, 'update');
  579. // 编辑审批申请
  580. if ($module == CommonConstant::MODULE_9) {
  581. // 9=合同呈批 不编辑合同编号
  582. unset($data['reason'],$data['type']);
  583. }
  584. $info->save($data);
  585. // 保存修改记录
  586. self::create_approve_info_log($approve->approve_user,$info->id, $module, $before, $info, $result);
  587. Db::commit();
  588. } catch (Exception $e) {
  589. except('出现错误:' . $e->getMessage(), 2, $e);
  590. }
  591. // 4=领用申请 领用商品库存扣除重新计算
  592. if ($module == CommonConstant::MODULE_4) {
  593. list($goodsStockSave, $redisStock) = [$result['other']['goodsStockSave'],$result['other']['redisStock']];
  594. if ($goodsStockSave) {
  595. // 批量更新商品库存
  596. (new GoodsStock)->saveAll($goodsStockSave);
  597. }
  598. if ($redisStock) {
  599. // 库存缓存记录
  600. $redis = new Redis(config('redis.'));
  601. foreach ($redisStock as $key => $value) {
  602. if($value['exp'] == 'inc'){
  603. $redis->inc($value['id'], $value['stock']);
  604. }
  605. if($value['exp'] == 'dec'){
  606. $redis->dec($value['id'], $value['stock']);
  607. }
  608. }
  609. }
  610. }
  611. return true;
  612. }
  613. /**
  614. * 保存修改记录
  615. *
  616. * @param integer $user_id 审批人userid
  617. * @param integer $info_id 审批申请ID
  618. * @param array $module 模块类型
  619. * @param array $before 数组
  620. * @param array $info 数组
  621. * @param array $params 数组
  622. **/
  623. public static function create_approve_info_log($user_id, $info_id, $module, $before, $info, $params)
  624. {
  625. unset($info['module_info'], $info['data'], $info['apply_goods'], $info['stock_goods'], $info['use_goods'], $info['peer_user'], $info['contract'], $info['apply']);
  626. $info['module_info'] = $params['module_info'];
  627. $info['data'] = isset($params['other']['data']) && !empty($params['other']['data']) ? $params['other']['data'] : [];
  628. $data = [
  629. 'user_id' => $user_id,
  630. 'info_id' => $info_id,
  631. 'module' => $module,
  632. 'before' => json_encode($before, JSON_UNESCAPED_UNICODE),
  633. 'after' => json_encode($info, JSON_UNESCAPED_UNICODE),
  634. ];
  635. ApproveInfoLog::create($data);
  636. return true;
  637. }
  638. }