erp/app/admin/logic/supplier/SupplierLogic.php

371 lines
14 KiB
PHP

<?php
namespace app\admin\logic\supplier;
use app\admin\logic\auth\AdminLogic;
use app\admin\logic\user\UserLogic;
use app\common\service\JgPushService;
use app\common\enum\OrderEnum;
use app\common\model\goods\GoodsLabel;
use app\common\model\merchant\MerchantBank;
use app\common\model\supplier\Supplier;
use app\common\logic\BaseLogic;
use app\common\model\auth\Admin;
use app\common\model\financial\FinancialRecord;
use think\facade\Db;
use support\exception\BusinessException;
use Webman\Config;
/**
* 供应商管理逻辑
* Class SupplierLogic
* @package app\admin\logic\supplier
*/
class SupplierLogic extends BaseLogic
{
/**
* @notes 添加供应商管理
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/27 14:33
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
$sys_labels = '';
if (isset($params['sys_labels'])) {
$sys_labels = explode(',', $params['sys_labels']);
$sys_labels = ',' . implode(',', $sys_labels) . ',';
}
$res = Supplier::create([
'category_id' => $params['category_id'],
'type_id' => $params['type_id'] ?? '',
'mer_name' => $params['mer_name'],
'settle_cycle' => $params['settle_cycle'] ?? 15,
'interest_rate' => $params['interest_rate'] ?? 0,
'sys_labels' => $sys_labels,
'province_id' => $params['province_id'] ?? '',
'city_id' => $params['city_id'] ?? '',
'area_id' => $params['area_id'] ?? '',
'street_id' => $params['street_id'] ?? '',
'village_id' => $params['village_id'] ?? '',
'mer_address' => $params['mer_address'] ?? '',
'mer_avatar' => $params['mer_avatar'] ?? '',
'mark' => $params['mark'] ?? '',
'status' => $params['status'] ?? 0,
'commission_rate' => $params['commission_rate'] ?? '',
'commission_switch' => $params['commission_switch'] ?? '',
'long' => $params['long'] ?? '',
'lat' => $params['lat'] ?? '',
'service_phone' => $params['phone'],
'financial_bank' => $params['financial_bank'] ?? '',
'financial_wechat' => $params['financial_wechat'] ?? '',
'financial_alipay' => $params['financial_alipay'] ?? '',
'financial_type' => $params['financial_type'] ?? '',
'data_json' => json_encode([
'company_address' => $params['address'] ?? '',
'company_nickname' => $params['company_nickname'] ?? '',
'social_credit_code' => $params['social_credit_code'] ?? '',
'company_name' => $params['company_name'] ?? '',
'images' => $params['images'][0] ?? '',
])
]);
Db::name('user_auth_shop')->insert(['pid' => $res->id, 'user_id' => 0, 'shop_uid' => $params['shop_user_id'], 'status' => 0, 'type' => 2, 'create_time' => time(), 'jg_register_id' => $params['jg_register_id']]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 审核商户状态
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/23 16:35
*/
public static function status(array $params): bool
{
$apply_info = Db::name('user_auth_shop')->where('id', $params['apply_id'])->findOrEmpty();
Db::startTrans();
try {
if ($params['status'] == 1) {
$user = [
'real_name' => $params['service_phone'],
'nickname' => '用户' . $params['service_phone'],
'account' => $params['service_phone'],
'mobile' => $params['service_phone'],
'password' => $params['service_phone'],
'sex' => 0,
'is_disable' => 0,
'user_money' => 0
];
$res1 = UserLogic::add($user);
if ($res1 == false) {
Db::rollback();
self::setError(UserLogic::getError());
return false;
}
$admin=[
'name' => '供应商' . $params['service_phone'],
'account' => $params['service_phone'],
'dept_id' => [],
'jobs_id' => [],
'role_id' => [1],
'avatar' => '',
'password' => $params['service_phone'],
'password_confirm' => $params['service_phone'],
'disable' => 0,
'multipoint_login' => 1,
'root' => 0
];
$res2 = AdminLogic::add($admin);
if ($res2 == false) {
Db::rollback();
self::setError(AdminLogic::getError());
return false;
}
$data = [
'user_id' => $res1['id'],
'status' => 1,
'apply_status' => 1,
'pid' => $params['id']
];
Supplier::where('id', $params['id'])->update(['status' => 1, 'uid' => $res1['id']]);
Db::name('user_auth_shop')->where('id', $params['apply_id'])->update($data);
}
if ($params['status'] == 0) {
$data = [
'mark' => $params['mark'] ?? '',
'apply_status' => -1
];
Supplier::where('id', $params['id'])->update([
'status' => 0,
]);
Db::name('user_auth_shop')->where('id', $params['apply_id'])->update($data);
}
if (!empty($apply_info['jg_register_id'])) {
(new JgPushService())->sendMsg($apply_info['jg_register_id'], '您的供应入驻请求审核' . ($params['status'] == 1 ? '通过' : '被拒绝'), '/pages/bindSupplier/index');
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑供应商管理
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/27 14:33
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
$sys_labels = '';
if (isset($params['sys_labels'])) {
$sys_labels = explode(',', $params['sys_labels']);
$sys_labels = ',' . implode(',', $sys_labels) . ',';
}
Supplier::where('id', $params['id'])->update([
'category_id' => $params['category_id'] ?? '',
'type_id' => $params['type_id'] ?? '',
'mer_name' => $params['mer_name'],
'settle_cycle' => $params['settle_cycle'] ?? 15,
'interest_rate' => $params['interest_rate'] ?? 0,
'sys_labels' => $sys_labels,
'province_id' => $params['province_id'] ?? '',
'city_id' => $params['city_id'] ?? '',
'area_id' => $params['area_id'] ?? '',
'street_id' => $params['street_id'] ?? '',
'village_id' => $params['village_id'] ?? '',
'mer_address' => $params['mer_address'] ?? '',
'mer_avatar' => $params['mer_avatar'] ?? '',
'mark' => $params['mark'] ?? '',
'status' => $params['status'] ?? 0,
'commission_rate' => $params['commission_rate'] ?? '',
'commission_switch' => $params['commission_switch'] ?? '',
'long' => $params['long'] ?? '',
'lat' => $params['lat'] ?? '',
'service_phone' => $params['phone'],
'financial_bank' => $params['financial_bank'] ?? '',
'financial_wechat' => $params['financial_wechat'] ?? '',
'financial_alipay' => $params['financial_alipay'] ?? '',
'financial_type' => $params['financial_type'] ?? '',
'data_json' => json_encode([
'company_address' => $params['address'] ?? '',
'company_nickname' => $params['company_nickname'] ?? '',
'social_credit_code' => $params['social_credit_code'] ?? '',
'company_name' => $params['company_name'] ?? '',
'images' => $params['images'][0] ?? '',
])
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 设置余额
*/
public static function set_money($id, $set_money, $type = 1)
{
Db::startTrans();
try {
if ($type == 1) {
Supplier::where('id', $id)->inc('mer_money', $set_money)->update();
$record = [
'financial_record_sn' => time(),
'order_id' => 0,
'number_sn' => getNewOrderId('XTCZ'),
'user_id' => 0,
'financial_type' => OrderEnum::SYSTEM_SET,
'financial_pm' => OrderEnum::INCOME,
'number' => $set_money,
'status' => 1,
'type' => OrderEnum::SUPPLIER,
'mer_id' => $id,
];
FinancialRecord::create($record);
} else {
$money = Supplier::where('id', $id)->value('mer_money');
if ($money < $set_money) {
Db::rollback();
self::setError('余额不足');
return false;
}
Supplier::where('id', $id)->dec('mer_money', $set_money)->update();
$record = [
'financial_record_sn' => time(),
'order_id' => 0,
'number_sn' => getNewOrderId('XTJS'),
'user_id' => 0,
'financial_type' => OrderEnum::SYSTEM_SET,
'financial_pm' => OrderEnum::EXPENDITURE,
'number' => $set_money,
'status' => 1,
'type' => OrderEnum::SUPPLIER,
'mer_id' => $id,
];
FinancialRecord::create($record);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 修改密码
*/
public static function editPassword($params)
{
Db::startTrans();
try {
$admin_id=Db::name('user_auth_shop')->where(['type'=>2,'pid'=>$params['id']])->value('admin_id');
if($admin_id){
$passwordSalt = Config::get('project.unique_identification');
$password=create_password($params['password'], $passwordSalt);
Admin::where('id',$admin_id)->update(['password'=>$password]);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除供应商管理
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/27 14:33
*/
public static function delete(array $params): bool
{
return Supplier::destroy($params['id']);
}
/**
* @notes 获取供应商管理详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/04/27 14:33
*/
public static function detail($params): array
{
$data = Supplier::findOrEmpty($params['id']);
$province = Db::name('geo_province')->field('province_name')->where('province_code', $data['province_id'])->findOrEmpty();
$city = Db::name('geo_city')->field('city_name')->where('city_code', $data['city_id'])->findOrEmpty();
$area = Db::name('geo_area')->field('area_name')->where('area_code', $data['area_id'])->findOrEmpty();
$street = Db::name('geo_street')->field('street_name')->where('street_code', $data['street_id'])->findOrEmpty();
$village = Db::name('geo_village')->field('village_name')->where('village_code', $data['village_id'])->findOrEmpty();
$data['province_name'] = !empty($province) ? $province['province_name'] : '';
$data['city_name'] = !empty($city) ? $city['city_name'] : '';
$data['area_name'] = !empty($area) ? $area['area_name'] : '';
$data['street_name'] = !empty($street) ? $street['street_name'] : '';
$data['village_name'] = !empty($village) ? $village['village_name'] : '';
if (!empty($data['sys_labels'])) {
$goodslabel = GoodsLabel::where('id', 'in', $data['sys_labels'])->column('name');
$data['sys_labels_text'] = implode(',', $goodslabel);
} else {
$data['sys_labels_text'] = '';
}
$data['supplier_banks'] = MerchantBank::where('supplier_id', $params['id'])->select()->each(function ($item) {
$item['is_check_text'] = $item->is_check_text;
$item['is_own_text'] = $item->is_own_text;
})->toArray();
return $data->toArray();
}
/**
* @notes 设置标签
* @param $params
* @return bool
* @date 2024/04/23 11:28
*/
public static function setLabel($params)
{
try {
$value = $params['sys_labels'];
if (!empty($value)) {
if (!is_array($value)) {
$value = ',' . $value . ',';
} else {
$value = ',' . implode(',', $value) . ',';
}
}
Supplier::where('id', $params['id'])->update(['sys_labels' => $value]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}