123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- namespace addons\qingdongams\model;
- use app\admin\model\AuthGroup;
- use think\Model;
- /**
- *通知模板
- */
- class NoticeTemplate extends Model
- {
- // 表名,不含前缀
- protected $name = 'qingdongams_notice_template';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = false;
- public function getDataAttr($value)
- {
- return json_decode($value, true);
- }
- public function getEnterpriseDataAttr($value)
- {
- return json_decode($value, true);
- }
- public static function getTypeForData($type)
- {
- $data = self::where(['type' => $type])->value('data');
- return json_decode($data, true);
- }
- public static function getTypeForEnterpriseData($type)
- {
- $data = self::where(['type' => $type])->value('enterprise_data');
- return json_decode($data, true);
- }
- public function getVariable($type)
- {
- switch ($type) {
- case Message::EXAMINE_TYPE:
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{examine_type}}' => '审批类型',
- '{{customer_name}}' => '关联客户',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::RECORD_TYPE:
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '关联客户',
- '{{customer_follow}}' => '跟进状态',
- '{{content}}' => '跟进内容',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::DAILY_TYPE:
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{daily_type}}' => '报告类型',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::SEAS_TYPE:
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '客户名称',
- '{{day}}' => '未跟进天数',
- '{{date}}' => '时间',
- ];
- break;
- case Message::SIGN_TYPE://签到
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '客户名称',
- '{{address}}' => '签到地点',
- '{{date}}' => '时间',
- ];
- break;
- case Message::EXAMINE_ADOPT_TYPE://
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '关联客户',
- '{{examine_type}}' => '审批类型',
- '{{check_name}}' => '审核人',
- '{{examine_desc}}' => '审核备注',
- '{{submit_time}}' => '提交时间',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::EXAMINE_REFUSE_TYPE://
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '关联客户',
- '{{examine_type}}' => '审批类型',
- '{{check_name}}' => '审核人',
- '{{examine_desc}}' => '审核备注',
- '{{submit_time}}' => '提交时间',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::CONTRACT_EXPIRE_TYPE://
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '关联客户',
- '{{money}}' => '合同到期金额',
- '{{submit_time}}' => '合同创建时间',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::PLAN_EXPIRE_TYPE://
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{customer_name}}' => '关联客户',
- '{{plan_money}}' => '计划回款金额',
- '{{plan_time}}' => '计划回款时间',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::COMMENT_TYPE://
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{content}}' => '评论内容',
- '{{date}}' => '操作时间',
- ];
- break;
- case Message::EVENT_TYPE://
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{event_title}}' => '日程标题',
- '{{event_start_time}}' => '日程开始时间',
- '{{event_end_time}}' => '日程结束时间',
- '{{date}}' => '操作时间',
- ];
- break;
- default:
- $data = [
- '{{staff_name}}' => '员工姓名',
- '{{staff_department}}' => '员工角色组',
- '{{date}}' => '操作时间',
- ];
- }
- return $data;
- }
- //替换模板内容
- public static function replaceTemplateContent($data, $relation_type, $relation_id)
- {
- $staff = Staff::info();
- $staff_name = $staff->name;
- $staff_department = AuthGroup::where(['id' => ['in', $staff->group_ids]])->column('name');
- $staff_department=implode(',',$staff_department);
- $date = date('Y-m-d H:i:s');
- $data = json_encode($data, JSON_UNESCAPED_UNICODE);
- $search = ['{{staff_name}}', '{{staff_department}}', '{{date}}'];
- $replace = [$staff_name, $staff_department, $date];
- $data = str_replace($search, $replace, $data);
- switch ($relation_type) {
- case Message::EXAMINE_TYPE:
- $examine_type = ExamineRecord::where(['id' => $relation_id])->value('relation_type');
- if($examine_type){
- if ($examine_type == ExamineRecord::CONSUME_TYPE) {
- $examine_type = '费用审批';
- $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
- $customer_name = $consume['customer']['name'] ?? '';
- } elseif ($examine_type == ExamineRecord::CONTRACT_TYPE) {
- $examine_type = '合同审批';
- $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
- $customer_name = $contract['customer']['name'] ?? '';
- } elseif ($examine_type == ExamineRecord::RECEIVABLES_TYPE) {
- $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
- $examine_type = '回款审批';
- $customer_name = $consume['customer']['name'] ?? '';
- } elseif ($examine_type == ExamineRecord::ACHIEVEMENT_TYPE) {
- $examine_type = '业绩目标';
- $customer_name = '';
- } else {
- $examine_type = '';
- $customer_name = '';
- }
- $search = ['{{examine_type}}', '{{customer_name}}'];
- $replace = [$examine_type, $customer_name];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::RECORD_TYPE:
- $record = Record::where(['id' => $relation_id])->find();
- if($record){
- if ($record['relation_type'] == 1) {//客户
- $customer = Customer::where(['id'=>$record['relation_id']])->value('name');
- $customer_name = $customer ?? '';
- } elseif ($record['relation_type'] == 2) {//联系人
- $customer = Contacts::where(['id' => $record['relation_id']])->with(['customer'])->find();
- $customer_name = $customer['customer']['name'] ?? '';
- } elseif ($record['relation_type'] == 3) {//合同
- $customer = Contract::where(['id' => $record['relation_id']])->with(['customer'])->find();
- $customer_name = $customer['customer']['name'] ?? '';
- } elseif ($record['relation_type'] == 4) {//线索
- $leads = Leads::where(['id' => $record['relation_id']])->field('name,follow')->find();
- $customer_name = $leads['name'] ?? '';
- }elseif ($record['relation_type'] == 5) {//商机
- $leads = Business::where(['id' => $record['relation_id']])->field('name')->find();
- $customer_name = $leads['name'] ?? '';
- }
- $customer_follow = $record['follow'] ?? '';
- $content = $record['content'] ?? '';
- $search = ['{{customer_name}}', '{{customer_follow}}', '{{content}}'];
- $replace = [$customer_name, $customer_follow, $content];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::DAILY_TYPE:
- $daily = Daily::get($relation_id);
- if($daily){
- $search = ['{{daily_type}}'];
- $replace = [$daily['type']];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::SEAS_TYPE:
- $customer = Customer::where(['id' => $relation_id])->field('id,name,receivetime')->find();
- if($customer){
- $customer_name = $customer['name'] ?? '';
- $day = intval((time() - $customer['receivetime']) / 86400);
- $search = ['{{customer_name}}', '{{day}}'];
- $replace = [$customer_name, $day];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::SIGN_TYPE://签到
- $sign = StaffSignIn::where(['id' => $relation_id])->with(['customer'])->find();
- if($sign){
- $customer_name = $sign['customer']['name'] ?? '';
- $address = $sign['location'] ?? '';
- $search = ['{{customer_name}}', '{{customer_follow}}', '{{address}}'];
- $replace = [$customer_name,$sign['relation_process'], $address];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::EXAMINE_ADOPT_TYPE://
- case Message::EXAMINE_REFUSE_TYPE://
- $examine = ExamineRecord::where(['id' => $relation_id])->with(['checkStaff'])->find();
- if($examine){
- $examine_type = $examine['relation_type'];
- if ($examine_type == ExamineRecord::CONSUME_TYPE) {
- $examine_type = '费用审批';
- $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
- $customer_name = $consume['customer']['name'] ?? '';
- } elseif ($examine_type == ExamineRecord::CONTRACT_TYPE) {
- $examine_type = '合同审批';
- $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
- $customer_name = $contract['customer']['name'] ?? '';
- } elseif ($examine_type == ExamineRecord::RECEIVABLES_TYPE) {
- $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
- $examine_type = '回款审批';
- $customer_name = $consume['customer']['name'] ?? '';
- } elseif ($examine_type == ExamineRecord::ACHIEVEMENT_TYPE) {
- $examine_type = '业绩目标';
- $customer_name = '';
- } else {
- $examine_type = '';
- $customer_name = '';
- }
- $check_name = $examine['check_staff']['name'] ?? '';
- $examine_desc = $examine['content'];
- $submit_time =$examine['check_time'];
- $search = ['{{examine_type}}', '{{customer_name}}', '{{check_name}}', '{{examine_desc}}', '{{submit_time}}'];
- $replace = [$examine_type, $customer_name, $check_name, $examine_desc, $submit_time];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::CONTRACT_EXPIRE_TYPE://
- $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
- if($contract){
- $customer_name = $contract['customer']['name'] ?? '';
- $money = $contract['money'];
- $submit_time = $contract['order_date'];
- $search = ['{{customer_name}}', '{{money}}', '{{submit_time}}'];
- $replace = [$customer_name, $money, $submit_time];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::PLAN_EXPIRE_TYPE://
- $plan=ReceivablesPlan::where(['id'=>$relation_id])->with(['customer'])->find();
- if($plan){
- $customer_name=$plan['customer']['name']??'';
- $plan_money=$plan['money'];
- $plan_time=$plan['return_date'];
- $search = ['{{customer_name}}', '{{plan_money}}', '{{plan_time}}'];
- $replace = [$customer_name, $plan_money, $plan_time];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::COMMENT_TYPE://
- $comment=Comment::where(['id'=>$relation_id])->order('id desc')->find();
- if($comment){
- $content=$comment['content'];
- $search = ['{{content}}'];
- $replace = [$content];
- $data = str_replace($search, $replace, $data);
- }
- break;
- case Message::EVENT_TYPE://
- $event=Event::where(['id'=>$relation_id])->find();
- if($event){
- $search = ['{{event_title}}','{{event_start_time}}','{{event_end_time}}'];
- $replace = [$event['title'],$event['start_time'],$event['end_time']];
- $data = str_replace($search, $replace, $data);
- }
- break;
- }
- return json_decode($data,true);
- }
- }
|