| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- <?php
- // +----------------------------------------------------------------------
- // | Description: 场景
- // +----------------------------------------------------------------------
- // | Author: Michael_xu | gengxiaoxu@5kcrm.com
- // +----------------------------------------------------------------------
-
- namespace app\admin\model;
-
- use think\Db;
- use app\admin\model\Common;
-
- class Scene extends Common
- {
- /**
- * 为了数据库的整洁,同时又不影响Model和Controller的名称
- * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
- */
- protected $name = 'admin_scene';
- protected $createTime = 'create_time';
- protected $updateTime = false;
- protected $autoWriteTimestamp = true;
-
- private $types_arr = ['crm_leads','crm_customer','crm_customer_pool','crm_contacts','crm_product','crm_business','crm_contract','crm_receivables','crm_visit','crm_invoice', 'jxc_product', 'jxc_supplier', 'jxc_purchase', 'jxc_retreat', 'jxc_sale', 'jxc_salereturn', 'jxc_receipt', 'jxc_outbound', 'jxc_payment', 'jxc_collection', 'jxc_inventory', 'jxc_allocation']; //支持场景的分类
-
- protected $type = [
- 'data' => 'array',
- ];
-
- /**
- * 创建场景
- * @param
- * @return
- */
- public function createData($param, $types = '')
- {
- if (empty($types)) {
- $this->error = '参数错误';
- return false;
- }
- if (empty($param['name'])) {
- $this->error = '场景名称必填';
- return false;
- }
- $user_id = $param['user_id'];
- if ($this->where(['types'=>$types,'user_id'=>$user_id,'name'=>$param['name']])->find()) {
- $this->error = '场景名称已存在';
- return false;
- }
- # 转自定义场景结构
- $sceneData = [];
- foreach ($param['data'] AS $key => $value) {
- foreach ($value AS $k => $v) {
- $sceneData[$k] = [
- 'condition' => trim($v['condition']),
- 'value' => $v['value'],
- 'form_type' => $v['form_type'],
- 'name' => $v['name'],
- 'type' => $v['type']
- ];
- if (!empty($v['start'])) $sceneData[$k]['start'] = $v['start'];
- if (!empty($v['end'])) $sceneData[$k]['end'] = $v['end'];
- }
- }
- if (!empty($sceneData)) $param['data'] = $sceneData;
- $max_order_id = $this->getMaxOrderid($types, $user_id);
- $param['order_id'] = $max_order_id ? $max_order_id+1 : 0;
- $param['update_time'] = time();
- $param['type'] = 0;
- $param['bydata'] = '';
- $res = $this->allowField(true)->save($param);
- if ($res) {
- //设置默认
- if ($param['is_default']) {
- $defaultData = [];
- $defaultData['types'] = $types;
- $defaultData['user_id'] = $user_id;
- $this->defaultDataById($defaultData, $this->scene_id);
- }
- return true;
- } else {
- $this->error = '添加失败';
- return false;
- }
- }
-
- /**
- * [getDataList 场景list]
- * @param types 分类
- * @author Michael_xu
- * @return [array]
- */
- public function getDataList($types, $user_id)
- {
- $fieldModel = new \app\admin\model\Field();
- $userModel = new \app\admin\model\User();
- if (!in_array($types, $this->types_arr)) {
- $this->error = '参数错误';
- return false;
- }
- $map['user_id'] = $user_id;
- $map['is_hide'] = 0;
- $map['types'] = $types;
- $list = db('admin_scene')
- ->where($map)
- ->whereOr(function ($query) use ($types) {
- $query->where(['types' => $types,'type' => 1]);
- })->order('order_id asc,scene_id asc')
- ->select();
- $defaultSceneId = db('admin_scene_default')->where(['types' => $types,'user_id' => $user_id])->value('scene_id');
- $fieldList = $fieldModel->getField(['types' => $types]);
- $newFieldList = [];
- foreach ($fieldList as $k=>$v) {
- $field = $v['field'];
- if ($v['field'] == 'customer_id') $field = 'customer_name';
- $newFieldList[$field] = $v;
- }
- foreach ($list as $k=>$v) {
- if ($v['scene_id'] == $defaultSceneId) {
- $list[$k]['is_default'] = 1;
- }
- $data = $v['data'] ? json_decode($v['data'],true) : [];
- if ($data) {
- foreach ($data as $key=>$val) {
- $setting = $newFieldList[$key]['setting'];
- $data[$key]['setting'] = $setting;
- if ($val['form_type'] == 'user' && !empty($val['value'][0])) {
- $userInfo = $userModel->getUserById($val['value'][0]);
- $data[$key]['setting']['realname'] = $userInfo['realname'];
- $data[$key]['setting']['id'] = $userInfo['id'];
- }
- }
- }
- $list[$k]['data'] = $data ? : [];
- }
- $map['is_hide'] = 1;
- $hideList = $this->where($map)->order('order_id asc')->select();
- $data = [];
- $data['list'] = $list;
- $data['hideList'] = $hideList;
- return $data;
- }
-
- /**
- * 根据主键获取详情
- * @param array $param [description]
- * @author Michael_xu
- */
- public function getDataById($id = '', $user_id = '', $types = '')
- {
- $where = [];
- $where['scene_id'] = $id;
- $data = db('admin_scene')->where($where)->find();
- if (!$types) {
- $types = $data['types'] ? : '';
- }
- //处理data
- if ($data['bydata'] && $types) {
- $data = $this->getByData($types, $data['bydata'], $user_id);
- } else {
- $data = json_decode($data['data'],true);
- if (is_array($data)) {
- foreach ($data as $k=>$v) {
- if ($v['form_type'] == 'business_type') {
- $data[$k]['value'] = $v['type_id'];
- }
- }
- }
- }
- return $data ? : [];
- }
-
- /**
- * 根据主键修改
- * @param array $param [description]
- * @author Michael_xu
- */
- public function updateDataById($param, $id)
- {
- $checkData = $this->get($id);
- $sceneInfo = $checkData->data;
- $user_id = $param['user_id'];
- if (!$sceneInfo) {
- $this->error = '暂无数据';
- return false;
- }
- //权限(只能编辑自己的)
- if ($sceneInfo['user_id'] !== $user_id) {
- $this->error = '参数错误';
- return false;
- }
- if (empty($param['name'])) {
- $this->error = '场景名称必填';
- return false;
- }
- if ($this->where(['scene_id' => ['neq', $id], 'types' => $param['types'], 'user_id' => $user_id,'name' => $param['name']])->find()) {
- $this->error = '场景名称已存在';
- return false;
- }
- # 转自定义场景结构
- $sceneData = [];
- foreach ($param['data'] AS $key => $value) {
- foreach ($value AS $k => $v) {
- $sceneData[$k] = [
- 'condition' => trim($v['condition']),
- 'value' => $v['value'],
- 'form_type' => $v['form_type'],
- 'name' => $v['name'],
- 'type' => $v['type']
- ];
- if (!empty($v['start'])) $sceneData[$k]['start'] = $v['start'];
- if (!empty($v['end'])) $sceneData[$k]['end'] = $v['end'];
- }
- }
- if (!empty($sceneData)) $param['data'] = $sceneData;
- $param['update_time'] = time();
- // $scene_data = $this->dataChangeString($param);
- //处理data数据
- $res = $this->allowField(true)->save($param, ['scene_id' => $id]);
- if ($param['is_default'] == 1) {
- $this->defaultDataById($param,$param['id']);
- } else {
- db('admin_scene_default')->where(['user_id' => $user_id, 'types' => $param['types'], 'scene_id' => $id])->delete();
- }
- if ($res) {
- return true;
- } else {
- $this->error = '修改失败';
- return false;
- }
- }
-
- /**
- * 场景设置为默认
- * @param types 类型
- * @param user_id 人员ID
- * @param id 场景ID
- * @author Michael_xu
- */
- public function defaultDataById($param, $id)
- {
- if (!$param['types'] || !$id) {
- $this->error = '参数错误';
- return false;
- }
- $resInfo = db('admin_scene_default')->where(['types' => $param['types'],'user_id' => $param['user_id']])->find();
- if ($resInfo) {
- $res = db('admin_scene_default')->where(['types' => $param['types'],'user_id' => $param['user_id']])->update(['scene_id' => $id]);
- } else {
- $data = [];
- $data['types'] = $param['types'];
- $data['user_id'] = $param['user_id'];
- $data['scene_id'] = $id;
- $res = db('admin_scene_default')->insert($data);
- }
- if (!$res) {
- $this->error = '设置失败或数据无变化';
- return false;
- }
- return true;
- }
-
- /**
- * 场景数据转换(字符串形式存储)
- * @param
- * @author Michael_xu
- * @return
- */
- public function dataChangeString($param = [])
- {
- $scene_data = '[';
- $field_arr = [];
- $i = 0;
- foreach ($param['data'] as $k=>$v) {
- if ($v != '' && !in_array($v['field'], $field_arr)) {
- $i++;
- if ($i == 1) {
- $scene_data .= $v['field']."=>[";
- } else {
- $scene_data .= ",".$v['field']."=>[";
- }
- $scene_data .= "field=>".$v['field'].",";
- //处理条件和值
- foreach ($param as $k1=>$v1) {
- switch ($k1) {
- case 'condition' : $scene_data .= "condition=>".$v1.","; break;
- case 'value' : $scene_data .= "value=>".$v1.","; break;
- case 'state' :
- case 'city' :
- case 'area' :
- //处理地址类型数据
- $scene_data .= $k1."=>".$v1.","; break;
- case 'start' : $scene_data .= "start=>".$v1.","; break;
- case 'end' : $scene_data .= "end=>".$v1.","; break;
- case 'start_date' : $scene_data .= "start_date=>".$v1.","; break;
- case 'end_date' : $scene_data .= "end_date=>".$v1.","; break;
- }
- }
- $form_type = '';
- //处理字段类型
- if ($v['field'] == 'create_time' || $v['field'] == 'update_time') {
- $form_type = 'datetime';
- } else {
- $form_type = db('admin_fields')->where(['types'=>$param['types'],'field'=>$v['field'],'is_main'=>1])->column('form_type');
- }
- $scene_data .= "form_type=>".$form_type;
- $field_arr[] = $v['field'];
- }
- $scene_data .= ']';
- }
- $scene_data .= ']';
- return $scene_data;
- }
-
- /**
- * 场景排序最大值
- * @param
- * @author Michael_xu
- * @return
- */
- public function getMaxOrderid($types, $user_id)
- {
- $maxOrderid = $this->where(['types' => $types, 'user_id' => $user_id])->max('order_id');
- return $maxOrderid ? : 0;
- }
-
- /**
- * 场景数据(转数组格式),用于where条件
- * @param
- * @author Michael_xu
- * @return
- */
- public function dataChangeArray($string)
- {
- $data_arr = [];
- $where = [];
- eval('$data_arr = '.$string.';');
- foreach ($data_arr as $k=>$v) {
- if ($v['state']) {
- $address_where[] = '%'.$v['state'].'%';
- if ($v['city']) {
- $address_where[] = '%'.$v['city'].'%';
- if ($v['area']) {
- $address_where[] = '%'.$v['area'].'%';
- }
- }
- if ($v['condition'] == 'not_contain') {
- $where[$k] = ['notlike', $address_where, 'OR'];
- } else {
- $where[$k] = ['like', $address_where, 'AND'];
- }
- } elseif (!empty($v['start']) || !empty($v['end'])) {
- if ($v['start'] && $v['end']) {
- $where[$k] = ['between',[strtotime($v['start']),strtotime($v['end'])+86399]];
- } elseif ($v['start']) {
- $where[$k] = ['egt',strtotime($v['start'])];
- } else {
- $where[$k] = ['elt',strtotime($v['end'])+86399];
- }
- } elseif (!empty($v['value'])) {
- $where[$k] = field($v['value'],$v['condition']);
- }
- }
- return $where ? : [];
- }
-
- /**
- * 场景排序
- * @param ids 场景id数组
- * @param hide_ids 隐藏场景id数组
- * @author Michael_xu
- * @return
- */
- public function listOrder($param, $user_id)
- {
- $res = true;
- $resHide = true;
- //使用
- $data = [];
- foreach ($param['ids'] as $k=>$v) {
- $data[] = ['scene_id' => $v,'order_id' => $k,'update_time' => time(),'user_id' => $user_id,'is_hide' => 0];
- }
- if ($param['ids']) $res = $this->isUpdate()->saveAll($data);
- //隐藏
- $hideData = [];
- foreach ($param['hide_ids'] as $k=>$v) {
- $hideData[] = ['scene_id' => $v,'order_id' => $k,'update_time' => time(),'user_id' => $user_id,'is_hide' => 1];
- }
- if ($param['hide_ids']) $resHide = $this->isUpdate()->saveAll($hideData);
- if ($res == false || $resHide == false) {
- $this->error = '设置出错,请重试';
- return false;
- }
- return true;
- }
-
- /**
- * [getDefaultData 默认场景]
- * @param types 分类
- * @author Michael_xu
- * @return [array]
- */
- public function getDefaultData($types, $user_id)
- {
- $where = [];
- $where['types'] = $types;
- $where['user_id'] = $user_id;
- $scene_id = db('admin_scene_default')->where($where)->value('scene_id');
- if (!$scene_id && in_array($types,['crm_leads','crm_customer','crm_business','crm_contacts','crm_contract','crm_receivables','crm_visit','crm_invoice'])) {
- $resData['bydata'] = 'all';
- } else {
- $resData = db('admin_scene')->where(['scene_id' => $scene_id])->find();
- }
- if ($resData['bydata']) {
- $data = $this->getByData($types, $resData['bydata'], $user_id);
- } else {
- //处理data
- $data = $resData ? json_decode($resData,true) : [];
- }
- return $data;
- }
-
- /**
- * [getByData 系统场景数据]
- * @param types 分类
- * @author Michael_xu
- * @return [array]
- */
- public function getByData($types, $bydata, $user_id)
- {
- $map = [];
- $auth_user_ids = [];
- $part_user_ids = [];
-
- switch ($bydata) {
- case 'me' :
- # 我负责的
- $auth_user_ids[] = $user_id;
- break;
- case 'mePart' :
- # 我参与的(即相关团队)
- $part_user_ids = $user_id;
- break;
- case 'sub' :
- # 下属负责的
- $auth_user_ids = getSubUserId(false, 0, $user_id) ? : ['-1'];
- break;
- case 'all' :
- # 全部
- $auth_user_ids = '';
- break;
- case 'is_transform' :
- # 已转化线索
- $map['is_transform'] = ['condition' => 'is', 'value' => [1], 'form_type' => 'text', 'name' => ''];
- break;
- case 'pool' :
- # 今日进入公海的客户
- $customerModel = new \app\crm\model\Customer();
- $map = $customerModel->getWhereByToday();
- break;
- case 'win_business' :
- # 赢单的商机
- $map['is_end'] = ['condition' => 'is', 'value' => [1], 'form_type' => 'text'];
- break;
- case 'fail_business' :
- # 输单的商机
- $map['is_end'] = ['condition' => 'is', 'value' => [2], 'form_type' => 'text'];
- break;
- case 'star' :
- # 我的关注
- $where = $this->getStarParam($types, $user_id);
- if (!empty($where)) $map = $where;
- break;
- case 'go_business' :
- # 进行中的商机
- $map['is_end'] = ['condition' => 'is', 'value' => ['0'], 'form_type' => 'text'];
- break;
- case 'put_on_shelves' :
- # 上架的产品
- $map['status'] = ['condition' => 'is', 'value' => ['上架'], 'form_type' => 'select', 'name' => '是否上架', 'type' => 'status'];
- break;
- case 'pull_off_shelves' :
- # 下架的产品
- $map['status'] = ['condition' => 'is', 'value' => ['下架'], 'form_type' => 'select', 'name' => '是否上架', 'type' => 'status'];
- break;
- case 'jxc_put_on_shelves' :
- # 上架的产品
- $map['product_ishelf'] = ['condition' => 'is', 'value' => ['上架'], 'form_type' => 'select', 'name' => '是否上架', 'type' => 'product_ishelf'];
- break;
- case 'jxc_pull_off_shelves' :
- # 下架的产品
- $map['product_ishelf'] = ['condition' => 'is', 'value' => ['下架'], 'form_type' => 'select', 'name' => '是否上架', 'type' => 'product_ishelf'];
- break;
- default :
- # 全部
- $auth_user_ids = '';
- }
- if($types=='receivables_plan'){
- switch ($bydata) {
- case 'me' :
- # 我负责的
- $auth_user_ids[] = $user_id;
- break;
- case 'sub' :
- # 下属负责的
- $auth_user_ids = getSubUserId(false, 0, $user_id) ? : ['-1'];
- break;
- case 'all' :
- # 全部
- $auth_user_ids = '';
- break;
- case 'not' :
- # 全部未回款
- $map['status'] = ['neq',1];
- break;
- case 'not_overdue' :
- # 全部逾期未回款
- // $map['status'] =array(['neq',1],['neq',4],'or');
- $map['status'] =['neq',1];
- $map['return_date'] = ['lt',time()];
- break;
- case 'not_moth' :
- # 本月到期未回款
- $timeAry = ByDateTime('month');
- $map['status'] =['neq',1];
- $map['return_date'] = ['between',[$timeAry[0],$timeAry[1]]];
- break;
- case 'not_week' :
- # 本周到期未回款
- $timeAry = ByDateTime('week');
- $map['status'] =['neq',1];
- $map['return_date'] = ['between',[$timeAry[0],$timeAry[1]]];
- break;
- case 'complete' :
- # 已回款
- $map['status'] =['eq',1];
- break;
- case 'complete_moth' :
- # 本月已回款
- $timeAry = ByDateTime('month');
- $map['status'] =['eq',1];
- $map['return_date'] = ['between',[$timeAry[0],$timeAry[1]]];
-
- break;
- case 'complete_week' :
- # 本周已回款
- $timeAry = ByDateTime('week');
- $map['status'] =['eq',1];
- $map['return_date'] = ['between',[$timeAry[0],$timeAry[1]]]; break;
- case 'complete_part' :
- # 部分已回
- $map['status'] = ['eq',2];
- break;
- default :
- # 全部
- $auth_user_ids = '';
- }
- }
- $auth_user_ids = $auth_user_ids ? : [];
-
- if ($auth_user_ids) {
- $map['owner_user_id'] = ['condition' => 'in', 'value' => $auth_user_ids, 'form_type' => 'user', 'name' => ''];
- }
-
- if ($part_user_ids) {
- $map['ro_user_id'] = $part_user_ids;
- $map['rw_user_id'] = $part_user_ids;
- }
-
- return $map;
- }
-
- /**
- * 获取我的关注条件
- *
- * @param $types
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getStarParam($types, $user_id)
- {
- $primaryKey = getPrimaryKeyName('crm_' . $types);
-
- $targetIds = Db::name('crm_star')->where(['user_id' => $user_id, 'type' => 'crm_' . $types])->column('target_id');
-
- $where[$primaryKey] = ['condition' => 'in', 'value' => !empty($targetIds) ? $targetIds : [-1], 'form_type' => 'user'];
-
- return $where;
- }
-
- /**
- * [updateData 跳过验证的编辑]
- * @param types 分类
- * @author Michael_xu
- * @return [array]
- */
- public function updateData($data, $scene_id)
- {
- $param['data'] = is_array($data) ? $data : '';
- $param['update_time'] = time();
- $res = $this->allowField(true)->save($param, ['scene_id' => $scene_id]);
- if ($res) {
- return true;
- } else {
- $this->error = '修改失败';
- return false;
- }
- }
- }
|