<?php
/**
 * 客户公海
 *
 * @author fanqi
 * @since 2021-04-13
 */

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\admin\model\Message;
use app\admin\model\User;
use app\crm\logic\CustomerPoolLogic;
use think\Hook;
use think\Request;
use think\response\Json;

class CustomerPool extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function _initialize()
    {
        $action = [
            'permission' => [],
            'allow' => [
                'index',
                'read',
                'pondlist',
                'field',
                'advanced',
                'authority',
                'receive',
                'distribute',
                'delete',
                'fieldconfig',
                'setfieldwidth',
                'setfieldconfig',
                'exceldownload',
                'import',
                'export',
                'checkCustomerReveive'
            ]
        ];
        Hook::listen('check_auth', $action);
        $request = Request::instance();
        $a = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::_initialize();
        }
    }
    
    /**
     * 公海列表
     *
     * @return Json
     * @since 2021-04-14
     * @author fanqi
     */
    public function index()
    {
        if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
        
        $data = (new CustomerPoolLogic())->getPoolList($this->param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 详情
     *
     * @return Json
     * @since 2021-04-14
     * @author fanqi
     */
    public function read()
    {
        if (empty($this->param['pool_id']) || empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
        
        $userInfo = $this->userInfo;
        $param = $this->param;
        $param['user_id'] = $userInfo['id'];
        
        $data = (new CustomerPoolLogic())->getPoolData($param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 删除公海客户
     *
     * @return Json
     * @since 2021-04-15
     * @author fanqi
     */
    public function delete()
    {
        if (empty($this->param['id'])) return resultArray(['error' => '请选择要删除的客户!']);
        
        $this->param['user_id'] = $this->userInfo['id'];
        
        $result = (new CustomerPoolLogic())->deletePoolCustomer($this->param);
        
        if (!empty($result)) return resultArray(['error' => $result]);
        
        return resultArray(['data' => '删除成功!']);
    }
    
    /**
     * 公海池列表
     *
     * @return Json
     * @since 2021-04-13
     * @author fanqi
     */
    public function pondList()
    {
        $data = (new CustomerPoolLogic())->getPondList(['user_id' => $this->userInfo['id'], 'structure_id' => $this->userInfo['structure_id']]);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 公海字段
     *
     * @return Json
     * @since 2021-04-13
     * @author fanqi
     */
    public function field()
    {
        if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
        
        $userInfo = $this->userInfo;
        $param = $this->param;
        $param['user_id'] = $userInfo['id'];
        
        $data = (new CustomerPoolLogic())->getFieldList($param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 高级筛选字段列表
     *
     * @return Json
     * @since 2021-04-14
     * @author fanqi
     */
    public function advanced()
    {
        if (empty($this->param['types'])) return resultArray(['error' => '缺少模块类型!']);
        
        $data = (new CustomerPoolLogic())->getAdvancedFilterFieldList($this->param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 领取公海池客户
     *
     * @return Json
     * @since 2021-04-15
     * @author fanqi
     */
    public function receive()
    {
        if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要领取的公海客户!']);
        
        $param = $this->param;
        $param['user_id'] = $this->userInfo['id'];
        
        $result = (new CustomerPoolLogic())->customerReceiveExamine($param);
        
        if (!empty($result)) return resultArray(['error' => $result]);
        
        return resultArray(['data' => '领取成功!']);
    }
    
    /**
     * 分配公海客户
     *
     * @return Json
     * @since 2021-04-15
     * @author fanqi
     */
    public function distribute()
    {
        if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要分配的公海客户!']);
        if (empty($this->param['user_id'])) return resultArray(['error' => '请选择要分配的员工!']);
        
        $result = (new CustomerPoolLogic())->distributeCustomer($this->param);
        
        if (!empty($result)) return resultArray(['error' => $result]);
        
        return resultArray(['data' => '分配成功!']);
    }
    
    // 公海客户导入模板下载
    public function excelDownload($save_path = '')
    {
        $excelModel = new \app\admin\model\Excel();
        $param = $this->param;
        $field_list = $this->fieldsData($param);
        $excelModel->excelImportDownload($field_list, 'crm_pool', $save_path);
    }
    
    // 导入
    public function import()
    {
        
        $param = $this->param;
        $userInfo = $this->userInfo;
        $excelModel = new \app\admin\model\Excel();
        $param['create_user_id'] = $userInfo['id'];
        $param['deal_time'] = time();
        $param['types'] = 'crm_customer';
        $param['deal_status'] = '未成交';
        $file = request()->file('file');
        // $res = $excelModel->importExcel($file, $param, $this);
        $res = $excelModel->batchImportData($file, $param, $this);
        RecordActionLog($userInfo['id'], 'crm_customer', 'excel', '导入公海客户', '', '', '导入公海客户');
        return resultArray(['data' => $excelModel->getError()]);
    }
    
    // 导出
    public function export()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $action_name = '导出全部';
        if ($param['customer_id']) {
            $action_name = '导出选中';
        }
        $param['is_excel'] = 1;
        $excelModel = new \app\admin\model\Excel();
        // 导出的字段列表
        $field_list = $this->fieldsData($param);
        // 文件名
        $file_name = '5kcrm_customer_' . date('Ymd');
        $model = model('Customer');
        $temp_file = $param['temp_file'];
        unset($param['temp_file']);
        $page = $param['page'] ?: 1;
        unset($param['page']);
        unset($param['export_queue_index']);
//        p($field_list);
        return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
            $param['page'] = $page;
            $param['limit'] = $limit;
            $data = (new CustomerPoolLogic())->getPoolList($param);
            $data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
            return $data;
        });
        RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出客户');
        return resultArray(['error' => 'error']);
    }
    
    /**
     * 公海权限
     *
     * @return Json
     * @since 2021-04-14
     * @author fanqi
     */
    public function authority()
    {
        $param = $this->param;
        $param['user_id'] = $this->userInfo['id'];
        $param['structure_id'] = $this->userInfo['structure_id'];
        
        $data = (new CustomerPoolLogic())->getAuthorityData($param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 公海字段样式
     *
     * @return Json
     * @since 2021-04-22
     * @author fanqi
     */
    public function fieldConfig()
    {
        if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
        
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id'];
        
        $data = (new CustomerPoolLogic())->getFieldConfigIndex($param);
        
        return resultArray(['data' => $data]);
    }
    
    /**
     * 设置公海字段宽度
     *
     * @return Json
     * @since 2021-04-22
     * @author fanqi
     */
    public function setFieldWidth()
    {
        if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
        if (empty($this->param['field'])) return resultArray(['error' => '缺少字段名称!']);
        if (empty($this->param['width'])) return resultArray(['error' => '缺少宽度值!']);
        
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id'];
        
        (new CustomerPoolLogic())->setFieldWidth($param);
        
        return resultArray(['data' => '操作成功!']);
    }
    
    /**
     * 设置公海字段样式
     *
     * @return Json
     * @since 2021-04-22
     * @author fanqi
     */
    public function setFieldConfig()
    {
        if (empty($this->param['pool_id'])) return resultArray(['缺少公海ID!']);
        if (empty($this->param['value']) && empty($this->param['hide_value'])) return resultArray(['error' => '字段参数错误!']);
        
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id'];
        
        (new CustomerPoolLogic())->setFieldConfig($param);
        
        return resultArray(['data' => '操作成功!']);
    }
    
    /**
     * 导入模板
     * @param $param
     *
     * @author      alvin guogaobo
     * @version     1.0 版本号
     * @since       2021/5/17 0017 11:34
     */
    public function fieldsData($param)
    {
        $where=['pool_id' => $param['pool_id'], 'is_hidden' => 0,
            'field_name' => ['not in', ['deal_status','create_user_id']]];
        if($param['is_excel']==1){
            $where=['pool_id' => $param['pool_id'], 'is_hidden' => 0,
                'field_name' => ['not in', ['deal_status']]];
        }
        $pool_list = db('crm_customer_pool_field_setting')->where($where)->field('field_name as field,form_type,name')->select();
        $fieldParam['types'] = 'crm_customer';
        $fieldParam['action'] = 'excel';
        $setting = [];
        $list=[];
        foreach ($pool_list as $k => &$v) {
            switch ($v['field']) {
                case 'address':
                    if($param['is_excel']!=1){
                        $v['field'] = 'customer_address';
                        $v['form_type'] = 'map_address';
                        $list[]=$v;
                        unset($pool_list[$k]);
                    }
                    break;
                case 'detail_address':
                    if($param['is_excel']!=1){
                        unset($pool_list[$k]);
                    }
                    break;
                case ($v['form_type'] == 'select') || ($v['form_type'] == 'checkbox') :
                    $set= db('admin_field')->where(['types' => 'crm_customer', 'field' => $v['field']])->find();
                    $pool_list[$k]['setting'] = explode(chr(10), $set['setting']);
                    break;
                default :
                    if($param['is_excel']!=1){
                        $pool_list[$k]['setting'] = [];
                    }
                    break;
            }
//            if($param['is_excel']==1){
//                foreach ( $pool_list as &$v){
//                    if($v['field']=='create_user_id'){
//                        $v['field'] = 'create_user_name';
//                    }elseif($v['field']=='before_owner_user_id'){
//                        $v['field'] = 'create_user_name';
//                    }
//                }
//            }
        }
        $pool_list=array_merge($pool_list,$list);
        return $pool_list;
    }

    /**
     * 领取公海池客户
     *
     * @return Json
     * @since 2021-04-15
     * @author fanqi
     */
    public function checkCustomerReveive()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $user_id = $userInfo['id'];
        $customerCheckModel = model('CustomerCheck');
        $examineStepModel = new \app\admin\model\ExamineStep();
        $examineRecordModel = new \app\admin\model\ExamineRecord();
        $examineFlowModel = new \app\admin\model\ExamineFlow();
        $customerModel = model('Customer');

        $customerCheckData = [];
        $customerCheckData['update_time'] = time();
        $customerCheckData['check_status'] = 1; //0待审核,1审核通中,2审核通过,3审核未通过
        //权限判断
        if (!$examineStepModel->checkExamine($user_id, 'crm_contract', $param['id'])) {
            return resultArray(['error' => $examineStepModel->getError()]);
        }
        //审批主体详情
        $dataInfo = db('crm_customer_check')->where('customer_check_id', 'in', $param['customer_check_id'])->find();
        $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
        $is_end = 0; // 1审批结束

        $status = $param['status'] ? 1 : 0; //1通过,0驳回
        $checkData = [];
        $checkData['check_user_id'] = $user_id;
        $checkData['types'] = 'crm_customer_check';
        $checkData['types_id'] = $param['id'];
        $checkData['check_time'] = time();
        $checkData['content'] = $param['content'];
        $checkData['flow_id'] = $dataInfo['flow_id'];
        $checkData['order_id'] = $dataInfo['order_id'] ? : 1;
        $checkData['status'] = $status;

        if ($status == 1) {
            if ($flowInfo['config'] == 1) {
                //固定流程
                //获取下一审批信息
                $nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_customer_check', $param['id'], $dataInfo['order_id'], $user_id);
                $next_user_ids = $nextStepData['next_user_ids'] ? : [];
                $customerCheckData['order_id'] = $nextStepData['order_id'] ? : '';
                if (!$next_user_ids) {
                    $is_end = 1;
                    //审批结束
                    $checkData['check_status'] = !empty($status) ? 2 : 3;
                    $customerCheckData['check_user_id'] = '';
                } else {
                    //修改主体相关审批信息
                    $customerCheckData['check_user_id'] = arrayToString($next_user_ids);
                }
            } else {
                //自选流程
                $is_end = $param['is_end'] ? 1 : '';
                $check_user_id = $param['check_user_id'] ? : '';
                if ($is_end !== 1 && empty($check_user_id)) {
                    return resultArray(['error' => '请选择下一审批人']);
                }
                $customerCheckData['check_user_id'] = arrayToString($param['check_user_id']);
            }
            if ($is_end == 1) {
                $checkData['check_status'] = !empty($status) ? 2 : 3;
                $customerCheckData['check_user_id'] = '';
                $customerCheckData['check_status'] = 2;
            }
        } else {
            //审批驳回
            $is_end = 1;
            $customerCheckData['check_status'] = 3;
            //将审批记录至为无效
            // $examineRecordModel->setEnd(['types' => 'crm_contract','types_id' => $param['id']]);
        }
        //已审批人ID
        $customerCheckData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']),[$user_id])) : arrayToString([$user_id]);
        $resCustomerCheck = db('crm_customer_check')->where(['customer_check_id' => $param['id']])->update($customerCheckData);
        if ($resCustomerCheck) {
            //审批记录
            $resRecord = $examineRecordModel->createData($checkData);
            //审核通过,修改相关信息
            if ($is_end == 1 && !empty($status)) {
                // 审批通过消息告知负责人
                (new Message())->send(
                    Message::CUSTOMER_CHECK_PASS,
                    [
                        'title' => $dataInfo['name'],
                        'action_id' => $param['id']
                    ],
                    $dataInfo['owner_user_id']
                );

                $customerData = [];
                $customerData['user_id']=$dataInfo['user_id'];
                $customerData['customer_id']=$dataInfo['customer_id'];

                $result = (new CustomerPoolLogic())->receiveCustomers($customerData);

                if (!empty($result)) return resultArray(['error' => $result]);

                return resultArray(['data' => '领取成功!']);
            } else {
                if ($status) {
                    //发送站内信
                    // 通过未完成,发送消息给
                    (new Message())->send(
                        Message::CUSTOMER_CHECK_TO_DO,
                        [
                            'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
                            'title' => $dataInfo['name'],
                            'action_id' => $param['id']
                        ],
                        stringToArray($customerCheckData['check_user_id'])
                    );
                } else {
                    (new Message())->send(
                        Message::CONTRACT_REJECT,
                        [
                            'title' => $dataInfo['name'],
                            'action_id' => $param['id']
                        ],
                        $dataInfo['owner_user_id']
                    );
                }
            }
            return resultArray(['data' => '审批成功']);
        } else {
            return resultArray(['error' => '审批失败,请重试!']);
        }
    }
}