You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
411 lines
14 KiB
411 lines
14 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\admin\customer_resources;
|
|
|
|
use app\model\campus_person_role\CampusPersonRole;
|
|
use app\model\course_schedule\CourseSchedule;
|
|
use app\model\customer_resource_changes\CustomerResourceChanges;
|
|
use app\model\customer_resources\CustomerResources;
|
|
use app\model\order_table\OrderTable;
|
|
use app\model\personnel\Personnel;
|
|
use app\model\campus\Campus;
|
|
|
|
use app\model\resource_sharing\ResourceSharing;
|
|
use app\model\six_speed\SixSpeed;
|
|
use app\model\six_speed_modification_log\SixSpeedModificationLog;
|
|
use app\service\admin\member\MemberLabelService;
|
|
use core\base\BaseAdminService;
|
|
|
|
|
|
/**
|
|
* 客户资源服务层
|
|
* Class CustomerResourcesService
|
|
* @package app\service\admin\customer_resources
|
|
*/
|
|
class CustomerResourcesService extends BaseAdminService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new CustomerResources();
|
|
}
|
|
|
|
/**
|
|
* 获取客户资源列表
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getPage(array $data = [])
|
|
{
|
|
// dump($data);die;
|
|
$field = 'a.*,b.id as shared_id';
|
|
$order = 'a.id desc';
|
|
$where = [];
|
|
if ($data['phone_number']) {
|
|
$where[] = ['a.phone_number', '=', $data['phone_number']];
|
|
}
|
|
|
|
if ($data['name']) {
|
|
$where[] = ['a.name', '=', $data['name']];
|
|
}
|
|
|
|
if ($data['age']) {
|
|
$where[] = ['a.age', '=', $data['age']];
|
|
}
|
|
|
|
if ($data['gender']) {
|
|
$where[] = ['a.gender', '=', $data['gender']];
|
|
}
|
|
|
|
if ($data['type'] == 'yjfp') {
|
|
$where[] = ['b.shared_by', '=', 0];
|
|
}
|
|
|
|
if ($data['member_label']) {
|
|
$where[] = ['a.member_label', 'like', "%" . $data['member_label'] . "%"];
|
|
}
|
|
|
|
|
|
$search_model = $this->model
|
|
->alias("a")
|
|
->join(['school_resource_sharing' => 'b'], 'a.id = b.resource_id', 'left')
|
|
->where($where)
|
|
->with(['personnel'])->field($field)->order($order);
|
|
|
|
|
|
if (isset($data['created_at'][0]) && isset($data['created_at'][1])) {
|
|
$search_model->whereBetweenTime('created_at', $data['created_at'][0] . "00:00:00", $data['created_at'][1] . "23:59:59");
|
|
}
|
|
|
|
if (isset($data['updated_at'][0]) && isset($data['updated_at'][1])) {
|
|
$search_model->whereBetweenTime('updated_at', $data['updated_at'][0] . "00:00:00", $data['updated_at'][1] . "23:59:59");
|
|
}
|
|
|
|
|
|
return $this->pageQuery($search_model, function ($item, $key) {
|
|
$item = $this->makeUp($item);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取客户资源信息
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function getInfo(int $id)
|
|
{
|
|
$field = 'id,create_year_month,create_date,source,source_channel,member_label,consultant,name,age,gender,phone_number,demand,purchasing_power,cognitive_idea,optional_class_time,distance,decision_maker,initial_intent,campus,created_at,updated_at,deleted_at,status';
|
|
|
|
$info = $this->model->field($field)->where([['id', "=", $id]])->with(['personnel'])->findOrEmpty()->toArray();
|
|
|
|
$sixSpeed = new SixSpeed();
|
|
$data = $sixSpeed->where(['resource_id' => $id])->field("*,distance as distance_tow")->findOrEmpty()->toArray();
|
|
$info = $info + $data;
|
|
|
|
return $this->makeUp($info);
|
|
}
|
|
|
|
|
|
public function makeUp($data)
|
|
{
|
|
//会员标签
|
|
if (!empty($data['member_label'])) {
|
|
$data['member_label_array'] = (new MemberLabelService())->getMemberLabelListByLabelIds($data['member_label']);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 添加客户资源
|
|
* @param array $data
|
|
* @return mixed
|
|
*/
|
|
public function add(array $data)
|
|
{
|
|
$resourceSharing = new ResourceSharing();
|
|
$personnel = new Personnel();
|
|
$data['consultant'] = $personnel->where(['sys_user_id' => $this->uid])->value("id");
|
|
|
|
if (!$data['consultant']) {
|
|
return fail("操作失败");
|
|
}
|
|
|
|
$sixSpeed = new SixSpeed();
|
|
|
|
// $data['member_label'] = json_encode($data['member_label']);
|
|
|
|
$res = $this->model->create($data);
|
|
|
|
|
|
$role_id = $personnel->alias("a")->join(['school_campus_person_role' => 'b'], 'a.id = b.person_id', 'left')
|
|
->where(['a.id' => $data['consultant']])->value('b.role_id');
|
|
$resourceSharing->insert([
|
|
'resource_id' => $res->id,
|
|
'user_id' => $data['consultant'],
|
|
'role_id' => $role_id
|
|
]);
|
|
|
|
if ($data['purchase_power']) {
|
|
|
|
$six_id = $sixSpeed->where(['resource_id' => $res->id])->value("id");
|
|
$data['staff_id'] = $data['consultant'];
|
|
|
|
$field = [
|
|
'purchase_power' => $data['purchase_power'],
|
|
'concept_awareness' => $data['concept_awareness'],
|
|
'preferred_class_time' => $data['preferred_class_time'],
|
|
'distance' => $data['distance_tow'],
|
|
'communication' => $data['communication'],
|
|
'promised_visit_time' => $data['promised_visit_time'],
|
|
'actual_visit_time' => $data['actual_visit_time'],
|
|
'call_intent' => $data['call_intent'],
|
|
'first_visit_status' => $data['first_visit_status'],
|
|
'second_visit_status' => $data['second_visit_status'],
|
|
'is_closed' => $data['is_closed'],
|
|
'staff_id' => $data['staff_id'],
|
|
'resource_id' => $res->id
|
|
];
|
|
if ($six_id) {
|
|
$sixSpeed->where(['resource_id' => $res->id])->update($field);
|
|
} else {
|
|
$sixSpeed->insert($field);
|
|
}
|
|
}
|
|
return success("操作成功");
|
|
|
|
}
|
|
|
|
/**
|
|
* 客户资源编辑
|
|
* @param int $id
|
|
* @param array $data
|
|
*/
|
|
public function edit(int $id, array $data)
|
|
{
|
|
$personnel = new Personnel();
|
|
// $data['consultant'] = $personnel->where(['sys_user_id' => $this->uid])->value("id");
|
|
// if(!$data['consultant']){
|
|
// return fail("操作失败");
|
|
// }
|
|
|
|
// $data['consultant'] = 1;
|
|
$res = $this->model->where([['id', '=', $id]])->findOrEmpty()->toArray();
|
|
|
|
|
|
$this->model->where([['id', '=', $id]])->update([
|
|
'source' => $data['source'],
|
|
'source_channel' => $data['source_channel'],
|
|
'member_label' => $data['member_label'],
|
|
'name' => $data['name'],
|
|
'age' => $data['age'],
|
|
'gender' => $data['gender'],
|
|
'phone_number' => $data['phone_number'],
|
|
'demand' => $data['demand'],
|
|
'purchasing_power' => $data['purchasing_power'],
|
|
'cognitive_idea' => $data['cognitive_idea'],
|
|
'optional_class_time' => $data['optional_class_time'],
|
|
'distance' => $data['distance'],
|
|
'decision_maker' => $data['decision_maker'],
|
|
'initial_intent' => $data['initial_intent'],
|
|
'campus' => $data['campus'],
|
|
'status' => $data['status'],
|
|
'create_year_month' => $data['create_year_month'],
|
|
'create_date' => $data['create_date']
|
|
]);
|
|
|
|
$resources_save = getModifiedFields($res, $data);
|
|
|
|
$customerResourceChanges = new CustomerResourceChanges();
|
|
if ($resources_save['is_save']) {
|
|
$customerResourceChanges->insert([
|
|
'customer_resource_id' => $id,
|
|
'operator_id' => $res['consultant'],
|
|
'campus_id' => $data['campus'],
|
|
'modified_fields' => $resources_save['modified_fields'],
|
|
'old_values' => $resources_save['old_values'],
|
|
'new_values' => $resources_save['new_values']
|
|
]);
|
|
}
|
|
|
|
|
|
$sixSpeed = new SixSpeed();
|
|
if ($data['purchase_power']) {
|
|
$sixSpeedModificationLog = new SixSpeedModificationLog();
|
|
$six_id = $sixSpeed->where(['resource_id' => $id])->value("id");
|
|
$data['staff_id'] = $res['consultant'];
|
|
|
|
|
|
$field = [
|
|
'purchase_power' => $data['purchase_power'],
|
|
'concept_awareness' => $data['concept_awareness'],
|
|
'preferred_class_time' => $data['preferred_class_time'],
|
|
'distance' => $data['distance_tow'],
|
|
'communication' => $data['communication'],
|
|
'promised_visit_time' => $data['promised_visit_time'],
|
|
'actual_visit_time' => $data['actual_visit_time'],
|
|
'call_intent' => $data['call_intent'],
|
|
'first_visit_status' => $data['first_visit_status'],
|
|
'second_visit_status' => $data['second_visit_status'],
|
|
'is_closed' => $data['is_closed'],
|
|
'staff_id' => $data['staff_id'],
|
|
'resource_id' => $id
|
|
];
|
|
if ($six_id) {
|
|
$six_log = $sixSpeed->where(['resource_id' => $id])->findOrEmpty()->toArray();
|
|
$six_save = getModifiedFields($six_log, $field);
|
|
|
|
if ($six_save['is_save']) {
|
|
|
|
$sixSpeedModificationLog->insert([
|
|
'customer_resource_id' => $id,
|
|
'operator_id' => $res['consultant'],
|
|
'campus_id' => $data['campus'],
|
|
'modified_field' => $six_save['modified_fields'],
|
|
'old_value' => $six_save['old_values'],
|
|
'new_value' => $six_save['new_values']
|
|
]);
|
|
}
|
|
|
|
$sixSpeed->where(['resource_id' => $id])->update($field);
|
|
} else {
|
|
$sixSpeed->insert($field);
|
|
}
|
|
}
|
|
|
|
return success("操作成功");
|
|
}
|
|
|
|
/**
|
|
* 删除客户资源
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function del(int $id)
|
|
{
|
|
$model = $this->model->where([['id', '=', $id]])->find();
|
|
$res = $model->delete();
|
|
return $res;
|
|
}
|
|
|
|
|
|
public function getPersonnelAll($data)
|
|
{
|
|
$personnelModel = new Personnel();
|
|
$where = [];
|
|
if ($data['role_id']) {
|
|
$where[] = ['b.role_id', '=', $data['role_id']];
|
|
}
|
|
return $personnelModel
|
|
->alias("a")
|
|
->join(['school_campus_person_role' => 'b'], 'a.id = b.person_id', 'left')
|
|
->field("a.*")
|
|
->where($where)->select()->toArray();
|
|
}
|
|
|
|
|
|
public function getCampusAll()
|
|
{
|
|
$campusModel = new Campus();
|
|
return $campusModel->select()->toArray();
|
|
}
|
|
|
|
public function fp_edit($data)
|
|
{
|
|
$resourceSharing = new ResourceSharing();
|
|
$resourceSharing->where(['id' => $data['shared_id']])->update([
|
|
'shared_by' => $data['shared_by'],
|
|
'shared_at' => date("Y-m-d H:i:s")
|
|
]);
|
|
return "分配成功";
|
|
}
|
|
|
|
public function personnelAllByname($name)
|
|
{
|
|
$query = $this->model->whereNotExists(function ($query) {
|
|
$query->name('order_table') // 订单表名
|
|
->whereRaw('school_order_table.resource_id = school_customer_resources.id');
|
|
});
|
|
//如果$name是手机号,则按手机号查询
|
|
if (isPhone($name)) {
|
|
$query = $query->where('phone_number', $name);
|
|
} else {
|
|
$query = $query->where('name', 'like', '%' . $name . '%');
|
|
}
|
|
return $query->select()->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取销售人员
|
|
*/
|
|
public function getSalesPerson($campus_id)
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取市场人员
|
|
*/
|
|
public function getMarketPerson($campus_id)
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取教练人员
|
|
*/
|
|
public function getCoachPerson($campus_id)
|
|
{
|
|
$coachModel = new CampusPersonRole();
|
|
return $coachModel
|
|
->alias("a")
|
|
->join(['school_personnel' => 'b'], 'a.person_id = b.id', 'left')
|
|
->field("a.*,b.name,b.phone")
|
|
->where([
|
|
['a.campus_id', '=', $campus_id],
|
|
['a.role_id', '=', 5]
|
|
])->select()->toArray();
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取课程下的资源和学员
|
|
* @param $course_id
|
|
* @return mixed
|
|
*/
|
|
public function getResourceByCourse($schedule_id)
|
|
{
|
|
$schedule = CourseSchedule::find($schedule_id);
|
|
$course_id = $schedule->course_id;
|
|
$all = (new OrderTable())
|
|
->alias('a')
|
|
->join(['school_customer_resources' => 'b'], 'a.resource_id = b.id')
|
|
->join(['school_student' => 'c'], 'a.resource_id = c.user_id')
|
|
->field('b.name, b.phone_number, b.id, c.name as student_name, c.age, c.id as student_id, a.id as order_id')
|
|
->where([
|
|
['a.course_id', '=', $course_id],
|
|
['a.order_status', '=', 'paid']
|
|
])
|
|
->group('a.id')
|
|
->select()
|
|
->toArray();
|
|
|
|
// 假设 $selectedCode 是一个数组,包含已经加入课程的人员 ID
|
|
$selectedCode = $schedule->participants;
|
|
|
|
// 遍历 $all,为每个元素添加 checked 属性
|
|
foreach ($all as &$item) {
|
|
$item['checked'] = in_array($item['id'], $selectedCode ? $selectedCode : []);
|
|
}
|
|
|
|
return $all;
|
|
}
|
|
}
|
|
|