Record.php 6.92 KB
<?php

namespace app\admin\controller\antis\record;
use addons\antis\model\Field;
use addons\antis\model\File;
use addons\antis\model\Remind;
use addons\antis\model\Staff;
use addons\antis\model\Customer;
use app\admin\controller\antis\Base;
use addons\antis\model\Contacts;
use addons\antis\model\Contract;
use addons\antis\model\Leads;
use addons\antis\model\Business;
use addons\antis\model\Comment;
use addons\antis\model\Record as RecordModel;
use app\admin\model\AuthGroup;
use think\DB;
use think\Exception;

/**
 * 客户跟进记录
 */
class Record extends Base {
    public function _initialize() {
        parent::_initialize();
        $this->model = new RecordModel();
    }

    /**
     * 客户跟进记录
     */
    public function index() {

        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage,则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();


        //$staff_ids = [14,37,38,39,40,41,42,106];
        //$wheres['create_staff_id'] = ['in',$staff_ids];

        $list = $this->model
            ->with('staff')
            ->where($where)
            //->where($wheres)
            //->whereTime('createtime','last month')
            ->order($sort, $order)
            ->paginate($limit);

        foreach ($list as &$v){

            if($v['relation_type'] == RecordModel::CUSTOMER_TYPE) {
                $v['relation_name'] = Customer::where(['id' => $v['relation_id']])->value('name');
            }elseif($v['relation_type'] == RecordModel::CONTACTS_TYPE) {
                $v['relation_name'] = Contacts::where(['id' => $v['relation_id']])->value('name');
            }elseif($v['relation_type'] == RecordModel::CONTRACT_TYPE) {
                $v['relation_name'] = Contract::where(['id' => $v['relation_id']])->value('name');
            }elseif($v['relation_type'] == RecordModel::LEADS_TYPE) {
                $v['relation_name'] = Leads::where(['id' => $v['relation_id']])->value('name');
            } elseif($v['relation_type'] == RecordModel::BUSINESS_TYPE) {
                $v['relation_name'] = Business::where(['id' => $v['relation_id']])->value('name');
            }
        }


        $result = ['total' => $list->total(), 'rows' => $list->items()];

        return json($result);
    }



    public function export(){

        $param = $this->request->param();

        $where = [];
        if(!empty($param['time'])){
            $time = explode(' - ',$param['time']);
            $where['follow_time'] = ['between',[$time[0],$time[1]]];
        }

        $list = (new Staff())->where('status', '1')->where('deletetime','null')->select();

        $data = [];
        foreach ($list as $item){
            $group_ids = $item['group_ids'];
            $group_name =implode(',',AuthGroup::where("find_in_set(id, '$group_ids')")->column('name'));

            $count = $this->model->where('create_staff_id',$item['id'])->where($where)->count();

            $data[]=[
                'staff'=>$item['name'],  //姓名
                'group_name'=>$group_name,
                'post'=>$item['post'],
                'count'=>$count
            ];
        }

        $title = [
            '姓名',
            '部门',
            '岗位',
            '跟进数量'
        ];


        $file = export_excel($title, $data, '跟进统计');
        if ($file['filePath']) {
            $this->success('导出成功', '', $file);
        }
        $this->error('导出失败');

    }

    /**
     * 添加跟进
     */
    public function add($ids = null) {
        $relation_type=input('relation_type',1);
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            // 表单验证
            if (($result = $this->qingdongValidate($params, 'Record', 'create')) !== true) {
                $this->error($result);
            }
            if(!empty($params['files'])){
                $params['files']=File::getId($params['files']);
            }
            Db::startTrans();
            try {
                $result = $this->model::createRecord($params);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result) {
                $this->success('创建跟进记录成功');
            }
            $this->error('创建失败');
        }

        $follow= Field::getField('客户状态');
        $customer=Customer::getList();
        $customers = [];
        foreach($customer as $k=>$v){
            $customers[$v['id']] = $v['name'];
        }
        if($relation_type == 2){
            $contacts=Contacts::where(['id'=>$ids])->column('id,name');
            $this->assign('contacts',$contacts);
        }

        $remind=Remind::where(['type'=>Remind::RECORD_TYPE])->find();
        $staff_ids=$remind['staff_ids']??'';
        $this->assign('staff_ids', $staff_ids);
        $this->assign('ids', $ids);
        $this->assign('relation_type', $relation_type);
        $this->assign('customer', $customers);
        $this->assign('follow', $follow);
        $this->assign('staff', Staff::where([])->column('name','id'));
        return $this->view->fetch();
    }
    /**
     * 跟进详情
     */
    public function detail($ids = null) {
        $row = $this->model->with(['staff','file'])->where([
            'id' => $ids,
        ])->find();
        if (empty($row)) {
            $this->error(__('No Results were found'));
        }
        $row = $row->toArray();
        if($row['staff_id']){
            $createname = Staff::where(['id'=>$row['staff_id']])->value('name');
        }else{
            $createname = isset($row['staff']['name']) ?  $row['staff']['name'] : '';
        }

        $row['createname'] = $createname;
        $this->assign('row', $row);
        $this->assignconfig("idinfo", ['id' => $ids]);
        return $this->view->fetch();
    }
    /**
     * 评论
     */
    public function comment() {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $record_id = input('record_id','');
            $wheres['relation_id'] = $record_id;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list   = Comment::where($where)->where($wheres)->with(['staff','record'])->order($sort, $order)->paginate($limit);
            $row    = $list->items();
            $result = array("total" => $list->total(), "rows" => $row);

            return json($result);
        }

        return $this->view->fetch();
    }

}