178 lines
5.9 KiB
PHP
178 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace app\KitchenScale\controller\admin;
|
|
|
|
use think\Db;
|
|
|
|
class Cookbook extends Base{
|
|
|
|
// protected $login_hours_out = 24;
|
|
|
|
protected $page_num = 15;
|
|
|
|
// 加 bcadd(,,20)
|
|
// 减 bcsub(,,20)
|
|
// 乘 bcmul(,,20)
|
|
// 除 bcdiv(,,20)
|
|
################################################################接口################################################################
|
|
################################################################接口################################################################
|
|
################################################################接口################################################################
|
|
|
|
// 登录
|
|
public function index($page = 1){
|
|
$data = input();
|
|
$pd = true;
|
|
$parameter = [];
|
|
// $parameter['is_del'] = 0;
|
|
if(array_key_exists('tt', $data)){
|
|
$page = $data['page_num'];
|
|
unset($data['page_num']);
|
|
unset($data['tt']);
|
|
$pd = false;
|
|
// if($data['status_num'] === "0" || $data['status_num'] === "1"){
|
|
// $parameter['is_del'] = $data['status_num'];
|
|
// }
|
|
|
|
// if($data['tel']){
|
|
// $parameter['tel'] = $data['tel'];
|
|
// }
|
|
// if($data['email']){
|
|
// $parameter['email'] = $data['email'];
|
|
// }
|
|
// if($data['s_time']){
|
|
// $parameter['create_time'] = ['>=',$data['s_time']];
|
|
// }
|
|
// if($data['e_time']){
|
|
// $parameter['create_time'] = ['<=',$data['e_time']];
|
|
// }
|
|
}
|
|
$where = '1=1';
|
|
$cfc = Db::connect('cfc_db');
|
|
$num = $cfc->table('app_user_cookbook')->where($parameter)->count();
|
|
$sql = "
|
|
SELECT
|
|
a.id,
|
|
a.title,
|
|
a.create_user_nickname,
|
|
a.likes_num,
|
|
a.read_it,
|
|
a.is_del,
|
|
a.create_time,
|
|
b.pic_url
|
|
FROM
|
|
app_user_cookbook a
|
|
LEFT JOIN
|
|
app_user_upload_img b ON a.cover = b.id
|
|
WHERE
|
|
$where
|
|
ORDER BY
|
|
a.id DESC
|
|
OFFSET (".($page - 1)." * ".$this->page_num.") ROWS
|
|
FETCH NEXT ".$this->page_num." ROWS ONLY;
|
|
";
|
|
$result = $cfc->query($sql);
|
|
|
|
if(!$pd){
|
|
$return_data['num'] = $num;
|
|
$return_data['data'] = $result;
|
|
return $this->msg($return_data);
|
|
}
|
|
$this->assign([
|
|
'result' => $result,
|
|
'num' => $num,
|
|
]);
|
|
return $this->fetch();
|
|
|
|
}
|
|
|
|
public function add_cookbook(){
|
|
$cfc = Db::connect('cfc_db');
|
|
$result = $cfc->table('app_user_cookbook_label')->where(['is_del'=>0])->select();
|
|
$this->assign([
|
|
'result' => $result,
|
|
]);
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function add_cookbook_action(){
|
|
$data = input();
|
|
$cfc = Db::connect('cfc_db');
|
|
// dump($data);
|
|
// 处理食谱数据
|
|
$id_list = [];
|
|
$food_cookbook_relationship = [];
|
|
foreach ($data['foodList'] as $key => $value) {
|
|
if(!in_array($value['id'],$id_list)){
|
|
array_push($id_list,$value['id']);
|
|
array_push($food_cookbook_relationship,[
|
|
'cookbook_id' => '',
|
|
'food_id' => $value['id']
|
|
]);
|
|
}
|
|
}
|
|
$kcal_data = $cfc->table('app_z_national_standard_food_type_3')->where("id in (".implode(',',$id_list).")")->field('id,Calorie_val')->select();
|
|
for ($i=0; $i < count($kcal_data); $i++) {
|
|
$id_list[$kcal_data[$i]['id']] = $kcal_data[$i]['Calorie_val'];
|
|
}
|
|
foreach ($data['foodList'] as $key => $value) {
|
|
$data['foodList'][$key]['kcal'] = bcmul(bcdiv($value['weight'],100,20),$id_list[$value['id']],2);
|
|
$data['foodList'][$key]['unit'] = 'g';
|
|
}
|
|
|
|
// 设置食谱信息
|
|
$cookbook_data = [
|
|
'title'=>$data['title'],
|
|
'cover'=>$data['cover'],
|
|
'create_user_token'=>'caadd1be045a65f30b92aa805f1de54a',
|
|
'create_user_head_pic'=>'https://tc.pcxbc.com/tsf/head_pic.png',
|
|
'create_user_nickname'=>'clown',
|
|
'describe_data'=>$data['description'],
|
|
'food_data'=>json_encode($data['foodList']),
|
|
'step_data'=>json_encode($data['stepList']),
|
|
'create_time'=>date('Y-m-d H:i:s'),
|
|
'original_cookbook_id'=>'admin',
|
|
'cook_label'=>$data['cook_label'],
|
|
'is_del'=>1,
|
|
|
|
];
|
|
|
|
$cfc->startTrans();
|
|
try {
|
|
|
|
$cookbook_id = $cfc->table('app_user_cookbook')->insertGetId($cookbook_data);
|
|
for ($i=0; $i < count($food_cookbook_relationship); $i++) {
|
|
$food_cookbook_relationship[$i]['cookbook_id'] = $cookbook_id;
|
|
}
|
|
$cfc->table('app_user_cookbook_food_relation')->insertAll($food_cookbook_relationship);
|
|
$cfc->commit();
|
|
return $this->msg([]);
|
|
} catch (\Exception $e) {
|
|
$cfc->rollback();
|
|
return $this->msg(10002,'数据保存失败,'.$e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
public function stop_and_run(){
|
|
$data = input();
|
|
$cfc = Db::connect('cfc_db');
|
|
$result = $cfc->table('app_user_cookbook')->where(['id'=>$data['id']])->update(['is_del'=>$data['status']]);
|
|
if($result){
|
|
return $this->msg([]);
|
|
}else{
|
|
return $this->msg(10002);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function find_food_list(){
|
|
$data = input();
|
|
$cfc = Db::connect('cfc_db');
|
|
$result = $cfc->table('app_z_national_standard_food_type_3')->where("food_name like '%". $data['search_data'] ."%'")->field('id,food_name,Calorie_val')->select();
|
|
return $this->msg(0,'success',$result);
|
|
}
|
|
|
|
|
|
} |