0
0

DinnerGroup.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\QrcodeUtil;
  4. use think\Db;
  5. use think\Exception;
  6. class DinnerGroup extends Auth
  7. {
  8. public function index(){
  9. if(request()->isAjax()){
  10. //分页参数
  11. $length = input('rows',10,'intval'); //每页条数
  12. $page = input('page',1,'intval'); //第几页
  13. $start = ($page - 1) * $length; //分页开始位置
  14. //排序
  15. $sortRow = input('sidx','id','trim'); //排序列
  16. $sort = input('sord','desc','trim'); //排序方式
  17. $order = $sortRow.' '.$sort;
  18. $title = input('title','','trim');
  19. if($title){
  20. $map[] = ['title','like','%'.$title.'%'];
  21. }
  22. $enable = input('enable','','trim');
  23. if($enable != ''){
  24. $map[] = ['enable','=',$enable];
  25. }
  26. $map[] = ['del','=',0];
  27. $map[] = ['org_id','=',$this->orgId];
  28. $map= empty($map) ? true: $map;
  29. //数据查询
  30. $lists = Db::name('dinner_group')->where($map)->limit($start,$length)->order($order)->select();
  31. //数据返回
  32. $totalCount = Db::name('dinner_group')->where($map)->count();
  33. $totalPage = ceil($totalCount/$length);
  34. $result['page'] = $page;
  35. $result['total'] = $totalPage;
  36. $result['records'] = $totalCount;
  37. $result['rows'] = $lists;
  38. return json($result);
  39. }else{
  40. return $this->fetch();
  41. }
  42. }
  43. /**
  44. * 新增/编辑
  45. */
  46. public function add($id=0){
  47. if(request()->isPost()){
  48. $res = model('DinnerGroup')->updates();
  49. if($res){
  50. $this->success('操作成功',url('index'));
  51. }else{
  52. $this->error(model('DinnerGroup')->getError());
  53. }
  54. }else{
  55. if($id){
  56. $info = Db::name('dinner_group')->where('id',$id)->find();
  57. $this->assign('info',$info);
  58. }
  59. $cur = get_week_days(0);
  60. $curnext = get_week_days(1);
  61. $weeks = [
  62. ["id"=>0,"name"=>"本周({$cur[0]} ~ {$cur[1]})"],
  63. ["id"=>1,"name"=>"下周({$curnext[0]} ~ {$curnext[1]})"]
  64. ];
  65. $this->assign('weeks',$weeks);
  66. return $this->fetch();
  67. }
  68. }
  69. /**
  70. * 删除记录
  71. * @param int $id
  72. */
  73. public function del($id=0){
  74. if(!$id){
  75. $this->error('参数错误');
  76. }
  77. $res = Db::name('dinner_group')->where('id',$id)->setField('del',1);
  78. if($res){
  79. $this->success('删除成功');
  80. }else{
  81. $this->error('删除失败');
  82. }
  83. }
  84. /**
  85. * 改变字段值
  86. * @param int $fv
  87. * @param string $fn
  88. * @param int $fv
  89. */
  90. public function changeField($id=0,$fn='',$fv=0){
  91. if(!$fn||!$id){
  92. $this->error('参数错误');
  93. }
  94. $res = Db::name('dinner_group')->where('id',$id)->setField($fn,$fv);
  95. if($res){
  96. $name = '套餐:';
  97. $content = '';
  98. if($fn == 'enable'){
  99. $content = $name.'禁用';
  100. if($fv == 1){
  101. $content = $name.'启用';
  102. }
  103. }
  104. $this->success('操作成功');
  105. }else{
  106. $this->error('操作失败');
  107. }
  108. }
  109. public function setting($id=0,$day=''){
  110. $info = Db::name('dinner_group')->where('id',$id)->find();
  111. if(!$info){
  112. $this->error('套餐不存在');
  113. }
  114. $day = $day?$day:$info['start'];
  115. $this->assign('day',$day);
  116. $weeks = [];
  117. $stime = $info['start'];
  118. while (true){
  119. if($stime > $info['end']){
  120. break;
  121. }
  122. $weeks[] = [
  123. 'day' => $stime,
  124. 'week' => get_week_txt($stime),
  125. ];
  126. $stime = date('Y-m-d',strtotime($stime) + 24*60*60);
  127. }
  128. $this->assign('weeks',$weeks);
  129. $typeList = model('DinnerType')->getList($this->orgId);
  130. foreach ($typeList as $k=>$v){
  131. $limit = Db::name('dinner_group_type')
  132. ->where('group_id',$id)
  133. ->where('day',$day)
  134. ->where('dinner_type_id',$v['id'])
  135. ->value('limit');
  136. $typeList[$k]['limit'] = $limit?$limit:0;
  137. $items = Db::name('dinner_group_item')
  138. ->alias('a')
  139. ->join('dinner b','a.dinner_id = b.id')
  140. ->where('a.group_id',$id)
  141. ->where('a.day',$day)
  142. ->where('b.dinner_type_id',$v['id'])
  143. ->field('a.id,a.money,a.dinner_id,b.name,b.money as oldmoney,b.imgs,a.required,a.max')
  144. ->order('b.sort asc, a.id desc')
  145. ->select();
  146. foreach ($items as $kk=>$vv){
  147. if($vv['money'] == 0 && $vv['oldmoney'] > 0){
  148. $items[$kk]['money'] = $vv['oldmoney'];
  149. }
  150. $imgs = $vv['imgs']?explode(',',$vv['imgs']):[];
  151. $items[$kk]['img'] = $imgs?$imgs[0]:'';
  152. }
  153. $typeList[$k]['items'] = $items?$items:[];
  154. // 组合
  155. $combination = Db::name('dinner_group_combination')
  156. ->where('group_id',$info['id'])
  157. ->where('dinner_type_id',$v['id'])
  158. ->where('day',$day)
  159. ->select();
  160. foreach ($combination as $kk=>$vv){
  161. $items = explode(',',$vv['items']);
  162. $names = Db::name('dinner')
  163. ->alias('a')
  164. ->join('dinner_group_item b','a.id = b.dinner_id')
  165. ->where('b.id','in',$items)
  166. ->column('name');
  167. $combination[$kk]['names'] = $names?implode(',',$names):'';
  168. }
  169. $typeList[$k]['combination'] = $combination?$combination:[];
  170. // 互斥组合
  171. $combination2 = Db::name('dinner_group_combination2')
  172. ->where('group_id',$info['id'])
  173. ->where('dinner_type_id',$v['id'])
  174. ->where('day',$day)
  175. ->select();
  176. foreach ($combination2 as $kk=>$vv){
  177. $items = explode(',',$vv['items']);
  178. $names = Db::name('dinner')
  179. ->alias('a')
  180. ->join('dinner_group_item b','a.id = b.dinner_id')
  181. ->where('b.id','in',$items)
  182. ->column('name');
  183. $combination2[$kk]['names'] = $names?implode(',',$names):'';
  184. }
  185. $typeList[$k]['combination2'] = $combination2?$combination2:[];
  186. }
  187. // halt($typeList);
  188. $this->assign('typeList',$typeList);
  189. $this->assign('info',$info);
  190. return $this->fetch();
  191. }
  192. // 选择种类限制
  193. public function limits(){
  194. $groupId = input('id/d',0);
  195. $day = input('day','','trim');
  196. $typeId = input('typeId/d',0);
  197. $info = Db::name('dinner_group_type')
  198. ->where('group_id',$groupId)
  199. ->where('day',$day)
  200. ->where('dinner_type_id',$typeId)
  201. ->find();
  202. if(request()->isPost()){
  203. $limit = input('limit/d',0);
  204. if($limit < 0){
  205. $this->error('限制数不能小于0');
  206. }
  207. if($info){
  208. if($info['limit'] == $limit){
  209. $ret = true;
  210. }else{
  211. $ret = Db::name('dinner_group_type')->where('id',$info['id'])->update(['limit'=>$limit]);
  212. }
  213. }else{
  214. $ret = Db::name('dinner_group_type')->insert([
  215. 'group_id' => $groupId,
  216. 'day' => $day,
  217. 'dinner_type_id' => $typeId,
  218. 'limit' => $limit
  219. ]);
  220. }
  221. if($ret){
  222. $this->success('操作成功');
  223. }else{
  224. $this->error('操作失败');
  225. }
  226. }else{
  227. $this->assign('id',$groupId);
  228. $this->assign('day',$day);
  229. $this->assign('typeId',$typeId);
  230. $limit = $info?$info['limit']:0;
  231. $this->assign('limit',$limit);
  232. return $this->fetch();
  233. }
  234. }
  235. public function items($id=0,$typeId=0,$day=''){
  236. if(request()->isAjax()){
  237. //分页参数
  238. $length = input('rows',10,'intval'); //每页条数
  239. $page = input('page',1,'intval'); //第几页
  240. $start = ($page - 1) * $length; //分页开始位置
  241. //排序
  242. $sortRow = input('sidx','id','trim'); //排序列
  243. $sort = input('sord','desc','trim'); //排序方式
  244. $order = $sortRow.' '.$sort;
  245. $title = input('title','','trim');
  246. if($title){
  247. $map[] = ['name','like','%'.$title.'%'];
  248. }
  249. $dids = Db::name('dinner_group_item')->where('group_id',$id)->where('day',$day)->column('dinner_id');
  250. if($dids){
  251. $map[] = ['id','not in',$dids];
  252. }
  253. $map[] = ['cate','=',1];
  254. $map[] = ['dinner_type_id','=',$typeId];
  255. $map[] = ['enable','=',1];
  256. $map[] = ['org_id','=',$this->orgId];
  257. $map= empty($map) ? true: $map;
  258. //数据查询
  259. $lists = Db::name('dinner')->where($map)->limit($start,$length)->order($order)->select();
  260. foreach ($lists as $k=>$v){
  261. $img = $v['imgs']?explode(',',$v['imgs']):[];
  262. $lists[$k]['img'] = $img?$img[0]:'';
  263. }
  264. //数据返回
  265. $totalCount = Db::name('dinner')->where($map)->count();
  266. $totalPage = ceil($totalCount/$length);
  267. $result['page'] = $page;
  268. $result['total'] = $totalPage;
  269. $result['records'] = $totalCount;
  270. $result['rows'] = $lists;
  271. return json($result);
  272. }else{
  273. $this->assign('id',$id);
  274. $this->assign('day',$day);
  275. $this->assign('typeId',$typeId);
  276. return $this->fetch();
  277. }
  278. }
  279. public function itemsave(){
  280. $groupId = input('groupId/d',0);
  281. $day = input('day','','trim');
  282. $id = input('id/d',0);
  283. if($groupId<=0 || !$day || $id < 0){
  284. $this->error('参数错误');
  285. }
  286. $dinner = Db::name('dinner')->where('id',$id)->find();
  287. if(!$dinner){
  288. $this->error('菜品不存在');
  289. }
  290. $info = Db::name('dinner_group_item')->where('dinner_id',$id)->where('day',$day)->find();
  291. if($info){
  292. $this->error('已添加,无重复');
  293. }
  294. $data = [
  295. 'group_id' => $groupId,
  296. 'day' => $day,
  297. 'dinner_id' => $id,
  298. 'required' => 0,
  299. 'money' => $dinner['money']
  300. ];
  301. $ret = Db::name('dinner_group_item')->insert($data);
  302. if($ret){
  303. $this->success('操作成功');
  304. }else{
  305. $this->success('操作失败');
  306. }
  307. }
  308. public function itemdel(){
  309. $id = input('id/d',0);
  310. if($id < 0){
  311. $this->error('参数错误');
  312. }
  313. $info = Db::name('dinner_group_item')->where('id',$id)->find();
  314. if(!$info){
  315. $this->error('记录不存在');
  316. }
  317. Db::startTrans();
  318. try{
  319. $ret = Db::name('dinner_group_item')->delete($id);
  320. if(!$ret){
  321. \exception('操作失败2');
  322. }
  323. // 消除组合里的商品
  324. $map[]=['','exp',Db::raw("FIND_IN_SET({$id},items)")];
  325. $lists = Db::name('dinner_group_combination')->where($map)->select();
  326. $lists = $lists?$lists:[];
  327. foreach ($lists as $k=>$v){
  328. $items = explode(',',$v['items']);
  329. $items = array_diff($items,[$id]);
  330. sort($items);
  331. if(count($items) <= 1){
  332. $ret = Db::name('dinner_group_combination')->delete($v['id']);
  333. }else{
  334. $ret = Db::name('dinner_group_combination')
  335. ->where('id',$v['id'])
  336. ->update(['items'=>implode(',',$items)]);
  337. }
  338. if(!$ret){
  339. \exception('操作失败1');
  340. }
  341. }
  342. // 消除互斥组合里的商品
  343. $map2[]=['','exp',Db::raw("FIND_IN_SET({$id},items)")];
  344. $lists = Db::name('dinner_group_combination2')->where($map2)->select();
  345. $lists = $lists?$lists:[];
  346. foreach ($lists as $k=>$v){
  347. $items = explode(',',$v['items']);
  348. $items = array_diff($items,[$id]);
  349. sort($items);
  350. if(count($items) <= 1){
  351. $ret = Db::name('dinner_group_combination2')->delete($v['id']);
  352. }else{
  353. $ret = Db::name('dinner_group_combination2')
  354. ->where('id',$v['id'])
  355. ->update(['items'=>implode(',',$items)]);
  356. }
  357. if(!$ret){
  358. \exception('操作失败1');
  359. }
  360. }
  361. Db::commit();
  362. }catch (Exception $e){
  363. Db::rollback();
  364. $this->error('操作失败'.$e->getMessage());
  365. }
  366. $this->success('操作成功');
  367. }
  368. public function itemcombination(){
  369. $id = input('id/d',0);
  370. if($id < 0){
  371. $this->error('参数错误');
  372. }
  373. $info = Db::name('dinner_group_combination')->where('id',$id)->find();
  374. if(!$info){
  375. $this->error('记录不存在');
  376. }
  377. $ret = Db::name('dinner_group_combination')->delete($id);
  378. if($ret){
  379. $this->success('操作成功');
  380. }else{
  381. $this->success('操作失败');
  382. }
  383. }
  384. public function itemrequired(){
  385. $id = input('id/d',0);
  386. if($id < 0){
  387. $this->error('参数错误');
  388. }
  389. $info = Db::name('dinner_group_item')->where('id',$id)->find();
  390. if(!$info){
  391. $this->error('记录不存在');
  392. }
  393. $ret = Db::name('dinner_group_item')->where('id',$id)->update(['required' => $info['required']==1?0:1]);
  394. if($ret){
  395. $this->success('操作成功');
  396. }else{
  397. $this->success('操作失败');
  398. }
  399. }
  400. public function itemmax(){
  401. $id = input('id/d',0);
  402. $val = input('val/d',0);
  403. if($id < 0 || $val < 0){
  404. $this->error('参数错误');
  405. }
  406. $info = Db::name('dinner_group_item')->where('id',$id)->find();
  407. if(!$info){
  408. $this->error('记录不存在');
  409. }
  410. $ret = Db::name('dinner_group_item')->where('id',$id)->update(['max' => $val]);
  411. if($ret){
  412. $info['val'] = $val;
  413. $this->success('操作成功');
  414. }else{
  415. $this->success('操作失败');
  416. }
  417. }
  418. public function itemmoney(){
  419. $id = input('id/d',0);
  420. $val = input('val/f',0);
  421. if($id < 0 || $val < 0){
  422. $this->error('参数错误');
  423. }
  424. $info = Db::name('dinner_group_item')->where('id',$id)->find();
  425. if(!$info){
  426. $this->error('记录不存在');
  427. }
  428. $ret = Db::name('dinner_group_item')->where('id',$id)->update(['money' => $val]);
  429. if($ret){
  430. $info['money'] = $val;
  431. $this->success('操作成功');
  432. }else{
  433. $this->success('操作失败');
  434. }
  435. }
  436. public function combination(){
  437. if(request()->isPost()){
  438. $data = [
  439. 'title' => input('title','','trim'),
  440. 'group_id' => input('group_id/d',0),
  441. 'day' => input('day','','trim'),
  442. 'dinner_type_id' => input('dinner_type_id/d',0),
  443. 'items' => input('items','','trim')
  444. ];
  445. if(!$data['title']){
  446. $this->error('请输入名称');
  447. }
  448. if(!$data['items'] && !format_str($data['items'])){
  449. $this->error('请选择菜品');
  450. }
  451. $items = explode(',',format_str($data['items']));
  452. sort($items);
  453. if(count($items) <= 1){
  454. $this->error('组合至少要选两种菜品');
  455. }
  456. $data['items'] = implode(',',$items);
  457. $ret = Db::name('dinner_group_combination')
  458. ->where('group_id',$data['group_id'])
  459. ->where('day',$data['day'])
  460. ->where('dinner_type_id',$data['dinner_type_id'])
  461. ->column('items');
  462. $ret = $ret?$ret:[];
  463. $aitems = [];
  464. foreach ($ret as $k=>$v){
  465. $aitems = array_merge($aitems,explode(',',$v));
  466. }
  467. if(array_intersect($items,$aitems)){
  468. $this->error('选择的部分商品已在其他组合中');
  469. }
  470. $ret = Db::name('dinner_group_combination')->insert($data);
  471. if(!$ret){
  472. $this->error('操作失败');
  473. }
  474. $this->success('操作成功');
  475. }else{
  476. $groupId = input('groupId/d',0);
  477. $typeId = input('typeId/d',0);
  478. $day = input('day','','trim');
  479. $dinners = Db::name('dinner_group_item')
  480. ->alias('a')
  481. ->join('dinner b','a.dinner_id = b.id')
  482. ->where('a.group_id',$groupId)
  483. ->where('a.day',$day)
  484. ->where('b.dinner_type_id',$typeId)
  485. ->field('a.id,b.name as title')
  486. ->select();
  487. $this->assign('groupId',$groupId);
  488. $this->assign('typeId',$typeId);
  489. $this->assign('day',$day);
  490. $this->assign('dinners',$dinners);
  491. return $this->fetch();
  492. }
  493. }
  494. public function combinationdel(){
  495. $id = input('id/d',0);
  496. if($id < 0){
  497. $this->error('参数错误');
  498. }
  499. $info = Db::name('dinner_group_combination')->where('id',$id)->find();
  500. if(!$info){
  501. $this->error('记录不存在');
  502. }
  503. Db::startTrans();
  504. try{
  505. $ret = Db::name('dinner_group_combination')->delete($id);
  506. if(!$ret){
  507. \exception('操作失败');
  508. }
  509. Db::commit();
  510. }catch (Exception $e){
  511. Db::rollback();
  512. $this->error('操作失败'.$e->getMessage());
  513. }
  514. $this->success('操作成功');
  515. }
  516. public function combination2(){
  517. if(request()->isPost()){
  518. $data = [
  519. 'group_id' => input('group_id/d',0),
  520. 'day' => input('day','','trim'),
  521. 'dinner_type_id' => input('dinner_type_id/d',0),
  522. 'items' => input('items','','trim')
  523. ];
  524. if(!$data['items'] && !format_str($data['items'])){
  525. $this->error('请选择菜品');
  526. }
  527. $items = explode(',',format_str($data['items']));
  528. sort($items);
  529. if(count($items) <= 1){
  530. $this->error('组合至少要选两种菜品');
  531. }
  532. $data['items'] = implode(',',$items);
  533. // $ret = Db::name('dinner_group_combination2')
  534. // ->where('group_id',$data['group_id'])
  535. // ->where('day',$data['day'])
  536. // ->where('dinner_type_id',$data['dinner_type_id'])
  537. // ->column('items');
  538. // $ret = $ret?$ret:[];
  539. // $aitems = [];
  540. // foreach ($ret as $k=>$v){
  541. // $aitems = array_merge($aitems,explode(',',$v));
  542. // }
  543. // if(array_intersect($items,$aitems)){
  544. // $this->error('选择的部分商品已在其他组合中');
  545. // }
  546. $ret = Db::name('dinner_group_combination2')->insert($data);
  547. if(!$ret){
  548. $this->error('操作失败');
  549. }
  550. $this->success('操作成功');
  551. }else{
  552. $groupId = input('groupId/d',0);
  553. $typeId = input('typeId/d',0);
  554. $day = input('day','','trim');
  555. $dinners = Db::name('dinner_group_item')
  556. ->alias('a')
  557. ->join('dinner b','a.dinner_id = b.id')
  558. ->where('a.group_id',$groupId)
  559. ->where('a.day',$day)
  560. ->where('b.dinner_type_id',$typeId)
  561. ->field('a.id,b.name as title')
  562. ->select();
  563. $this->assign('groupId',$groupId);
  564. $this->assign('typeId',$typeId);
  565. $this->assign('day',$day);
  566. $this->assign('dinners',$dinners);
  567. return $this->fetch();
  568. }
  569. }
  570. public function combination2del(){
  571. $id = input('id/d',0);
  572. if($id < 0){
  573. $this->error('参数错误');
  574. }
  575. $info = Db::name('dinner_group_combination2')->where('id',$id)->find();
  576. if(!$info){
  577. $this->error('记录不存在');
  578. }
  579. Db::startTrans();
  580. try{
  581. $ret = Db::name('dinner_group_combination2')->delete($id);
  582. if(!$ret){
  583. \exception('操作失败');
  584. }
  585. Db::commit();
  586. }catch (Exception $e){
  587. Db::rollback();
  588. $this->error('操作失败'.$e->getMessage());
  589. }
  590. $this->success('操作成功');
  591. }
  592. }