NoticeTemplate.php 15.9 KB
<?php

namespace addons\antis\model;

use app\admin\model\AuthGroup;
use think\Model;

/**
 *通知模板
 */
class NoticeTemplate extends Model
{

    // 表名,不含前缀
    protected $name = 'qingdong_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,$to_staff_id='')
    {
        if($to_staff_id){
            $staff = Staff::get($to_staff_id);
            $staff_name = $staff['name'];
            $group_ids =$staff['group_ids'];
        }else{
            $staff = Staff::info();
            $staff_name = $staff->name;
            $group_ids = $staff->group_ids;
        }

        $staff_department = AuthGroup::where(['id' => ['in', $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_name = $customer['name'] ?? '';
                    } 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}}', '{{address}}'];
                    $replace = [$customer_name, $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])->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);
    }
}