request->isGet()) { $id = input('id'); $this->vo = $this->app->db->name('merchant_list')->where(array('id' => $id))->find(); $this->title = '商户关联渠道列表'; $w = array ( 'mid' => $id, ); $relation = $this->app->db->name($this->table)->where($w)->order('sort desc,id desc')->select()->toArray(); foreach ($relation as $k => $v) { $this->relation[$v['pid']] = $v; } $w = array ( 'status' => 1, 'is_deleted' => 0, ); $channelData = $this->app->db->name('ChannelList')->where($w)->select()->toArray(); $channel = array(); foreach ($channelData as $k => $v) { $channel[$v['id']] = $v; } $sort = array(); $this->name = $this->product = array(); $product = $this->app->db->name('ChannelProduct')->where($w)->order('sort desc,id desc')->select()->toArray(); foreach ($product as $k => $v) { if (!isset($this->name[$v['key']])) { $this->name[$v['key']] = $v['name']; } if (!isset($channel[$v['cid']])) { continue; } $v['channel'] = $channel[$v['cid']]; # 可用面值 if ($v['value']) { $v['value'] = explode(',', $v['value']); } else { $v['value'] = array(); } if (isset($this->relation[$v['id']]) && $this->relation[$v['id']]) { $v['select'] = 1; $v['cash'] = explode(',', $this->relation[$v['id']]['cash']); $v['sort'] = $this->relation[$v['id']]['sort']; } else { $v['select'] = 0; $v['cash'] = array(); $v['sort'] = $v['sort']; } $sort[$v['key']][] = $v['sort']; $this->product[$v['key']][] = $v; array_multisort($sort, SORT_DESC, SORT_NUMERIC, $this->product); } if ($sort && $this->name) { foreach ($this->name as $k => $v) { if (isset($sort[$k])) { array_multisort($sort[$k], SORT_DESC, SORT_NUMERIC, $this->product[$k]); } } } $this->_form($this->table, 'setting','',[], $this->product); } else { $data = input(); $mid = $data['mid']; $all = $this->app->db->name($this->table)->where(array('mid' => $mid))->select()->toArray(); $select = $product_key = array(); if ($all) { foreach ($all as $k => $v) { $select[$v['pid']] = $v; } } if (isset($data['pid']) && $data['pid']) { foreach ($data['pid'] as $k => $v) { $t = explode('|', $v); $pid = $t[0]; $cid = $t[1]; $product = $this->app->db->name('ChannelProduct')->where(array('id' => $pid))->find(); if ($product) { $update['product_key'] = $product['key']; if (isset($data['sort_' . $pid]) && $data['sort_' . $pid]) { $update['sort'] = $data['sort_' . $pid]; } if (isset($data['cash_' . $pid]) && $data['cash_' . $pid]) { $update['cash'] = implode(',', $data['cash_' . $pid]); } else { $update['cash'] = ''; } $update['mid'] = $mid; $update['cid'] = $cid; $update['pid'] = $pid; if (isset($select[$pid]) && $select[$pid]) { $this->app->db->name($this->table)->where(array('id' => $select[$pid]['id']))->update($update); unset($select[$pid]); } else { $this->app->db->name($this->table)->insert($update); } } } } if ($select) { # 删除 foreach ($select as $k => $v) { $this->app->db->name($this->table)->where(array('id' => $v['id']))->delete(); } } $this->success('操作成功', ''); } } }