<?php
/**
 * 活动控制器
 *
 * @author qifan
 * @date 2020-12-09
 */

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\crm\logic\ActivityLogic;
use think\Hook;
use think\Request;

class Activity extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function _initialize()
    {
        $action = [
            'permission' => [],
            'allow' => ['index', 'save', 'read', 'update', 'delete', 'getphrase', 'setphrase', 'getrecordauth', 'excelimport', 'excelexport', 'exceldownload']
        ];
        Hook::listen('check_auth', $action);
        $request = Request::instance();
        $a = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::_initialize();
        }
    }
    
    /**
     * 活动列表
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(ActivityLogic $activityLogic)
    {
        $param = $this->param;
        $param['user_id'] = $this->userInfo['id'];
        
        $data = $activityLogic->index($param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 创建活动【跟进记录】
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function save(ActivityLogic $activityLogic)
    {
        if (!checkPerByAction('crm', 'activity', 'save')) {
            return resultArray(['error' => '你没有创建跟进记录的权限!']);
        }
        if (empty($this->param['activity_type'])) return resultArray(['error' => '缺少模块类型!']);
        if (empty($this->param['activity_type_id'])) return resultArray(['error' => '缺少活动类型ID!']);
        if (empty($this->param['content'])) return resultArray(['error' => '请填写跟进内容!']);
        if (!empty($this->param['next_time']) && strtotime($this->param['next_time']) < time()) {
            return resultArray(['error' => '下次联系时间不能在当前时间之前!']);
        }
        
        $param = $this->param;
        $param['user_id'] = $this->userInfo['id'];
        
        if (!$activityLogic->save($param)) return resultArray(['error' => '操作失败!']);
        return resultArray(['data' => '操作成功!']);
    }
    
    /**
     * 活动详情
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read(ActivityLogic $activityLogic)
    {
        if (!checkPerByAction('crm', 'activity', 'read')) {
            return resultArray(['error' => '你没有查看跟进记录的权限!']);
        }
        if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
        
        $data = $activityLogic->read($this->param['activity_id']);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 编辑活动【跟进记录】
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update(ActivityLogic $activityLogic)
    {
        if (!checkPerByAction('crm', 'activity', 'update')) {
            return resultArray(['error' => '你没有编辑跟进记录的权限!']);
        }
        if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
        if (empty($this->param['activity_type'])) return resultArray(['error' => '缺少活动类型!']);
        if (empty($this->param['activity_type_id'])) return resultArray(['error' => '缺少活动类型ID!']);
        if (empty($this->param['content'])) return resultArray(['error' => '请填写跟进内容!']);
        
        $param = $this->param;
        $userId = $this->userInfo['id'];
        
        if (!$activityLogic->update($param)) return resultArray(['error' => '操作失败!']);
        
        $data = $activityLogic->getFollowData($param['activity_id'], $userId);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 删除活动【跟进记录】
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     */
    public function delete(ActivityLogic $activityLogic)
    {
        $userInfo = $this->userInfo;
        if (!checkPerByAction('crm', 'activity', 'delete')) {
            return resultArray(['error' => '你没有删除跟进记录的权限!']);
        }
        if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
        
        if (!$activityLogic->delete($this->param['activity_id'], $userInfo['id'])) return resultArray(['error' => '操作失败!']);
        return resultArray(['data' => '操作成功!']);
    }
    
    /**
     * 获取常用语
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     */
    public function getPhrase(ActivityLogic $activityLogic)
    {
        $data = $activityLogic->getPhrase();
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 设置常用语
     *
     * @param ActivityLogic $activityLogic
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setPhrase(ActivityLogic $activityLogic)
    {
        if (empty($this->param['phrase'])) return resultArray(['error' => '缺少常用语数据!']);
        if (!is_array($this->param['phrase'])) return resultArray(['error' => '参数格式错误!']);
        
        if (!$activityLogic->setPhrase($this->param['phrase'])) return resultArray(['error' => '操作失败!']);
        
        return resultArray(['data' => '操作成功!']);
    }
    
    /**
     * 跟进记录权限
     *
     * @return \think\response\Json
     */
    public function getRecordAuth()
    {
        $data = [
            'index' => checkPerByAction('crm', 'activity', 'index'),
            'read' => checkPerByAction('crm', 'activity', 'read'),
            'save' => checkPerByAction('crm', 'activity', 'save'),
            'update' => checkPerByAction('crm', 'activity', 'update'),
            'delete' => checkPerByAction('crm', 'activity', 'delete'),
        ];
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 导入模板下载
     * @author      alvin guogaobo
     * @version     1.0 版本号
     * @since       2021/4/10 0010 16:01
     */
    public function excelDownload($save_path = '')
    {
        $param = $this->param;
        $excelModel = new \app\admin\model\Excel();
        $field_list = $this->importData($param);
        $types = 'crm_activity';
        $excelModel->excelImportDownload($field_list, $types, $save_path);
    }
    
    /**
     * 导入导出模板标题
     * @param $param
     *
     * @author      alvin guogaobo
     * @version     1.0 版本号
     * @since       2021/4/13 0013 11:15
     */
    public function importData($param)
    {
        $field_list = [];
        switch ($param['crmType']) {
            case 1 :
                $field = [
                    '2' => [
                        'name' => '所属线索',
                        'field' => 'activity_type_id',
                        'form_type' => 'leads_id',
                        'is_null' => 1,
                    ]
                ];
                $name = '所属线索';
                break;
            case 3:
                $field = [
                    '2' => [
                        'name' => '所属联系人',
                        'field' => 'activity_type_id',
                        'form_type' => 'contacts_id',
                        'is_null' => 1,
                    ],
                ];
                $name = '所属联系人';
                break;
            case 5:
                $field = [
                    '2' => [
                        'name' => '所属商机',
                        'field' => 'activity_type_id',
                        'form_type' => 'business_id',
                        'is_null' => 1,
                    ],
                ];
                $name = '所属商机';
                break;
            case 6:
                $field = [
                    '2' => [
                        'name' => '所属合同',
                        'field' => 'activity_type_id',
                        'form_type' => 'contract_id',
                        'is_null' => 1,
                    ],
                ];
                $name = '所属合同';
                break;
            case 2:
                $field = [
                    '2' => [
                        'name' => '所属客户',
                        'field' => 'activity_type_id',
                        'form_type' => 'customer_id',
                        'is_null' => 1,
                    ],
                ];
                $name = '所属客户';
                break;
        }

        $record_type = db('crm_config')->where(['name' => 'record_type'])->find();
        if ($record_type) {
            $arr = json_decode($record_type['value']);
        }
        $fields = [
            '0' => [
                'name' => '跟进内容',
                'field' => 'content',
                'form_type' => 'text',
                'is_null' => 1,
            ],
            '1' => [
                'name' => '创建人',
                'field' => 'create_user_id',
                'form_type' => 'user',
                'is_null' => 1,
            ],
            '2' => [
                'name' => '跟进时间-例:2020-2-1',
                'field' => 'next_time',
                'form_type' => 'datetime',
            ],
            '3' => [
                'name' => '跟进方式',
                'field' => 'category',
                'form_type' => 'select',
                'setting' =>$arr
            ],
        ];
        // 导入的字段列表
        if ($param['is_excel']) {
            $field_lists = [
                '0' => ['name' => $name, 'field' => 'activity_type_name'],
                '1' => ['name' => '跟进内容', 'field' => 'content'],
                '2' => ['name' => '创建人', 'field' => 'create_user_name'],
                '3' => ['name' => '跟进时间', 'field' => 'create_time'],
                '4' => ['name' => '跟进方式', 'field' => 'category'],
                '5' => ['name' => '下次联系时间', 'field' => 'next_time']
            ];
            if ($param['crmType'] == 2) {
                $fields = [
                    '6' => ['name' => '相关联系人', 'field' => 'contacts_ids'],
                    '7' => ['name' => '相关商机', 'field' => 'business_ids'],];
                $field_list = array_merge($field_lists, $fields);
            } else {
                $field_list =$field_lists;
            }
        } else {
            $first_array = array_splice($fields, 2, 0, $field);
            $field_list = $fields;
            if ($param['crmType'] == 2) {
                $field = [
                    '5' => [
                        'name' => '相关联系人',
                        'field' => 'contacts_ids',
                        'form_type' => 'contacts_id',
                    ],
                    '6' => [
                        'name' => '相关商机',
                        'field' => 'business_ids',
                        'form_type' => 'business_id',
                    ]
                ];
                $field_list = array_merge($field_list, $field);
            }
        }
        return $field_list;
    }
    
    /**
     * 导入数据
     *
     * @author      alvin guogaobo
     * @version     1.0 版本号
     * @since       2021/4/10 0010 16:27
     */
    public function excelImport()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id'];
        $field_list = $this->importData($param);
        $excelModel = new \app\admin\model\Excel();
        $file = request()->file('file');
        switch ($param['crmType']) {
            case 1 :
                $param['types'] = 'crm_leads';
                $param['activity_type'] = 1;
                break;
            case 3:
                $param['types'] = 'crm_contacts';
                $param['activity_type'] = 3;
                break;
            case 5:
                $param['types'] = 'crm_business';
                $param['activity_type'] = 5;
                break;
            case 6:
                $param['types'] = 'crm_contract';
                $param['activity_type'] = 6;
                break;
            case 2:
                $param['types'] = 'crm_customer';
                $param['activity_type'] = 2;
                break;
        }
        $res = $excelModel->ActivityImport($file, $field_list, $param, $this);
        if (!$res) {
            return resultArray(['error' => $excelModel->getError()]);
        }
        return resultArray(['data' => $excelModel->getError()]);
    }
    
    /**
     * 导出跟进记录
     * action 列表分辨是否导出
     * label 导出类型 合同 客户 联系人
     *
     * @author      alvin guogaobo
     * @version     1.0 版本号
     * @since       2021/4/13 0013 11:32
     */
    public function excelExport()
    {
        
        $activityLogic = new ActivityLogic();
        $indexLogic = new \app\crm\logic\IndexLogic();
        $userInfo = $this->userInfo;
        $param = $this->param;
        $param['action'] = 'crm_activity';
        $param['is_excel'] = 1;
        $param['id'] = $userInfo['id'];
        $list = $indexLogic->activityList($param);
        $field_list = $this->importData($param);
        $data = $activityLogic->excelExport($field_list, $list);
        return $data;
    }
}