drag-create-api/app/common/model/Customer/Customer.php

154 lines
3.9 KiB
PHP

<?php
namespace app\common\model\Customer;
use app\common\arw\adjfut\src\Validate;
use app\common\logic\Customer\Customer as LogicCustomer;
use app\common\logic\Customer\Customer as CommonLogicCustomer;
use app\BaseModel;
use think\model\concern\SoftDelete;
use app\common\arw\adjfut\src\Excel;
use app\Request;
use app\common\exception\Tool;
use think\facade\Db;
class Customer extends BaseModel
{
use SoftDelete;
// 删除字段
protected $deleteTime = 'customer_delete_time';
// 设置主键名
protected $pk = 'customer_guid';
// 设置废弃字段
protected $disuse = [];
// 设置字段信息
protected $schema = [
'customer_id' => 'int',
'customer_guid' => 'string',
'customer_name' => 'string',
'customer_account' => 'string',
'customer_password' => 'string',
'customer_phone' => 'string',
'customer_email' => 'string',
'customer_sex' => 'string',
'customer_blacklist' => 'int',
'customer_create_time' => 'datetime',
'customer_create_user_guid' => 'string',
'customer_update_time' => 'datetime',
'customer_update_user_guid' => 'string',
'customer_delete_time' => 'datetime',
'customer_delete_user_guid' => 'string',
];
// 设置json类型字段
protected $json = [''];
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 创建时间
protected $createTime = 'customer_create_time';
// 修改时间
protected $updateTime = 'customer_update_time';
// 字典
public static $dictionaryMap = [
'customer_blacklist' => [
self::BLACKLIST_ENABLE => '是',
self::BLACKLIST_DISABLE => '否'
],
];
// 是否黑名单 是
const BLACKLIST_ENABLE = 1;
// 是否黑名单 否
const BLACKLIST_DISABLE = 2;
/**
* 新增前
*/
public static function onBeforeInsert(self $model): void
{
$model->customer_password = CommonLogicCustomer::encryptPassword($model->customer_password);
self::checkRepeatData($model);
$model->completeCreateField();
}
/**
* 更新前
*/
public static function onBeforeUpdate(self $model): void
{
$model->customer_password = CommonLogicCustomer::encryptPassword($model->customer_password);
self::checkRepeatData($model);
$model->completeUpdateField();
}
/**
* 删除前
*/
public static function onBeforeDelete(self $model): void
{
$model->completeDeleteField();
}
/**
* 数据查重
*
* @param self $model
* @return void
* @date 2022-03-11
* @example
* @author admin
* @since 1.0.0
*/
private static function checkRepeatData(self $model)
{
Validate::unique(
self::class,
$model->customer_guid,
$model->getData(),
['customer_account' => '账号',],
['customer_account' => "账号已存在!"]
);
}
/**
* 获取客户菜单
*
* @return array
* @date 2023-06-25
* @author xjh
* @since 1.0.0
*/
public static function getCustomerMenu(): array
{
return LogicCustomer::getCustomerMenu();
}
/**
* 查询范围-非客户黑名单的客户
*
* @param $query
* @return void
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function scopeBlacklist($query): void
{
$query->where('customer.customer_blacklist', self::BLACKLIST_DISABLE);
}
/**
* 获取客户选项展示文字
*
* @param $value
* @param $data
* @return string
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function getCustomerShowTextAttr($value, $data): string
{
return "客户名称:【{$data['customer_name']}】 账号:【{$data['customer_account']}";
}
}