admin-api/app/common/repositories/system/merchant/MerchantIntentionRepository...

163 lines
6.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\repositories\system\merchant;
use app\common\repositories\BaseRepository;
use app\common\repositories\user\UserRepository;
use crmeb\jobs\SendSmsJob;
use crmeb\services\SmsService;
use FormBuilder\Factory\Elm;
use app\common\dao\system\merchant\MerchantIntentionDao;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Queue;
use think\facade\Route;
/**
* @mixin MerchantIntentionDao
*/
class MerchantIntentionRepository extends BaseRepository
{
public function __construct(MerchantIntentionDao $dao)
{
$this->dao = $dao;
}
public function getList(array $where, $page, $limit)
{
$query = $this->dao->search($where);
$count = $query->count();
$list = $query->page($page, $limit)->order('create_time DESC , status ASC')->with(['merchantCategory', 'merchantType'])->select();
return compact('count', 'list');
}
public function detail($id, ?int $uid)
{
$where = ['mer_intention_id' => $id];
if (!is_null($uid)) {
$where['uid'] = $uid;
}
return $this->dao->search($where)->find();
}
public function updateIntention($id, array $data)
{
if ($this->dao->existsWhere(['mer_intention_id' => $id, 'status' => '1']))
throw new ValidateException('当前状态不能修改');
$data['images'] = implode(',', (array)$data['images']);
$data['status'] = 0;
$data['fail_msg'] = '';
return $this->dao->update($id, $data);
}
public function markForm($id)
{
$data = $this->dao->get($id);
$form = Elm::createForm(Route::buildUrl('systemMerchantIntentionMark', ['id' => $id])->build());
$form->setRule([
Elm::textarea('mark', '备注', $data['mark'])->required(),
]);
return $form->setTitle('修改备注');
}
public function statusForm($id)
{
$form = Elm::createForm(Route::buildUrl('systemMerchantIntentionStatus', ['id' => $id])->build());
$form->setRule([
Elm::select('status', '审核状态', 1)->options([
['value' => 1, 'label' => '同意'],
['value' => 2, 'label' => '拒绝'],
])->control([
// [
// 'value' => 1,
// 'rule' => [
// Elm::radio('create_mer', '自动创建商户', 1)->options([
// ['value' => 1, 'label' => '创建'],
// ['value' => 2, 'label' => '不创建'],
// ])
// ]
// ],
[
'value' => 2,
'rule' => [
Elm::textarea('fail_msg', '失败原因', '信息填写有误')
]
]
]),
]);
return $form->setTitle('修改审核状态');
}
public function updateStatus($id, $data)
{
$create = 1;//$data['create_mer'] == 1;// TODO2023-12-27修改 审核通过后,直接自动创建商户信息
unset($data['create_mer']);
$intention = $this->search(['mer_intention_id' => $id])->find();
if (!$intention) throw new ValidateException('信息不存在');
if ($intention->status) throw new ValidateException('状态有误,修改失败');
$config = systemConfig(['broadcast_room_type', 'broadcast_goods_type']);
$margin = app()->make(MerchantTypeRepository::class)->get($intention['mer_type_id']);
$data['is_margin'] = $margin['is_margin'] ?? -1;
$data['margin'] = $margin['margin'] ?? 0;
$merData = [];
$smsData = [];
if ($create == 1) {
$password = substr($intention['phone'], -6);
$uid = $intention['uid'] ?? 0;
$user = app()->make(UserRepository::class)->get($uid);
$merData = [
'mer_name' => $intention['mer_name'],
'mer_phone' => $intention['phone'],
'mer_account' => $intention['phone'],
'category_id' => $intention['merchant_category_id'],
'type_id' => $intention['mer_type_id'],
'real_name' => $intention['name'],
'status' => 1,
'is_audit' => 1,
'is_bro_room' => $config['broadcast_room_type'] == 1 ? 0 : 1,
'is_bro_goods' => $config['broadcast_goods_type'] == 1 ? 0 : 1,
'mer_password' => $password,
'is_margin' => $margin['is_margin'] ?? -1,
'margin' => $margin['margin'] ?? 0,
'uid' => $uid,
'spread_uid' => $user->spread_uid ?? 0
];
$data['fail_msg'] = '';
$smsData = [
'date' => date('m月d日', strtotime($intention->create_time)),
'mer' => $intention['mer_name'],
'phone' => $intention['phone'],
'pwd' => $password ?? '',
'site_name' => systemConfig('site_name'),
];
}
if ($data['status'] == 2) {
$smsData = [
'phone' => $intention['phone'],
'date' => date('m月d日', strtotime($intention->create_time)),
'mer' => $intention['mer_name'],
'site' => systemConfig('site_name'),
];
}
Db::transaction(function () use ($config, $intention, $data, $create,$margin,$merData,$smsData) {
if ($data['status'] == 1) {
if ($create == 1) {
$merchant = app()->make(MerchantRepository::class)->createMerchant($merData);
$data['mer_id'] = $merchant->mer_id;
Queue::push(SendSmsJob::class, ['tempId' => 'APPLY_MER_SUCCESS', 'id' => $smsData]);
}
} else {
Queue::push(SendSmsJob::class, ['tempId' => 'APPLY_MER_FAIL', 'id' => $smsData]);
}
$intention->save($data);
});
}
}