feat: init project codebase
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Package extends AdminController
|
||||
{
|
||||
protected $base_model = 'package_model';
|
||||
|
||||
function list_get()
|
||||
{
|
||||
$data = $this->_list_get(true);
|
||||
$package_up_ids = [];
|
||||
$disease_ids = [];
|
||||
foreach ($data['items'] as &$item) {
|
||||
$item['content'] = empty($item['content'])?[]:json_decode($item['content'],true);
|
||||
$item['structure'] = array_values(array_filter(explode(',',$item['structure'])));
|
||||
$item['structure_hide'] = array_values(array_filter(explode(',',$item['structure_hide'])));
|
||||
$item['cert'] = array_values(array_filter(explode(',',$item['cert'])));
|
||||
$item['disease'] = array_values(array_filter(explode(',',$item['disease'])));
|
||||
//套餐升级目标
|
||||
$package_up_ids[] = $item['package_up'];
|
||||
//套餐自带疾病
|
||||
$disease_ids = array_merge($disease_ids,$item['disease']);
|
||||
//套餐疾病选项
|
||||
foreach ($item['content'] as $val) {
|
||||
$disease_ids = array_merge($disease_ids,$val['disease_id']);
|
||||
}
|
||||
}
|
||||
//提供所有升级目标套餐的值
|
||||
$data['options']['package'] = $this->package_model->get($package_up_ids,null,true,'base.id,base.name');
|
||||
//提供所有疾病选项
|
||||
$this->load->model('disease_model');
|
||||
$data['options']['disease'] = $this->disease_model->get($disease_ids,null,true,'base.id,base.name');
|
||||
$this->success($data);
|
||||
}
|
||||
|
||||
function update_post()
|
||||
{
|
||||
$config = array(
|
||||
array(
|
||||
'field' => 'id',
|
||||
'label' => '序号',
|
||||
'rules' => 'trim|min_length[0]|max_length[20]'
|
||||
),
|
||||
array(
|
||||
'field' => 'name',
|
||||
'label' => '套餐名称',
|
||||
'rules' => 'trim|required|min_length[2]|max_length[20]',
|
||||
'errors' => array(
|
||||
'required' => 'You must provide a %s.',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'field' => 'is_breed',
|
||||
'label' => '繁育套餐',
|
||||
'rules' => 'trim|min_length[1]|max_length[2]',
|
||||
'errors' => array(
|
||||
'required' => 'You must provide a %s.',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'field' => 'species',
|
||||
'label' => '物种',
|
||||
'rules' => 'trim|required|min_length[1]|max_length[2]',
|
||||
'errors' => array(
|
||||
'required' => 'You must provide a %s.',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'field' => 'package_up',
|
||||
'label' => '升级目标',
|
||||
'rules' => 'trim|min_length[1]|max_length[2]',
|
||||
'errors' => array(
|
||||
'required' => 'You must provide a %s.',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'field' => 'description',
|
||||
'label' => '描述',
|
||||
'rules' => 'trim|min_length[0]|max_length[20]'
|
||||
),
|
||||
array(
|
||||
'field' => 'price',
|
||||
'label' => '价格',
|
||||
'rules' => 'trim|min_length[0]|max_length[20]'
|
||||
)
|
||||
);
|
||||
//涉及字段序列化的特殊操作
|
||||
$data = $this->json_validation($config);
|
||||
//处理content字段
|
||||
if(!is_array($data['content'])){
|
||||
$this->error('套餐内容需要传入数组');
|
||||
}
|
||||
if(!empty($data['content']))foreach ($data['content'] as $item) {
|
||||
if(empty($item['name'])){
|
||||
$this->error('套餐选项名称不能为空');
|
||||
}
|
||||
if(empty($item['disease_id'])){
|
||||
$this->error('套餐疾病选项不能为空');
|
||||
}
|
||||
}
|
||||
$data['content'] = json_encode($data['content']);
|
||||
$data['structure'] = implode(',',$data['structure']);
|
||||
$data['structure_hide'] = implode(',',$data['structure_hide']);
|
||||
$data['cert'] = implode(',',$data['cert']);
|
||||
$data['disease'] = implode(',',$data['disease']);
|
||||
$fields = array_column($config,'field');
|
||||
$fields[] = 'content';
|
||||
$fields[] = 'structure';
|
||||
$fields[] = 'structure_hide';
|
||||
$fields[] = 'cert';
|
||||
$fields[] = 'disease';
|
||||
foreach ($data as $key => $val) {
|
||||
if(!in_array($key,$fields)){
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
//print_r($data);die;
|
||||
if(empty($data['id'])){
|
||||
$ret = $this->{$this->base_model}->add($data);
|
||||
if($ret === -1){
|
||||
$this->error('名称不能与已有的重复');
|
||||
}
|
||||
}else{
|
||||
$this->{$this->base_model}->update($data, ['id'=>$data['id']]);
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user