MerchantIntentionRepository.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\common\repositories\system\merchant;
  12. use app\common\repositories\BaseRepository;
  13. use crmeb\jobs\SendSmsJob;
  14. use FormBuilder\Factory\Elm;
  15. use app\common\dao\system\merchant\MerchantIntentionDao;
  16. use think\exception\ValidateException;
  17. use think\facade\Db;
  18. use think\facade\Queue;
  19. use think\facade\Route;
  20. /**
  21. * @mixin MerchantIntentionDao
  22. */
  23. class MerchantIntentionRepository extends BaseRepository
  24. {
  25. public function __construct(MerchantIntentionDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. public function getList(array $where, $page, $limit)
  30. {
  31. $query = $this->dao->search($where);
  32. $count = $query->count();
  33. $list = $query->page($page, $limit)->order('create_time DESC , status ASC')->with(['merchantCategory'])->select();
  34. return compact('count', 'list');
  35. }
  36. public function detail($id, ?int $uid)
  37. {
  38. $where = ['mer_intention_id' => $id];
  39. if (!is_null($uid)) {
  40. $where['uid'] = $uid;
  41. }
  42. return $this->dao->search($where)->find();
  43. }
  44. public function updateIntention($id, array $data)
  45. {
  46. if ($this->dao->existsWhere(['mer_intention_id' => $id, 'status' => '1']))
  47. throw new ValidateException('当前状态不能修改');
  48. $data['images'] = implode(',', (array)$data['images']);
  49. $data['status'] = 0;
  50. $data['fail_msg'] = '';
  51. return $this->dao->update($id, $data);
  52. }
  53. public function markForm($id)
  54. {
  55. $data = $this->dao->get($id);
  56. $form = Elm::createForm(Route::buildUrl('systemMerchantIntentionMark', ['id' => $id])->build());
  57. $form->setRule([
  58. Elm::textarea('mark', '备注', $data['remark'])->required(),
  59. ]);
  60. return $form->setTitle('修改备注');
  61. }
  62. public function statusForm($id)
  63. {
  64. $form = Elm::createForm(Route::buildUrl('systemMerchantIntentionStatus', ['id' => $id])->build());
  65. $form->setRule([
  66. Elm::select('status', '审核状态', 1)->options([
  67. ['value' => 1, 'label' => '同意'],
  68. ['value' => 2, 'label' => '拒绝'],
  69. ])->control([
  70. [
  71. 'value' => 1,
  72. 'rule' => [
  73. Elm::radio('create_mer', '自动创建商户', 1)->options([
  74. ['value' => 1, 'label' => '创建'],
  75. ['value' => 2, 'label' => '不创建'],
  76. ])
  77. ]
  78. ],
  79. [
  80. 'value' => 2,
  81. 'rule' => [
  82. Elm::textarea('fail_msg', '失败原因', '信息填写有误')
  83. ]
  84. ]
  85. ]),
  86. ]);
  87. return $form->setTitle('修改审核状态');
  88. }
  89. public function updateStatus($id, $data)
  90. {
  91. $create = $data['create_mer'] == 1;
  92. unset($data['create_mer']);
  93. $intention = $this->search(['mer_intention_id' => $id])->find();
  94. if (!$intention)
  95. throw new ValidateException('信息不存在');
  96. if ($intention->status)
  97. throw new ValidateException('状态有误,修改失败');
  98. $config = systemConfig(['broadcast_room_type', 'broadcast_goods_type']);
  99. Db::transaction(function () use ($config, $intention, $data, $create) {
  100. if ($data['status'] == 1) {
  101. $data['fail_mag'] = '';
  102. if ($create) {
  103. $password = substr(md5(time() . $intention['phone']), 0, 8);
  104. $merchant = app()->make(MerchantRepository::class)->createMerchant([
  105. 'mer_name' => $intention['mer_name'],
  106. 'mer_phone' => $intention['phone'],
  107. 'mer_account' => $intention['phone'],
  108. 'category_id' => $intention['merchant_category_id'],
  109. 'real_name' => $intention['name'],
  110. 'status' => 1,
  111. 'is_audit' => 1,
  112. 'is_bro_room' => $config['broadcast_room_type'] == 1 ? 0 : 1,
  113. 'is_bro_goods' => $config['broadcast_goods_type'] == 1 ? 0 : 1,
  114. 'mer_password' => $password
  115. ]);
  116. $data['mer_id'] = $merchant->mer_id;
  117. Queue::push(SendSmsJob::class, ['tempId' => 'APPLY_MER_SUCCESS', 'id' => [
  118. 'mer' => $intention['mer_name'], 'date' => $intention->create_time, 'pwd' => $password, 'phone' => $intention['phone']
  119. ]]);
  120. }
  121. } else {
  122. Queue::push(SendSmsJob::class, ['tempId' => 'APPLY_MER_FAIL', 'id' => [
  123. 'mer' => $intention['mer_name'], 'date' => $intention->create_time, 'phone' => $intention['phone']
  124. ]]);
  125. }
  126. $intention->save($data);
  127. });
  128. }
  129. }