SystemUserApplyServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\dao\system\SystemUserApplyDao;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\system\SystemRoleDao;
  16. use app\services\supplier\SystemSupplierServices;
  17. use crmeb\services\FormBuilder as Form;
  18. use think\exception\ValidateException;
  19. use think\facade\Route as Url;
  20. /**
  21. * Class SystemUserApplyServices
  22. * @package app\services\system
  23. * @mixin SystemRoleDao
  24. */
  25. class SystemUserApplyServices extends BaseServices
  26. {
  27. /**
  28. * @var string[]
  29. */
  30. protected $status_name = [
  31. 0 => '未处理',
  32. 1 => '已审核',
  33. 2 => '未通过',
  34. ];
  35. /**
  36. * SystemUserApplyServices constructor.
  37. * @param SystemUserApplyDao $dao
  38. */
  39. public function __construct(SystemUserApplyDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 所有申请记录列表
  45. * @param array $where
  46. * @param string $field
  47. * @return array
  48. */
  49. public function getApplyList(array $where, string $field = '*')
  50. {
  51. [$page, $limit] = $this->getPageValue();
  52. $list = $this->dao->getList($where, $field, ['user'], $page, $limit);
  53. foreach ($list as &$item) {
  54. $item['nickname'] = $item['user']['nickname'] ?? '';
  55. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', (int)$item['add_time']) : '';
  56. $item['status_name'] = $this->status_name[$item['status']] ?? '未处理';
  57. }
  58. $count = $this->dao->count($where);
  59. return compact('list', 'count');
  60. }
  61. /**
  62. * 用户申请记录
  63. * @param int $uid
  64. * @param int $type
  65. * @return array
  66. */
  67. public function getUserApply(int $uid, int $type = 2)
  68. {
  69. [$page, $limit] = $this->getPageValue();
  70. $where = ['uid' => $uid, 'type' => $type, 'is_del' => 0];
  71. $list = $this->dao->getList($where, '*', ['user'], $page, $limit);
  72. foreach ($list as &$item) {
  73. $item['nickname'] = $item['user']['nickname'] ?? '';
  74. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', (int)$item['add_time']) : '';
  75. }
  76. return $list;
  77. }
  78. /**
  79. * 添加申请
  80. * @param int $id
  81. * @param int $uid
  82. * @param $data
  83. * @param int $type
  84. * @return int|mixed
  85. */
  86. public function saveApply(int $id, int $uid, $data, int $type = 2)
  87. {
  88. $data['uid'] = $uid;
  89. $data['add_time'] = time();
  90. $data['type'] = $type;
  91. if ($id) {
  92. $data['status'] = 0;
  93. $data['status_time'] = 0;
  94. $data['fail_msg'] = '';
  95. $this->dao->update($id, $data);
  96. } else {
  97. $res = $this->dao->save($data);
  98. if (!$res) {
  99. throw new ValidateException('保存失败,请稍后再试');
  100. }
  101. $id = $res->id;
  102. }
  103. return $id;
  104. }
  105. /**
  106. * 审核表单
  107. * @param int $id
  108. * @return mixed
  109. */
  110. public function verifyForm(int $id)
  111. {
  112. $info = $this->dao->get($id);
  113. if (!$info) {
  114. throw new ValidateException('申请记录不存在');
  115. }
  116. $f = [];
  117. $f[] = Form::radio('status', '审核状态', 1)->options([['value' => 1, 'label' => '通过'], ['value' => 2, 'label' => '拒绝']])->appendControl(2, [
  118. Form::textarea('fail_msg', '拒绝原因')->required('请输入拒绝原因')
  119. ]);
  120. return create_form('供应商申请审核', $f, Url::buildUrl('/supplier/apply/verify/' . $id), 'post');
  121. }
  122. /**
  123. * 备注表单
  124. * @param int $id
  125. * @return mixed
  126. */
  127. public function markForm(int $id)
  128. {
  129. $info = $this->dao->get($id);
  130. if (!$info) {
  131. throw new ValidateException('申请记录不存在');
  132. }
  133. $info = $info->toArray();
  134. $f = [];
  135. $f[] = Form::textarea('mark', '备注', $info['mark'])->required('请输入拒绝原因');
  136. return create_form('供应商申请备注', $f, Url::buildUrl('/supplier/apply/mark/' . $id), 'post');
  137. }
  138. /**
  139. * 审核申请记录
  140. * @param int $id
  141. * @param array $data
  142. * @param int $type
  143. * @return mixed
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function verifyApply(int $id, array $data, int $type = 2)
  149. {
  150. $info = $this->dao->get($id);
  151. if (!$info) {
  152. throw new ValidateException('申请记录不存在');
  153. }
  154. $info = $info->toArray();
  155. if ($info['status'] != 0) {
  156. throw new ValidateException('请不要重复审核');
  157. }
  158. if (!isset($data['status']) || !in_array($data['status'], [1, 2])) {
  159. throw new ValidateException('审核状态异常,请稍后重试');
  160. }
  161. $res = $this->transaction(function () use ($id, $info, $data, $type) {
  162. $result = [];
  163. if ($data['status'] == 1) {//通过
  164. switch ($type) {
  165. case 2://供应商
  166. /** @var SystemSupplierServices $systemSupplierServices */
  167. $systemSupplierServices = app()->make(SystemSupplierServices::class);
  168. $result = $systemSupplierServices->verifyAgreeCreate($id, $info);
  169. break;
  170. }
  171. }
  172. $data['relation_id'] = $result['id'] ?? 0;
  173. $data['status_time'] = time();
  174. $res = $this->dao->update($id, $data);
  175. if (!$res) {
  176. throw new ValidateException('审核保存失败');
  177. }
  178. return $info;
  179. });
  180. event('supplier.verify', [$res, $data['status']]);
  181. return $res;
  182. }
  183. }