title = '渠道列表'; $query = $this->_query($this->table); $query->equal('status')->dateBetween('create_at'); $query->like('name,phone,contacts'); $query->where(['is_deleted' => 0]); $query->order('status desc, id desc')->page(); } /** * 添加渠道 * @auth true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function add() { $this->_applyFormToken(); $this->_form($this->table, 'form'); } /** * 编辑渠道 * @auth true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit() { $this->_applyFormToken(); $this->_form($this->table, 'form'); } /** * 表单数据处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ protected function _form_filter(&$data) { if ($this->request->isPost()) { if (empty($data['name'])) $this->error('渠道名称不能为空!'); if (!isset($data['id'])) { $data['channel_num']=uniqid('CH'); //if ($this->app->db->name($this->table)->where(['appid' => $data['appid'],'is_deleted' => 0])->find()) $this->error("APPID:{$data['appid']}已经存在!"); //if ($this->app->db->name($this->table)->where(['key' => $data['key'],'is_deleted' => 0])->find()) $this->error("KEY:{$data['key']}已经存在!"); } } } protected function _form_result($id, $data) { ChannelService::instance()->get($id, false); } /** * 修改渠道状态 * @auth true * @throws \think\db\exception\DbException */ public function state() { $this->_applyFormToken(); $this->_save($this->table, $this->_vali([ 'status.in:0,1' => '状态值范围异常!', 'status.require' => '状态值不能为空!', ])); } /** * 删除渠道 * @auth true * @throws \think\db\exception\DbException */ public function remove() { $this->_applyFormToken(); $this->_delete($this->table); } /** * 关联产品 * @auth true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setting() { if ($this->request->isGet()) { $id = input('cid'); $this->vo = $this->app->db->name($this->table)->where(array('id' => $id))->find(); $this->title = '关联产品'; $w = array ( 'cid' => $id, ); $channel_product = $this->app->db->name('channel_product')->where($w)->order('sort desc,id desc')->select()->toArray(); $relation = array(); if ($channel_product) { foreach ($channel_product as $k => $v) { $relation[$v['key']] = $v; } } $w = array ( 'status' => 1, ); $service = $this->app->db->name('service_info')->where($w)->select()->toArray(); $this->type = ProductBaseService::instance()->getType(); $this->service = $this->product = array(); foreach ($this->type as $k => $v) { $this->service[$k] = array(); } foreach ($service as $k => $v) { $w['service_id'] = $v['id']; $v['product'] = $this->app->db->name('service_product')->where($w)->select()->toArray(); if (isset($relation[$v['key']]) && $relation[$v['key']]) { $v['select'] = 1; $v['cash'] = $relation[$v['key']]['auto_value']; $v['cash'] = explode(',', $v['cash']); } else { $v['select'] = 2; $v['cash'] = array(); } $this->service[$v['type']][] = $v; } $this->_form($this->table, 'setting'); } else { $data = input(); $all = $this->app->db->name('channel_product')->where(array('cid' => $data['cid']))->select()->toArray(); $select = array(); if ($all) { foreach ($all as $k => $v) { $select[$v['key']] = $v; } } $service =$data['service']? implode(',', $data['service']):''; $service = $this->app->db->name('service_info')->whereRaw('id in('.$service.')')->select()->toArray(); if ($service) { foreach ($service as $k => $v) { $product = $this->app->db->name('service_product')->where(array('service_id' => $v['id'], 'status' => 1))->order('sort asc, status desc, id desc')->column('value'); $update = array(); $update['service_id'] = $v['id']; $update['name'] = $v['name']; $update['key'] = $v['key']; $update['value'] = implode(',', $product); $update['auto_value'] = ''; if (isset($data['product_' . $v['id']]) && $data['product_' . $v['id']]) { $update['auto_value'] = implode(',', $data['product_' . $v['id']]); } if (isset($select[$v['key']]) && $select[$v['key']]) { # 更新 $this->app->db->name('channel_product')->where(array('id' => $select[$v['key']]['id']))->update($update); unset($select[$v['key']]); } else { # 插入 $update['cid'] = $data['cid']; $this->app->db->name('channel_product')->insert($update); } } } if ($select) { # 删除 foreach ($select as $k => $v) { $this->app->db->name('channel_product')->where(array('id' => $v['id']))->delete(); } } $this->success('操作成功', ''); } } }