MerchantIntentionRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\system\merchant;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\repositories\system\config\ConfigValueRepository;
  14. use crmeb\jobs\SendSmsJob;
  15. use crmeb\services\SmsService;
  16. use FormBuilder\Factory\Elm;
  17. use app\common\dao\system\merchant\MerchantIntentionDao;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\Queue;
  21. use think\facade\Route;
  22. /**
  23. * 商户申请
  24. */
  25. class MerchantIntentionRepository extends BaseRepository
  26. {
  27. public function __construct(MerchantIntentionDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 根据条件获取商户列表
  33. *
  34. * 本函数用于根据给定的条件数组 $where,获取指定页码 $page 和每页数量 $limit 的商户列表。
  35. * 它首先通过条件搜索查询商户数据,然后计算总记录数,最后分页并排序后返回商户列表。
  36. *
  37. * @param array $where 搜索条件数组
  38. * @param int $page 当前页码
  39. * @param int $limit 每页的记录数
  40. * @return array 包含 'count' 和 'list' 两个元素的数组,'count' 为总记录数,'list' 为分页后的商户列表
  41. */
  42. public function getList(array $where, $page, $limit)
  43. {
  44. // 根据条件搜索商户数据
  45. $query = $this->dao->search($where);
  46. // 计算搜索结果的总记录数
  47. $count = $query->count();
  48. // 进行分页,并按照创建时间降序和状态升序排序,同时加载关联的商户类别和类型信息
  49. $list = $query->page($page, $limit)->order('create_time DESC , status ASC')->with(['merchantCategory', 'merchantType'])->select();
  50. // 返回包含总记录数和分页后的商户列表的数组
  51. return compact('count', 'list');
  52. }
  53. /**
  54. * 获取意向商家详情
  55. *
  56. * 本函数用于根据提供的意向商家ID和可选的用户ID,从数据库中检索对应的意向商家详情。
  57. * 如果提供了用户ID,则只返回该用户相关的意向商家详情;否则,返回所有与指定意向商家ID相关的详情。
  58. *
  59. * @param int $id 意向商家的唯一标识ID
  60. * @param int|null $uid 可选的用户ID,用于过滤结果,仅返回该用户相关的意向商家详情
  61. * @return array|null 返回符合条件的意向商家详情数组,如果未找到则返回null
  62. */
  63. public function detail($id, ?int $uid)
  64. {
  65. // 定义查询条件,初始只包含意向商家ID
  66. $where = ['mer_intention_id' => $id];
  67. // 如果提供了用户ID,则将其添加到查询条件中
  68. if (!is_null($uid)) {
  69. $where['uid'] = $uid;
  70. }
  71. // 执行查询并返回结果
  72. return $this->dao->search($where)->find();
  73. }
  74. /**
  75. * 更新意向商家的信息。
  76. *
  77. * 此方法用于更新意向商家在数据库中的信息。它首先检查该意向商家是否存在以及其状态是否为启用,
  78. * 如果存在且状态为启用,则抛出一个异常,表示当前状态不能修改。然后,它处理图片数据,
  79. * 将图片数组转换为以逗号分隔的字符串,并设置默认的状态、失败消息。最后,它调用DAO层的方法
  80. * 来更新数据库中的记录。
  81. *
  82. * @param int $id 意向商家的ID。
  83. * @param array $data 包含要更新的意向商家信息的数组。
  84. * @return bool 更新操作的结果,true表示成功,false表示失败。
  85. * @throws ValidateException 如果意向商家当前状态不允许修改,则抛出此异常。
  86. */
  87. public function updateIntention($id, array $data)
  88. {
  89. // 检查意向商家是否存在且状态为启用,如果存在则抛出异常
  90. if ($this->dao->existsWhere(['mer_intention_id' => $id, 'status' => '1']))
  91. throw new ValidateException('当前状态不能修改');
  92. // 处理图片数据,将数组转换为以逗号分隔的字符串
  93. $data['images'] = implode(',', (array)$data['images']);
  94. // 设置默认的状态和失败消息
  95. $data['status'] = 0;
  96. $data['fail_msg'] = '';
  97. // 调用DAO层的方法更新数据库中的记录
  98. return $this->dao->update($id, $data);
  99. }
  100. /**
  101. * 标记商家意向的表单生成方法
  102. *
  103. * 本方法用于生成一个用于修改商家意向备注的表单。通过给定的商家意向ID,获取当前备注信息,
  104. * 并构建一个表单以允许用户输入新的备注。表单提交的URL是基于当前商家意向ID动态生成的。
  105. *
  106. * @param int $id 商家意向的唯一标识ID,用于获取当前商家意向的备注信息以及构建表单提交的URL。
  107. * @return \FormBuilder\Form|\Phper6\Elm\Form
  108. */
  109. public function markForm($id)
  110. {
  111. // 通过商家意向ID获取当前商家意向的备注信息
  112. $data = $this->dao->get($id);
  113. // 构建表单提交的URL,使用商家意向ID作为参数
  114. $form = Elm::createForm(Route::buildUrl('systemMerchantIntentionMark', ['id' => $id])->build());
  115. // 设置表单的验证规则,这里仅包含一个文本区域用于输入备注信息
  116. $form->setRule([
  117. Elm::textarea('mark', '备注:', $data['mark'])->placeholder('请输入备注')->required(),
  118. ]);
  119. // 设置表单的标题为“修改备注”
  120. return $form->setTitle('修改备注');
  121. }
  122. /**
  123. * 创建用于修改商户意向状态的表单
  124. *
  125. * 该方法通过Elm库构建一个表单,用于修改商户的意向状态。表单中包含一个选择字段,用于选择审核状态(同意或拒绝)。
  126. * 根据所选的审核状态,表单会动态显示不同的字段:如果选择同意,则需要选择是否自动创建商户;如果选择拒绝,则需要输入拒绝的原因。
  127. *
  128. * @param int $id 商户意向的ID,用于构建表单的提交URL。
  129. * @return \FormBuilder\Form|\think\form\Form
  130. */
  131. public function statusForm($id)
  132. {
  133. // 构建表单提交的URL,使用Route类生成带ID的URL
  134. $form = Elm::createForm(Route::buildUrl('systemMerchantIntentionStatus', ['id' => $id])->build());
  135. // 设置表单的验证规则
  136. $form->setRule([
  137. // 创建一个选择字段,用于选择商户的审核状态
  138. Elm::select('status', '审核状态:', 1)->options([
  139. ['value' => 1, 'label' => '同意'],
  140. ['value' => 2, 'label' => '拒绝'],
  141. ])->control([
  142. // 当选择同意时,显示一个选择字段,用于选择是否自动创建商户
  143. [
  144. 'value' => 1,
  145. 'rule' => [
  146. Elm::radio('create_mer', '自动创建商户:', 1)->options([
  147. ['value' => 1, 'label' => '创建'],
  148. ['value' => 2, 'label' => '不创建'],
  149. ])
  150. ]
  151. ],
  152. // 当选择拒绝时,显示一个文本区域,用于输入拒绝的原因
  153. [
  154. 'value' => 2,
  155. 'rule' => [
  156. Elm::textarea('fail_msg', '失败原因:', '信息填写有误')->placeholder('请输入失败原因')
  157. ]
  158. ]
  159. ]),
  160. ]);
  161. // 设置表单的标题
  162. return $form->setTitle('修改审核状态');
  163. }
  164. /**
  165. * 更新商家意向状态
  166. * 此函数用于处理商家意向的更新操作,包括状态更改、信息完善等。
  167. * @param $id 商家意向ID
  168. * @param $data 更新的数据数组
  169. * @throws ValidateException 当信息不存在或状态不正确时抛出异常
  170. */
  171. public function updateStatus($id, $data)
  172. {
  173. // 判断是否创建商家
  174. $create = $data['create_mer'] == 1;
  175. // 移除创建商家标记
  176. unset($data['create_mer']);
  177. // 根据ID查询商家意向信息
  178. $intention = $this->search(['mer_intention_id' => $id])->find();
  179. // 如果商家意向不存在,抛出异常
  180. if (!$intention)
  181. throw new ValidateException('信息不存在');
  182. // 如果商家意向状态已设置,抛出异常
  183. if ($intention->status)
  184. throw new ValidateException('状态有误,修改失败');
  185. // 获取系统配置信息
  186. $config = systemConfig(['broadcast_room_type', 'broadcast_goods_type']);
  187. // 获取商家类型信息
  188. $margin = app()->make(MerchantTypeRepository::class)->get($intention['mer_type_id']);
  189. // 设置商家意向的保证金相关字段
  190. $data['is_margin'] = $margin['is_margin'] ?? -1;
  191. $data['margin'] = $margin['margin'] ?? 0;
  192. // 初始化商家数据和短信数据数组
  193. $merData = [];
  194. $smsData = [];
  195. // 如果是创建商家
  196. if ($create == 1) {
  197. // 处理商家密码
  198. $password = substr($intention['phone'], -6);
  199. // 构建商家信息数组
  200. $merData = [
  201. 'mer_name' => $intention['mer_name'],
  202. 'mer_phone' => $intention['phone'],
  203. 'mer_account' => $intention['phone'],
  204. 'category_id' => $intention['merchant_category_id'],
  205. 'type_id' => $intention['mer_type_id'],
  206. 'real_name' => $intention['name'],
  207. 'status' => 1,
  208. 'is_audit' => 1,
  209. 'is_bro_room' => $config['broadcast_room_type'] == 1 ? 0 : 1,
  210. 'is_bro_goods' => $config['broadcast_goods_type'] == 1 ? 0 : 1,
  211. 'mer_password' => $password,
  212. 'is_margin' => $margin['is_margin'] ?? -1,
  213. 'margin' => $margin['margin'] ?? 0,
  214. 'mark' => $margin['margin'] ?? 0,
  215. ];
  216. // 设置失败原因为空
  217. $data['fail_msg'] = '';
  218. // 构建短信数据数组
  219. $smsData = [
  220. 'date' => date('m月d日', strtotime($intention->create_time)),
  221. 'mer' => $intention['mer_name'],
  222. 'phone' => $intention['phone'],
  223. 'pwd' => $password ?? '',
  224. 'site_name' => systemConfig('site_name'),
  225. ];
  226. }
  227. // 如果状态更新为2(可能是审核不通过)
  228. if ($data['status'] == 2) {
  229. // 设置短信数据
  230. $smsData = [
  231. 'phone' => $intention['phone'],
  232. 'date' => date('m月d日', strtotime($intention->create_time)),
  233. 'mer' => $intention['mer_name'],
  234. 'site' => systemConfig('site_name'),
  235. ];
  236. }
  237. // 开启数据库事务处理
  238. Db::transaction(function () use ($config, $intention, $data, $create,$margin,$merData,$smsData) {
  239. // 如果状态更新为1(审核通过)
  240. if ($data['status'] == 1) {
  241. // 如果需要创建商家
  242. if ($create == 1) {
  243. // 创建商家
  244. $merchant = app()->make(MerchantRepository::class)->createMerchant($merData);
  245. // 保存商家证书信息
  246. app()->make(ConfigValueRepository::class)->setFormData(['mer_certificate' => $intention['images']], $merchant->mer_id);
  247. // 设置商家ID
  248. $data['mer_id'] = $merchant->mer_id;
  249. // 添加发送短信任务
  250. Queue::push(SendSmsJob::class, ['tempId' => 'APPLY_MER_SUCCESS', 'id' => $smsData]);
  251. }
  252. } else {
  253. // 添加发送短信任务(审核不通过)
  254. Queue::push(SendSmsJob::class, ['tempId' => 'APPLY_MER_FAIL', 'id' => $smsData]);
  255. }
  256. // 保存商家意向信息
  257. $intention->save($data);
  258. });
  259. }
  260. }