WxBookDinner.php 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. <?php
  2. namespace app\h5\controller;
  3. use app\hander\HelpHander;
  4. use EasyWeChat\Factory;
  5. use think\App;
  6. use think\Controller;
  7. use think\Db;
  8. use think\Exception;
  9. class WxBookDinner extends Controller
  10. {
  11. protected $user = [];
  12. protected $orgId = 0;
  13. protected $addressId = 0;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $userId = dinner_is_login();
  18. // $userId = 205;
  19. if(empty($userId)){
  20. $this->redirect(url('WxHome/login'));
  21. }
  22. $sn = decodecookie('sn');
  23. $orgId = decodecookie('HospitalId');
  24. if(!$sn||!$orgId){
  25. $this->redirect(url('WxHome/login'));
  26. }
  27. $data = get_qrcode_arr($sn);
  28. if(!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address'){
  29. $this->redirect(url('WxHome/index'));
  30. }
  31. $this->addressId = $data['id'];
  32. $orgName = Db::name('org')->where('id',$orgId)->cache(3600)->value('name');
  33. $this->assign('orgName',$orgName);
  34. $user = Db::name('wxuser')->where('id',$userId)->where('del',0)->find();
  35. if(!$user){
  36. $this->redirect(url('WxHome/login'));
  37. }
  38. if($user['curr_org_id'] != $orgId){
  39. Db::name('wxuser')->where('id',$user['id'])->update(['curr_org_id'=>$orgId]);
  40. }
  41. $user['isdinder'] = decodecookie('isdinder') == 1?1:0;
  42. $user['HospitalName'] = $orgName;
  43. $this->orgId = $orgId;
  44. $this->user = $user;
  45. }
  46. public function index(){
  47. $is_spot = decodecookie('is_spot');
  48. $is_spot = $is_spot==2?2:1; // 默认1=当日餐 2=预定餐
  49. $dinner_type_id = input('dinner_type_id/d',0);
  50. // 判断是否是点餐员
  51. $bDinder = isset($this->user['isdinder'])?$this->user['isdinder']:false;
  52. if ($bDinder||$is_spot==1) {
  53. if($bDinder){
  54. encodecookie('is_spot',1);
  55. }
  56. $is_spot = 1;
  57. // 显示当天的
  58. $is_today = 1;
  59. } else {
  60. $is_today = 0;
  61. }
  62. $cartCount = $this->cartCount();
  63. $this->assign('cartCount',$cartCount);
  64. $this->assign('user',$this->user);
  65. $this->assign('is_spot',$is_spot);
  66. $this->assign('is_today',$is_today);
  67. $this->assign('bDinder',$bDinder);
  68. $typeList = model('DinnerType')->getList($this->orgId);
  69. if(!$dinner_type_id){
  70. if(isset($typeList[0])){
  71. $dinner_type_id=$typeList[0]['id'];
  72. }
  73. }
  74. $this->assign('dinner_type_id',$dinner_type_id);
  75. $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find();
  76. $this->assign('content1',$config?$config['content1']:'');
  77. $this->assign('content2',$config?$config['content2']:'');
  78. $this->assign('typeList',$typeList);
  79. $special = Db::name('dinner_config')->where('org_id',$this->orgId)->value('special');
  80. $special = $special==1?$special:0;
  81. if($special){
  82. $isSpecial = $this->checkSpecial();
  83. $this->assign('isSpecial',$isSpecial);
  84. }
  85. $this->assign('special',$special);
  86. return $this->fetch();
  87. }
  88. // 点餐页
  89. public function tc(){
  90. if(!$this->checkSpecial()){
  91. $this->error("只有特殊人群才可进入");
  92. }
  93. $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周
  94. $this->assign('week',$week);
  95. $curday = date('Y-m-d');
  96. $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订
  97. // 当天12点之前可以定明天的餐,12点之后可以定后台的餐
  98. $hour = intval(date('H'));
  99. if($hour < 12){
  100. $curday = date('Y-m-d');
  101. }else{
  102. $curday = date('Y-m-d');
  103. $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订
  104. }
  105. $cur = get_week_days($week);
  106. // 获取当前周已经定过餐的日期
  107. $days1 = Db::name('dinner_order_refuse_item')
  108. ->alias('a')
  109. ->join('dinner_order_refuse c','c.id = a.order_refuse_id')
  110. ->join('dinner_order b','c.dinner_order_id = b.id')
  111. ->where('b.cate',1)
  112. ->where('c.status',4)
  113. ->where('a.day','>=',$cur[0])
  114. ->where('a.day','<=',$cur[1])
  115. ->where('b.user_id',$this->user['id'])
  116. ->where('b.org_id',$this->orgId)
  117. ->group('a.day')
  118. ->distinct(true)
  119. ->column('a.day');
  120. $days1 = $days1?$days1:[];
  121. $seldays = Db::name('dinner_order_item')
  122. ->alias('a')
  123. ->join('dinner_order b','a.order_id = b.id')
  124. ->where('b.cate',1)
  125. ->where('a.is_refuse',0)
  126. ->where('a.day','>=',$cur[0])
  127. ->where('a.day','<=',$cur[1])
  128. ->where('b.user_id',$this->user['id'])
  129. ->where('b.org_id',$this->orgId)
  130. ->group('a.day')
  131. ->distinct(true)
  132. ->column('a.day');
  133. $seldays = $seldays?$seldays:[];
  134. $seldays = array_unique(array_merge($seldays,$days1));
  135. // 获取当前周已选择的套餐
  136. $groupId = Db::name('dinner_order_item')
  137. ->alias('a')
  138. ->join('dinner_order b','a.order_id = b.id')
  139. ->where('b.cate',1)
  140. ->where('a.day','>=',$cur[0])
  141. ->where('a.day','<=',$cur[1])
  142. ->where('b.user_id',$this->user['id'])
  143. ->where('b.org_id',$this->orgId)
  144. ->value('a.group_id');
  145. if($groupId > 0){
  146. $map[] = ['id','=',$groupId];
  147. }
  148. $map[] = ['start','=',$cur[0]];
  149. $map[] = ['del','=',0];
  150. $map[] = ['enable','=',1];
  151. $map[] = ['org_id','=',$this->orgId];
  152. $groups = Db::name('dinner_group')
  153. ->where($map)
  154. ->field('id,title,start,end')
  155. ->select();
  156. $groups = $groups?$groups:[];
  157. $days = [];
  158. $sday = $cur[0];
  159. while (true){
  160. if($sday > $cur[1]){
  161. break;
  162. }
  163. if($curday < $sday){
  164. if(!$seldays || ($seldays && !in_array($sday,$seldays))){
  165. $days[] = [
  166. 'count' => 0,
  167. 'money' => 0,
  168. 'day' => $sday,
  169. 'text' => get_week_txt($sday),
  170. ];
  171. }
  172. }
  173. $sday = date('Y-m-d',strtotime($sday) + 24*60*60);
  174. }
  175. $typeList = model('DinnerType')->getList($this->orgId);
  176. $typeList = $typeList?$typeList:[];
  177. foreach ($groups as $gk=>$gv){
  178. $combination = Db::name('dinner_group_combination')
  179. ->where('group_id',$gv['id'])
  180. ->select();
  181. $combination = $combination?$combination:[];
  182. foreach ($combination as $ck=>$cv){
  183. $combination[$ck]['items'] = explode(',',$cv['items']);
  184. }
  185. $groups[$gk]['combination'] = $combination;
  186. $dinners = Db::name('dinner_group_item')
  187. ->alias('a')
  188. ->join('dinner b','b.id = a.dinner_id')
  189. ->where('b.enable',1)
  190. ->where('b.cate',1)
  191. ->where('a.group_id',$gv['id'])
  192. ->field('a.id,a.day,a.group_id,a.max,a.required,a.money,b.name,b.content,b.money as oldmoney,b.imgs,b.dinner_type_id,b.type')
  193. ->order('b.sort asc,a.id desc')
  194. ->select();
  195. $dinners = $dinners?$dinners:[];
  196. foreach ($dinners as $dkk=>$dvv){
  197. if($dvv['money'] == 0 && $dvv['oldmoney'] > 0){
  198. $dinners[$dkk]['money'] = $dvv['oldmoney'];
  199. }
  200. $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[];
  201. $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:'';
  202. $dinners[$dkk]['nums'] = 0;
  203. $dinners[$dkk]['cid'] = 0;
  204. // 组合id
  205. $items = [];
  206. foreach ($combination as $ck=>$cv){
  207. if(in_array($dvv['id'],$cv['items'])){
  208. $items = array_merge($items,$cv['items']);
  209. $dinners[$dkk]['cid'] = $cv['id'];
  210. }
  211. }
  212. if($items){
  213. $items = array_diff(array_unique($items),[$dvv['id']]);
  214. sort($items);
  215. }
  216. $dinners[$dkk]['combination'] = $items;
  217. }
  218. $groups[$gk]['dinners'] = $dinners?$dinners:[];
  219. foreach ($days as $dk=>$dv){
  220. $days[$dk]['group'] = $gv['title'];
  221. $types = [];
  222. foreach ($typeList as $tk=>$tv){
  223. $limit = Db::name('dinner_group_type')
  224. ->where('group_id',$gv['id'])
  225. ->where('dinner_type_id',$tv['id'])
  226. ->where('day',$dv['day'])
  227. ->value('limit');
  228. $coms = [];
  229. foreach ($combination as $ck=>$cv){
  230. if($cv['dinner_type_id'] == $tv['id'] && $cv['day'] == $dv['day']){
  231. $coms[] = $cv;
  232. }
  233. }
  234. $combination2 = Db::name('dinner_group_combination2')
  235. ->where('group_id',$gv['id'])
  236. ->where('dinner_type_id',$tv['id'])
  237. ->where('day',$dv['day'])
  238. ->select();
  239. $combination2 = $combination2?$combination2:[];
  240. $ndinners = [];
  241. foreach ($dinners as $dkk=>$dvv){
  242. if($dvv['day'] == $dv['day'] && $dvv['group_id'] == $gv['id'] && $dvv['dinner_type_id'] == $tv['id']){
  243. $dvv['cid'] = 0;
  244. $dvv['ctitle'] = '';
  245. foreach ($coms as $cck=>$ccv){
  246. if(in_array($dvv['id'],$ccv['items'])){
  247. $dvv['cid'] = $ccv['id'];
  248. $dvv['ctitle'] = $ccv['title'];
  249. break;
  250. }
  251. }
  252. $coms2 = [];
  253. foreach ($combination2 as $ck=>$cv){
  254. $items = explode(',',$cv['items']);
  255. if(in_array($dvv['id'],$items)){
  256. foreach ($items as $ik=>$iv){
  257. if($iv != $dvv['id'] && !in_array($iv,$coms2)){
  258. $coms2[] = $iv;
  259. }
  260. }
  261. }
  262. }
  263. $dvv['combination2'] = $coms2;
  264. $ndinners[] = $dvv;
  265. }
  266. }
  267. $types[] = [
  268. 'id' => $tv['id'],
  269. 'name' => $tv['name'],
  270. 'group_id' => $gv['id'],
  271. 'group_title' => $gv['title'],
  272. 'limit' => $limit > 0?$limit:0,
  273. 'dinners' => $ndinners,
  274. 'combination' => $coms,
  275. ];
  276. }
  277. $days[$dk]['types'] = $types;
  278. }
  279. $groups[$gk]['days'] = $days;
  280. }
  281. // halt($groups);
  282. $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find();
  283. $this->assign('standard',$config['standard']?$config['standard']:0);
  284. $this->assign('standardmin',$config['min']?$config['min']:0);
  285. $this->assign('groups',$groups);
  286. $this->assign('days',$days);
  287. $this->assign('user',$this->user);
  288. return $this->fetch();
  289. }
  290. public function tc备份2(){
  291. if(!$this->checkSpecial()){
  292. $this->error("只有特殊人群才可进入");
  293. }
  294. $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周
  295. $this->assign('week',$week);
  296. $curday = date('Y-m-d');
  297. $cur = get_week_days($week);
  298. // 获取当前周已经定过餐的日期
  299. $seldays = Db::name('dinner_order_item')
  300. ->alias('a')
  301. ->join('dinner_order b','a.order_id = b.id')
  302. ->where('b.cate',1)
  303. ->where('a.day','>=',$cur[0])
  304. ->where('a.day','<=',$cur[1])
  305. ->where('b.user_id',$this->user['id'])
  306. ->where('b.org_id',$this->orgId)
  307. ->group('a.day')
  308. ->distinct(true)
  309. ->column('a.day');
  310. // 获取当前周已选择的套餐
  311. $groupId = Db::name('dinner_order_item')
  312. ->alias('a')
  313. ->join('dinner_order b','a.order_id = b.id')
  314. ->where('b.cate',1)
  315. ->where('a.day','>=',$cur[0])
  316. ->where('a.day','<=',$cur[1])
  317. ->where('b.user_id',$this->user['id'])
  318. ->where('b.org_id',$this->orgId)
  319. ->value('a.group_id');
  320. if($groupId > 0){
  321. $map[] = ['id','=',$groupId];
  322. }
  323. $map[] = ['start','=',$cur[0]];
  324. $map[] = ['del','=',0];
  325. $map[] = ['enable','=',1];
  326. $map[] = ['org_id','=',$this->orgId];
  327. $groups = Db::name('dinner_group')
  328. ->where($map)
  329. ->field('id,title,start,end')
  330. ->select();
  331. $groups = $groups?$groups:[];
  332. $days = [];
  333. $sday = $cur[0];
  334. while (true){
  335. if($sday > $cur[1]){
  336. break;
  337. }
  338. if($curday < $sday){
  339. if(!$seldays || ($seldays && !in_array($sday,$seldays))){
  340. $days[] = [
  341. 'count' => 0,
  342. 'money' => 0,
  343. 'day' => $sday,
  344. 'text' => get_week_txt($sday),
  345. ];
  346. }
  347. }
  348. $sday = date('Y-m-d',strtotime($sday) + 24*60*60);
  349. }
  350. $typeList = model('DinnerType')->getList($this->orgId);
  351. $typeList = $typeList?$typeList:[];
  352. foreach ($groups as $gk=>$gv){
  353. foreach ($days as $dk=>$dv){
  354. $days[$dk]['group'] = $gv['title'];
  355. $types = [];
  356. foreach ($typeList as $tk=>$tv){
  357. $dinners = Db::name('dinner_group_item')
  358. ->alias('a')
  359. ->join('dinner b','b.id = a.dinner_id')
  360. ->where('b.enable',1)
  361. ->where('b.cate',1)
  362. ->where('b.dinner_type_id',$tv['id'])
  363. ->where('a.group_id',$gv['id'])
  364. ->where('a.day',$dv['day'])
  365. ->field('a.id,a.day,a.group_id,b.name,b.content,b.money,b.imgs,a.required,b.dinner_type_id,b.type')
  366. ->order('b.sort asc,a.id desc')
  367. ->select();
  368. $dinners = $dinners?$dinners:[];
  369. foreach ($dinners as $dkk=>$dvv){
  370. $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[];
  371. $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:'';
  372. $dinners[$dkk]['nums'] = 0;
  373. }
  374. $limit = Db::name('dinner_group_type')
  375. ->where('group_id',$gv['id'])
  376. ->where('dinner_type_id',$tv['id'])
  377. ->where('day',$dv['day'])
  378. ->value('limit');
  379. $types[] = [
  380. 'id' => $tv['id'],
  381. 'name' => $tv['name'],
  382. 'group_id' => $gv['id'],
  383. 'group_title' => $gv['title'],
  384. 'limit' => $limit > 0?$limit:0,
  385. 'dinners' => $dinners,
  386. ];
  387. }
  388. $days[$dk]['types'] = $types;
  389. }
  390. $groups[$gk]['days'] = $days;
  391. }
  392. $this->assign('groups',$groups);
  393. $this->assign('days',$days);
  394. $this->assign('user',$this->user);
  395. return $this->fetch();
  396. }
  397. public function tc备份(){
  398. if(!$this->checkSpecial()){
  399. $this->error("只有特殊人群才可进入");
  400. }
  401. $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周
  402. $this->assign('week',$week);
  403. $curday = date('Y-m-d');
  404. $cur = get_week_days($week);
  405. $weeks = [
  406. ['week'=>$week,'start'=>$cur[0],'end'=>$cur[1],'selected'=>1],
  407. ];
  408. foreach ($weeks as $k=>$v){
  409. $days = [];
  410. $sday = $v['start'];
  411. while (true){
  412. if($sday > $v['end']){
  413. break;
  414. }
  415. if($curday < $sday){
  416. $days[] = [
  417. 'count' => 0,
  418. 'money' => 0,
  419. 'day' => $sday,
  420. 'text' => get_week_txt($sday),
  421. ];
  422. }
  423. $sday = date('Y-m-d',strtotime($sday) + 24*60*60);
  424. }
  425. foreach ($days as $dk=>$dv){
  426. $groups = Db::name('dinner_group')
  427. ->where('org_id',$this->orgId)
  428. ->where('del',0)
  429. ->where('enable',1)
  430. ->where('start',$v['start'])
  431. ->select();
  432. $groups = $groups?$groups:[];
  433. $types = [];
  434. foreach ($groups as $kk=>$vv){
  435. $typeList = model('DinnerType')->getList($this->orgId);
  436. $typeList = $typeList?$typeList:[];
  437. foreach ($typeList as $tk=>$tv){
  438. $dinners = Db::name('dinner_group_item')
  439. ->alias('a')
  440. ->join('dinner b','b.id = a.dinner_id')
  441. ->where('b.enable',1)
  442. ->where('b.cate',1)
  443. ->where('b.dinner_type_id',$tv['id'])
  444. ->where('a.group_id',$vv['id'])
  445. ->where('a.day',$dv['day'])
  446. ->field('a.id,a.day,a.group_id,b.name,b.content,b.money,b.imgs,a.required,b.dinner_type_id')
  447. ->order('b.sort asc,a.id desc')
  448. ->select();
  449. $dinners = $dinners?$dinners:[];
  450. foreach ($dinners as $dkk=>$dvv){
  451. $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[];
  452. $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:'';
  453. // $dinners[$dkk]['selected'] = $dvv['required']?1:0;
  454. }
  455. $limit = Db::name('dinner_group_type')
  456. ->where('group_id',$vv['id'])
  457. ->where('dinner_type_id',$tv['id'])
  458. ->where('day',$dv['day'])
  459. ->value('limit');
  460. $types[] = [
  461. 'id' => $tv['id'],
  462. 'name' => $tv['name'],
  463. 'group_id' => $vv['id'],
  464. 'group_title' => $vv['title'],
  465. 'group_type' => $vv['type'],
  466. 'limit' => $limit > 0?$limit:0,
  467. 'dinners' => $dinners,
  468. ];
  469. }
  470. }
  471. $days[$dk]['types'] = $types;
  472. }
  473. $weeks[$k]['days'] = $days;
  474. }
  475. $this->assign('weeks',$weeks);
  476. $groups = Db::name('dinner_group')
  477. ->where('org_id',$this->orgId)
  478. ->where('del',0)
  479. ->where('enable',1)
  480. ->where('start',$cur[0])
  481. ->select();
  482. $groups = $groups?$groups:[];
  483. $this->assign('groups',$groups);
  484. $this->assign('user',$this->user);
  485. return $this->fetch();
  486. }
  487. // 点套餐商品页
  488. public function group(){
  489. }
  490. public function cartCount(){
  491. $cartCount = Db::name('dinner_cart')
  492. ->where('user_id',$this->user['id'])
  493. ->where('org_id',$this->orgId)
  494. ->sum('count');
  495. return $cartCount;
  496. }
  497. public function indexchange(){
  498. $is_spot = input('spot/d',1);
  499. $is_spot = $is_spot==1?1:2; // 默认1当日餐 2=预定餐
  500. // 清空购物车
  501. Db::name('dinner_cart')->where('user_id',$this->user['id'])->delete();
  502. encodecookie('is_spot',$is_spot);
  503. $this->redirect(url('WxBookDinner/index'));
  504. }
  505. public function get_type_json(){
  506. $size = input('size');
  507. $page = input('page');
  508. $today = input('today');
  509. $btoday = false;
  510. if (!empty($today)&&($today==1)) {
  511. $btoday = true;
  512. }
  513. $DinnerTypeId = input('type');
  514. $ret = $this->show_all_page_by_type($btoday, $DinnerTypeId,$page, $size);
  515. if (empty($ret)) {
  516. header('Content-Type:application/json; charset=utf-8');
  517. exit(json_encode([]));
  518. } else {
  519. header('Content-Type:application/json; charset=utf-8');
  520. exit(json_encode($ret));
  521. }
  522. }
  523. public function show_all_page_by_type($today, $DinnerTypeId, $page, $size){
  524. $query = Db::name('dinner');
  525. $query->where('cate', 0);
  526. if ($DinnerTypeId>0) {
  527. $query->where('dinner_type_id', $DinnerTypeId);
  528. }
  529. if($today){
  530. $query->where('today_enable', 1);
  531. // 限定时间
  532. $currtime = date('H:i');
  533. $query->whereRaw(Db::raw('(today_start_time<="'.$currtime.'" and today_end_time>="'.$currtime.'") or today_start_time= null or today_end_time=null'));
  534. }else{
  535. $query->where('enable', 1);
  536. // 限定时间
  537. $currtime = date('H:i');
  538. $query->whereRaw(Db::raw('(start_time<="'.$currtime.'" and end_time>="'.$currtime.'") or start_time= null or end_time=null'));
  539. }
  540. $query->where('org_id', $this->orgId);
  541. $query->order('sort ASC,id desc');
  542. $query->page($page,$size);
  543. $ret = $query->select();
  544. if (empty($ret)) {
  545. return array();
  546. }
  547. $isSpecial = $this->checkSpecial();
  548. foreach ($ret as $key => $value) {
  549. $img = $value['imgs']?explode(',',$value['imgs']):[];
  550. $ret[$key]['img'] = $img?$img[0]:'';
  551. if($isSpecial){
  552. $money = $this->getDiscountMoney($value);
  553. }else{
  554. $money = $value['money'];
  555. }
  556. $ret[$key]['money'] = sprintf("%.2f", $money);
  557. }
  558. return $ret;
  559. }
  560. public function getDiscountMoney($value){
  561. $money = $value['money'];
  562. if($value['free'] == 1){
  563. $money = 0;
  564. }else{
  565. $money = $money*$value['discount'];
  566. }
  567. return round($money,2);
  568. }
  569. public function addCart(){
  570. $id = input('id/d',0);
  571. if($id <= 0){
  572. HelpHander::error('参数错误');
  573. }
  574. $map[] = ['dinner_id','=',$id];
  575. $map[] = ['org_id','=',$this->orgId];
  576. $map[] = ['user_id','=',$this->user['id']];
  577. $num = input('num/d',1);
  578. $info = Db::name('dinner_cart')->where($map)->find();
  579. if(!empty($info)){
  580. Db::name('dinner_cart')->where($map)->setInc('count',$num);
  581. $count = $num+$info['count'];
  582. }else{
  583. $a = [
  584. 'dinner_id'=>$id,
  585. 'org_id'=>$this->orgId,
  586. 'user_id'=>$this->user['id'],
  587. 'count'=>1,
  588. 'create_time'=>getTime(),
  589. ];
  590. Db::name('dinner_cart')->insertGetId($a);
  591. $count = 1;
  592. }
  593. HelpHander::success(['count'=>$count],'操作成功');
  594. }
  595. public function updateCart(){
  596. $id = input('id/d',0);
  597. if($id <= 0){
  598. HelpHander::error('参数错误');
  599. }
  600. $map[] = ['id','=',$id];
  601. $map[] = ['org_id','=',$this->orgId];
  602. $map[] = ['user_id','=',$this->user['id']];
  603. $num = input('num/d',1);
  604. Db::name('dinner_cart')->where($map)->update([
  605. 'count'=>$num
  606. ]);
  607. Db::name('dinner_cart')->where($map)->where('count',0)->delete();
  608. HelpHander::success(['count'=>$this->cartCount()],'操作成功');
  609. }
  610. public function detail(){
  611. $id = input('id/d',0);
  612. $res = Db::name('dinner')->where('id',$id)->find();
  613. if(!$res){
  614. return $this->fetch('h5/msg_error',['msg'=>'记录不存在']);
  615. }
  616. $res['imgs'] = $res['imgs']?explode(',',$res['imgs']):[];
  617. $isSpecial = $this->checkSpecial();
  618. if($isSpecial){
  619. $res['money'] = $this->getDiscountMoney($res);
  620. }
  621. $this->assign('info',$res);
  622. $this->assign('t',time());
  623. return $this->fetch();
  624. }
  625. public function cart(){
  626. // 需要检查是否还在售卖时间范围内,只取范围内的数据
  627. $cartCount = $this->cartCount();
  628. $map[] = ['org_id','=',$this->orgId];
  629. $map[] = ['user_id','=',$this->user['id']];
  630. $info = Db::name('dinner_cart')->where($map)->select();
  631. $a = [];
  632. foreach ($info as $k=>$v){
  633. $data = Db::name('dinner')->where('id',$v['dinner_id'])->find();
  634. $imgs = $data['imgs']?explode(',',$data['imgs']):[];
  635. $data['img'] = $imgs?$imgs[0]:'';
  636. $data['count'] = $v['count'];
  637. $data['cart_id'] = $v['id'];
  638. $a[] = $data;
  639. }
  640. $this->assign('cartCount',$cartCount);
  641. $this->assign('data',$a);
  642. return $this->fetch();
  643. }
  644. public function submitorder(){
  645. $bDinder = $this->user['isdinder'];
  646. if (request()->isGet()) {
  647. if(!decodecookie('is_spot')){
  648. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  649. }
  650. $orderids = input('orders');
  651. if (empty($orderids)) {
  652. return $this->fetch('h5/msg_error',['msg'=>'ids不能为空']);
  653. }
  654. $ids = explode(',', $orderids);
  655. //创建订单
  656. $cart =Db::name('dinner_cart')
  657. ->where('id','in',$ids)
  658. ->where('org_id',$this->orgId)
  659. ->where('user_id',$this->user['id'])
  660. ->column('dinner_id');
  661. if(empty($cart)){
  662. return $this->fetch('h5/msg_error',['msg'=>'购物车信息不存在']);
  663. }
  664. $all_data = Db::name('dinner')
  665. ->where('id','in',$cart)
  666. ->select();
  667. if(count($all_data) != count($ids)){
  668. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  669. }
  670. $is_spot = decodecookie('is_spot');
  671. $is_spot = $is_spot==1?1:0;
  672. $price_count =0;
  673. $curTime = date('Y-m-d H:i:s');
  674. $isSpecial = $this->checkSpecial();
  675. foreach ($all_data as $key => $value) {
  676. $catNum = Db::name('dinner_cart')
  677. ->where('dinner_id',$value['id'])
  678. ->where('org_id',$this->orgId)
  679. ->where('user_id',$this->user['id'])
  680. ->value('count');
  681. if($is_spot == 1){
  682. if($value['today_enable'] != 1){
  683. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'已下架']);
  684. }
  685. if($curTime < date('Y-m-d').' '.$value['today_start_time']||$curTime > date('Y-m-d').' '.$value['today_end_time']){
  686. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'不再售卖时间内']);
  687. }
  688. }
  689. if($is_spot == 0){
  690. if($value['enable'] != 1){
  691. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'已下架']);
  692. }
  693. if($curTime < date('Y-m-d').' '.$value['start_time']||$curTime > date('Y-m-d').' '.$value['end_time']){
  694. return $this->fetch('h5/msg_error',['msg'=>$value['name'].'不再售卖时间内']);
  695. }
  696. }
  697. $money = $value['money'];
  698. if($isSpecial == 1){
  699. $money = $this->getDiscountMoney($value);
  700. }
  701. $all_data[$key]['money'] = $money;
  702. $all_data[$key]['count'] = $catNum;
  703. $price_count += $money*$catNum;
  704. }
  705. $addressInfo = Db::name('dinner_address')
  706. ->where('id',$this->addressId)
  707. ->find();
  708. if(!$addressInfo){
  709. return $this->fetch('h5/msg_error',['msg'=>"地点不正确,请重新扫码"]);
  710. }
  711. $address = $addressInfo?$addressInfo['title']:'';
  712. $is_pay = true;
  713. $ret = Db::name('dinner_order')
  714. ->where('org_id',$this->orgId)
  715. ->where('user_id',$this->user['id'])
  716. ->order('id desc')
  717. ->find();
  718. if($ret){
  719. $this->assign('name',$ret['name']);
  720. $this->assign('phone',$ret['mobile']);
  721. }else{
  722. $this->assign('name','');
  723. $this->assign('phone','');
  724. }
  725. $this->assign('list',$all_data);
  726. $this->assign('count',$price_count);
  727. $this->assign('address',$address);
  728. $this->assign('bDinder',$bDinder);
  729. $this->assign('is_pay',$is_pay);
  730. $this->assign('HospitalName',$this->user['HospitalName']);
  731. $this->assign('ids',$orderids);
  732. //显示当前医院
  733. return $this->fetch();
  734. } else {
  735. if(!decodecookie('is_spot')){
  736. HelpHander::error('参数错误');
  737. }
  738. $orderids = input('orders');
  739. if (empty($orderids)) {
  740. HelpHander::error('ids不能为空');
  741. }
  742. if (empty($this->user)) {
  743. HelpHander::error('未登录请先登录');
  744. }
  745. $ids = explode(',', $orderids);
  746. //创建订单
  747. $cart =Db::name('dinner_cart')
  748. ->where('id','in',$ids)
  749. ->where('org_id',$this->orgId)
  750. ->where('user_id',$this->user['id'])
  751. ->column('dinner_id');
  752. if(empty($cart)){
  753. HelpHander::error('购物车信息不存在');
  754. }
  755. $all_data = Db::name('dinner')
  756. ->where('id','in',$cart)
  757. ->select();
  758. if(count($all_data) != count($ids)){
  759. HelpHander::error('参数错误');
  760. }
  761. $is_spot = decodecookie('is_spot');
  762. $is_spot = $is_spot==1?1:0;
  763. $is_take = input('is_take');
  764. if($is_take==1){
  765. $typeId = array_unique(array_column($all_data,'dinner_type_id'));
  766. if($typeId >1){
  767. HelpHander::error('自取订单只能下单同一分类菜品');
  768. }
  769. }
  770. $price_count = 0;
  771. $money_count = 0;
  772. $curTime = date('Y-m-d H:i:s');
  773. $isSpecial = $this->checkSpecial();
  774. foreach ($all_data as $key => $value) {
  775. $catNum = Db::name('dinner_cart')
  776. ->where('dinner_id',$value['id'])
  777. ->where('org_id',$this->orgId)
  778. ->where('user_id',$this->user['id'])
  779. ->value('count');
  780. if($is_spot == 1){
  781. if($value['today_enable'] != 1){
  782. HelpHander::error($value['name'].'已下架');
  783. }
  784. if($curTime < date('Y-m-d').' '.$value['today_start_time']||$curTime > date('Y-m-d').' '.$value['today_end_time']){
  785. HelpHander::error($value['name'].'不再售卖时间内');
  786. }
  787. }
  788. if($is_spot == 0){
  789. if($value['enable'] != 1){
  790. HelpHander::error($value['name'].'已下架');
  791. }
  792. if($curTime < date('Y-m-d').' '.$value['start_time']||$curTime > date('Y-m-d').' '.$value['end_time']){
  793. HelpHander::error($value['name'].'不再售卖时间内');
  794. }
  795. }
  796. $money1 = $value['money'];
  797. if($isSpecial == 1){
  798. $money1 = $this->getDiscountMoney($value);
  799. }
  800. $all_data[$key]['money'] = $money1;
  801. $all_data[$key]['original_price'] = $value['money'];
  802. $all_data[$key]['count'] = $catNum;
  803. $price_count += $money1*$catNum;
  804. $money_count += $value['money']*$catNum;
  805. }
  806. $money = $money_count - $price_count;
  807. $name =input('name');
  808. $mobile = input('mobile');
  809. $address = input('address');
  810. $order_type = input('order_type');
  811. $remark = trim(input('remark'));
  812. if($remark && mb_strlen($remark) > 15){
  813. HelpHander::error('床号及备注最多15字');
  814. }
  815. // log_message('error','值是:'.$ORDER_TYPE);
  816. // 如果没有的话,表示的就是自己订餐
  817. // 1-微信;2-食堂卡;3-现金;4-未付款(不需要付款)
  818. if (empty($order_type)) {
  819. $order_type = 1;
  820. $is_replace = 0;
  821. }
  822. // 4-未付款
  823. elseif ($order_type==4) {
  824. $is_replace = 0;
  825. }
  826. //
  827. else {
  828. $is_replace = 1;
  829. }
  830. $bDinder = $this->user['isdinder'];
  831. if($bDinder){
  832. $is_spot = 1; // 点餐员只能点当日餐
  833. }
  834. if (empty($name)) {
  835. HelpHander::error('姓名不能为空');
  836. }
  837. if (empty($mobile)) {
  838. HelpHander::error('手机号不能为空');
  839. }
  840. if(!check_mobile($mobile)){
  841. HelpHander::error('手机号格式错误');
  842. }
  843. if (empty($address)) {
  844. HelpHander::error('地址不能为空');
  845. }
  846. if (empty($remark)) {
  847. HelpHander::error('床号及备注不能为空');
  848. }
  849. Db::startTrans();
  850. if ($order_type==1) {
  851. $state = 0;
  852. } else {
  853. $state = 1;
  854. }
  855. if($price_count == 0){
  856. $state = 1;
  857. }
  858. try{
  859. $pp = array(
  860. 'price' => $price_count,
  861. 'state' => $state,
  862. 'org_id'=>$this->orgId,
  863. 'sn' => get_unique_id(),
  864. 'create_time' => date('Y-m-d H:i:s'),
  865. 'ymd'=>date('Ymd'),
  866. 'y'=>date('Y'),
  867. 'ym'=>date('Ym'),
  868. 'w'=>date('Y').date('W'),
  869. 'pay_time' => date('Y-m-d H:i:s'),
  870. 'user_id' => $this->user['id'],
  871. 'order_type'=>$order_type,
  872. 'name'=>$name,
  873. 'mobile'=>$mobile,
  874. 'address'=>$address,
  875. 'is_replace'=>$is_replace,
  876. 'is_spot'=>$is_spot,
  877. 'remark'=>$remark,
  878. 'is_take'=>$is_take,
  879. 'cate' => 0,
  880. 'money' => $money,
  881. );
  882. $orderId = Db::name('dinner_order')
  883. ->insertGetId($pp);
  884. if(!$orderId){
  885. \exception('订单添加失败');
  886. }
  887. $item = [];
  888. foreach ($all_data as $kk=>$vv){
  889. $item[] = [
  890. 'dinner_id'=>$vv['id'],
  891. 'order_id'=>$orderId,
  892. 'price'=>$vv['money']*$vv['count'],
  893. 'num'=>$vv['count'],
  894. 'dinner_type_id'=>$vv['dinner_type_id'],
  895. 'original_price'=>$vv['original_price'],
  896. 'money'=>$vv['money'],
  897. ];
  898. }
  899. $r = Db::name('dinner_order_item')->insertAll($item);
  900. if(!$r){
  901. \exception('操作失败');
  902. }
  903. $rr = Db::name('dinner_cart')
  904. ->where('id','in',$ids)
  905. ->delete();
  906. if(!$rr){
  907. \exception('操作失败');
  908. }
  909. Db::commit();
  910. if ($state==0) {
  911. HelpHander::success(['url'=>url('pay').'?id='.$orderId],'操作成功' );
  912. } else {
  913. HelpHander::success(['url'=>url('payresult').'?id='.$orderId], '操作成功');
  914. }
  915. }catch (Exception $e){
  916. Db::rollback();
  917. HelpHander::error($e->getMessage());
  918. }
  919. }
  920. }
  921. public function submitorder2(){
  922. $user = $this->user;
  923. if (request()->isGet()) {
  924. $ret = Db::name('dinner_apply')
  925. ->where('org_id',$this->orgId)
  926. ->where('user_id',$user['id'])
  927. ->where('del',0)
  928. ->where('status',1)
  929. ->find();
  930. if(!$ret){
  931. return $this->fetch('h5/msg_error',['msg'=>'只有特殊人群才可进入']);
  932. }
  933. $this->assign('name',$ret['name']);
  934. $this->assign('phone',$ret['phone']);
  935. $addressInfo = Db::name('dinner_address')
  936. ->where('id',$this->addressId)
  937. ->find();
  938. if(!$addressInfo){
  939. return $this->fetch('h5/msg_error',['msg'=>"地点不正确,请重新扫码"]);
  940. }
  941. $address = $addressInfo?$addressInfo['title']:'';
  942. $this->assign('HospitalName',$this->user['HospitalName']);
  943. $this->assign('address',$address);
  944. return $this->fetch();
  945. } else {
  946. if (empty($this->user)) {
  947. $this->error('未登录请先登录');
  948. }
  949. $is_take = 0;
  950. $name =input('name','','trim');
  951. $mobile = input('mobile','','trim');
  952. $address = input('address','','trim');
  953. $order_type = 4;
  954. $remark = trim(input('remark'));
  955. if($remark && mb_strlen($remark) > 15){
  956. $this->error('床号及备注最多15字');
  957. }
  958. if (empty($name)) {
  959. $this->error('姓名不能为空');
  960. }
  961. if (empty($mobile)) {
  962. $this->error('手机号不能为空');
  963. }
  964. if(!check_mobile($mobile)){
  965. $this->error('手机号格式错误');
  966. }
  967. if (empty($address)) {
  968. $this->error('地址不能为空');
  969. }
  970. if (empty($remark)) {
  971. $this->error('床号及备注不能为空');
  972. }
  973. $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find();
  974. $special = $config&&$config['special']==1?1:0;
  975. if(!$special){
  976. $this->error('未开启套餐预定模式');
  977. }
  978. $money = 0;
  979. $img = '';
  980. $goods = input('goods');
  981. if(!$goods){
  982. $this->error('参数错误');
  983. }
  984. $goods = json_decode($goods,true);
  985. if(!$goods){
  986. $this->error('参数错误');
  987. }
  988. $days = [];
  989. $type = 0;
  990. $all_data = [];
  991. $groups = [];
  992. foreach ($goods as $k=>$v){
  993. if($v['nums'] <= 0){
  994. $this->error('参数错误');
  995. }
  996. $dinner = Db::name('dinner_group_item')
  997. ->alias('a')
  998. ->join('dinner_group b','a.group_id = b.id')
  999. ->join('dinner c','a.dinner_id = c.id')
  1000. ->where('a.id',$v['id'])
  1001. ->where('b.enable',1)
  1002. ->where('b.del',0)
  1003. ->where('c.enable',1)
  1004. ->where('c.cate',1)
  1005. ->field('a.group_id,a.day,a.money,c.id,c.money as oldmoney,c.dinner_type_id,c.type,a.max,c.name')
  1006. ->find();
  1007. if(!$dinner){
  1008. $this->error('选择的部分商品不存在或已下架');
  1009. }
  1010. if($dinner['max'] > 0 && $v['nums'] > $dinner['max']){
  1011. $this->error($dinner['name'].'最多选择'.$dinner['max'].'个');
  1012. }
  1013. if($dinner['money'] == 0 && $dinner['oldmoney'] > 0){
  1014. $dinner['money'] == $dinner['oldmoney'];
  1015. }
  1016. unset($dinner['oldmoney']);
  1017. // 当天12点之前可以定明天的餐,12点之后可以定后台的餐
  1018. $hour = intval(date('H'));
  1019. if($hour < 12){
  1020. $curday = date('Y-m-d');
  1021. if($dinner['day'] <= $curday){
  1022. $this->error('12点之前只能提前一天预订');
  1023. }
  1024. }else{
  1025. $curday = date('Y-m-d');
  1026. $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订
  1027. if($dinner['day'] <= $curday){
  1028. $this->error('12点之后只能后天的餐');
  1029. }
  1030. }
  1031. $groups[] = $dinner['group_id'];
  1032. $dinner['nums'] = $v['nums'];
  1033. $all_data[] = $dinner;
  1034. $days[] = $dinner['day'];
  1035. if($dinner['type'] == 1){
  1036. $type = 1;
  1037. }
  1038. $money = $money + $dinner['money']*$v['nums'];
  1039. }
  1040. if($config['standard'] > 0){
  1041. foreach ($days as $day){
  1042. $dmoney = 0;
  1043. foreach ($all_data as $k=>$v){
  1044. if($v['day'] == $day){
  1045. $dmoney += $v['money']*$v['nums'];
  1046. }
  1047. }
  1048. if($dmoney > $config['standard']){
  1049. $this->error($day.'超过餐标标准');
  1050. }
  1051. }
  1052. }
  1053. $groups = array_unique($groups);
  1054. if(count($groups) > 1){
  1055. $this->error('一周只能点一种套餐');
  1056. }
  1057. // 检查本周是否有其他订单
  1058. $week = get_week_days(0); // 本周
  1059. $week1 = get_week_days(1); // 下周
  1060. $day = $days[0];
  1061. $start = "";
  1062. $end = "";
  1063. if($day >= $week[0] && $day <= $week[1]){
  1064. $start = $week[0];
  1065. $end = $week[1];
  1066. }
  1067. if($day >= $week1[0] && $day <= $week1[1]){
  1068. $start = $week1[0];
  1069. $end = $week1[1];
  1070. }
  1071. if(!$start){
  1072. $this->error('只能预订本周或下周的餐');
  1073. }
  1074. $ret = Db::name('dinner_order_item')
  1075. ->alias('a')
  1076. ->join('dinner_order b','a.order_id = b.id')
  1077. ->where('b.cate',1)
  1078. ->where('b.user_id',$this->user['id'])
  1079. ->where('a.group_id','<>',$groups[0])
  1080. ->where('a.day','>=',$start)
  1081. ->where('a.day','<=',$end)
  1082. ->find();
  1083. if($ret){
  1084. $this->error('一周只能点一种套餐');
  1085. }
  1086. // 验证这些天是否已有订单
  1087. foreach ($days as $k=>$v){
  1088. $days1 = Db::name('dinner_order_refuse_item')
  1089. ->alias('a')
  1090. ->join('dinner_order_refuse c','c.id = a.order_refuse_id')
  1091. ->join('dinner_order b','c.dinner_order_id = b.id')
  1092. ->where('b.cate',1)
  1093. ->where('c.status',4)
  1094. ->where('a.day',$v)
  1095. ->where('b.user_id',$this->user['id'])
  1096. ->field('a.*')
  1097. ->find();
  1098. if($days1){
  1099. $this->error($v.'已下过订单,不能下单');
  1100. }
  1101. $ret = Db::name('dinner_order_item')
  1102. ->alias('a')
  1103. ->join('dinner_order b','a.order_id = b.id')
  1104. ->where('b.user_id',$this->user['id'])
  1105. ->where('b.cate',1)
  1106. ->where('a.is_refuse',0)
  1107. ->where('a.day',$v)
  1108. ->find();
  1109. if($ret){
  1110. $this->error($v.'已下过订单,不能下单');
  1111. }
  1112. }
  1113. if($type == 1){ // 必须上传医嘱证明
  1114. if(!$_FILES['files']['tmp_name']){
  1115. $this->error('未上传医嘱证明');
  1116. }
  1117. try{
  1118. $updir = config('upload_dir');
  1119. $file = request()->file('files');
  1120. $fconfig = config('admin_upload_img');
  1121. $finfo = $file->validate($fconfig)
  1122. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files');
  1123. if($finfo){
  1124. $img = '/'.$updir.'/files/' . $finfo->getSaveName();
  1125. $img = request()->domain(true).str_replace('\\', '/', $img);
  1126. }else{
  1127. // 上传失败获取错误信息
  1128. \exception($file->getError());
  1129. }
  1130. }catch (\Exception $e){
  1131. $this->error($e->getMessage());
  1132. }
  1133. }
  1134. Db::startTrans();
  1135. try{
  1136. $pp = array(
  1137. 'price' => 0,
  1138. 'state' => 1,
  1139. 'org_id' => $this->orgId,
  1140. 'sn' => get_unique_id(),
  1141. 'create_time' => date('Y-m-d H:i:s'),
  1142. 'ymd'=>date('Ymd'),
  1143. 'y'=>date('Y'),
  1144. 'ym'=>date('Ym'),
  1145. 'w'=>date('Y').date('W'),
  1146. 'pay_time' => date('Y-m-d H:i:s'),
  1147. 'user_id' => $this->user['id'],
  1148. 'order_type'=>$order_type,
  1149. 'name'=>$name,
  1150. 'mobile'=>$mobile,
  1151. 'address'=>$address,
  1152. 'remark'=>$remark,
  1153. 'is_take'=>$is_take,
  1154. 'cate' => 1,
  1155. 'money' => $money,
  1156. 'img' => $img
  1157. );
  1158. $orderId = Db::name('dinner_order')->insertGetId($pp);
  1159. if(!$orderId){
  1160. \exception('订单添加失败');
  1161. }
  1162. $item = [];
  1163. foreach ($all_data as $kk=>$vv){
  1164. $item[] = [
  1165. 'dinner_id'=>$vv['id'],
  1166. 'order_id'=>$orderId,
  1167. 'price'=>$vv['money']*$vv['nums'],
  1168. 'original_price' => $vv['money'],
  1169. 'num'=>$vv['nums'],
  1170. 'dinner_type_id'=>$vv['dinner_type_id'],
  1171. 'group_id'=>$vv['group_id'],
  1172. 'day' => $vv['day'],
  1173. 'money'=>$vv['money'],
  1174. ];
  1175. }
  1176. $r = Db::name('dinner_order_item')->insertAll($item);
  1177. if(!$r){
  1178. \exception('操作失败');
  1179. }
  1180. Db::commit();
  1181. }catch (Exception $e){
  1182. Db::rollback();
  1183. $this->error($e->getMessage());
  1184. }
  1185. $this->redirect(url('payresult').'?id='.$orderId);
  1186. }
  1187. }
  1188. public function pay()
  1189. {
  1190. $id = input('id');
  1191. if (empty($id)) {
  1192. return $this->fetch('h5/msg_error',['msg'=>'ID不能为空']);
  1193. }
  1194. $_order = Db::name('dinner_order')
  1195. ->where('id',$id)
  1196. ->find();
  1197. if (empty($_order)) {
  1198. return $this->fetch('h5/msg_error',['msg'=>'不存此订单']);
  1199. }
  1200. if($_order['state'] != 0){
  1201. return $this->fetch('h5/msg_error',['msg'=>'此订单不能支付']);
  1202. }
  1203. if($_order['state'] == 0){
  1204. $tt = time() - strtotime($_order['create_time']);
  1205. if($tt >= 30*60){ // 半小时内可以支付
  1206. return $this->fetch('h5/msg_error',['msg'=>'此订单已过期不能支付']);
  1207. }
  1208. }
  1209. $notify_url = getSite().url('Notify/payDinner',['org_id'=>$_order['org_id']]);
  1210. $org = Db::name('org')->where('id',$_order['org_id'])->find();
  1211. $body = $org['name'].'_'.$_order['org_id'];
  1212. $config =get_pay_wechat($_order['org_id']);
  1213. $app = \EasyWeChat\Factory::payment($config);
  1214. $result = $app->order->unify([
  1215. 'body' => $body,
  1216. 'out_trade_no' => $_order['sn'],
  1217. 'total_fee' => $_order['price']*100,
  1218. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  1219. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  1220. 'openid' => $this->user['openid'],
  1221. ]);
  1222. trace($notify_url);
  1223. if($result['return_code'] != 'SUCCESS' || $result['result_code'] != 'SUCCESS'){
  1224. $msg = isset($result['return_msg'])?$result['return_msg']:'调起失败支付';
  1225. return $this->fetch('h5/msg_error',['msg'=>$msg]);
  1226. }
  1227. $jssdk = $app->jssdk;
  1228. $jsApiParameters = $jssdk->bridgeConfig($result['prepay_id']);
  1229. $params = array(
  1230. 'orderid'=>$id,
  1231. 'localorder' => $_order,
  1232. 'openid' => $this->user['openid'],
  1233. // 'input' => $input,
  1234. 'config' => $config,
  1235. // 'order' => $order,
  1236. 'jsApiParameters' => $jsApiParameters,
  1237. // 'editAddress' => $editAddress,
  1238. );
  1239. $params['jsApiParameters'] = json_encode($params['jsApiParameters']);
  1240. // var_dump($params);exit();
  1241. $this->assign('params',$params);
  1242. return $this->fetch();
  1243. }
  1244. public function payresult()
  1245. {
  1246. $msg = '';
  1247. $detail= '';
  1248. $type=0;
  1249. $id = input('id');
  1250. if (empty($id)) {
  1251. $msg='ID不能为空';
  1252. } else {
  1253. $_order = Db::name('dinner_order')
  1254. ->where('id',$id)
  1255. ->find();
  1256. if (empty($_order)) {
  1257. $msg='不存在此订单';
  1258. } else {
  1259. if ($_order['state']==1) {
  1260. $msg='订购成功!';
  1261. $type=1;
  1262. // 减库存
  1263. // $this->load->model('DinnercartModel');
  1264. // $this->DinnercartModel->dec_diner($_order);
  1265. } else {
  1266. $msg='支付可能失败';
  1267. $detail= '没有查询到订单成功的状态,可能系统缓慢原因导致,稍后在我的订单里查看';
  1268. }
  1269. }
  1270. }
  1271. $this->assign('msg',$msg);
  1272. $this->assign('type',$type);
  1273. $this->assign('detail',$detail);
  1274. return $this->fetch();
  1275. }
  1276. public function checkSpecial(){
  1277. $isSpecial = false;
  1278. $user = $this->user;
  1279. $ret = Db::name('dinner_apply')
  1280. ->where('org_id',$this->orgId)
  1281. ->where('user_id',$user['id'])
  1282. ->where('del',0)
  1283. ->where('status',1)
  1284. ->find();
  1285. if($ret){
  1286. $isSpecial = true;
  1287. }
  1288. return $isSpecial;
  1289. }
  1290. public function order()
  1291. {
  1292. $DinnerCount = $this->cartCount();
  1293. //用户个人信息
  1294. $user = $this->user;
  1295. $org = Db::name('org')->where('id',$this->orgId)->find();
  1296. $user['HospitalName'] = $org['name'];
  1297. $this->assign('orgName',$org['name']);
  1298. $DinnerCart = [];
  1299. $this->assign('cart',$DinnerCart);
  1300. $this->assign('cartCount',$DinnerCount);
  1301. $this->assign('user',$user);
  1302. $special = Db::name('dinner_config')->where('org_id',$this->orgId)->value('special');
  1303. $special = $special==1?$special:0;
  1304. if($special){
  1305. $isSpecial = 0; //未申请
  1306. $apply = Db::name('dinner_apply')
  1307. ->where('org_id',$this->orgId)
  1308. ->where('user_id',$user['id'])
  1309. ->where('del',0)
  1310. ->where('status','in',[0,1])
  1311. ->find();
  1312. if($apply && $apply['status'] == 1){
  1313. $isSpecial = 2; // 通过审核
  1314. }
  1315. if($apply && $apply['status'] == 0){
  1316. $isSpecial = 1; // 审核中
  1317. }
  1318. $this->assign('isSpecial',$isSpecial);
  1319. }
  1320. $this->assign('special',$special);
  1321. return $this->fetch();
  1322. }
  1323. public function userapply(){
  1324. if(request()->isPost()){
  1325. $user = $this->user;
  1326. $ret = Db::name('dinner_apply')
  1327. ->where('org_id',$this->orgId)
  1328. ->where('user_id',$user['id'])
  1329. ->where('del',0)
  1330. ->find();
  1331. if($ret && $ret['status'] == 0){
  1332. $this->error('信息审核中,勿重复提交');
  1333. }
  1334. if($ret && $ret['status'] == 1){
  1335. $this->error('你已是特殊人群,勿重复提交');
  1336. }
  1337. $updir = config('upload_dir');
  1338. $imgs = [];
  1339. if(!$_FILES['files']['tmp_name'][0]){
  1340. $this->error('未上传证明图片');
  1341. }
  1342. try{
  1343. $files = request()->file('files');
  1344. foreach($files as $file){
  1345. // 移动到框架应用根目录/uploads/ 目录下
  1346. $info = $file->validate(config('app.admin_upload_img'))->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files');
  1347. if($info){
  1348. $img = '/'.$updir.'/files/' . $info->getSaveName();
  1349. $img = str_replace('\\', '/', $img);
  1350. $path = request()->domain(true).$img;
  1351. $imgs[] = $path;
  1352. }else{
  1353. \exception($file->getError());
  1354. }
  1355. }
  1356. }catch (\Exception $e){
  1357. $this->error($e->getMessage());
  1358. }
  1359. $data = [
  1360. 'name' => input('name','','trim'),
  1361. 'phone' => input('TEL','','trim'),
  1362. 'imgs' => $imgs?implode(',',$imgs):'',
  1363. 'org_id' => $this->orgId,
  1364. 'user_id' => $this->user['id'],
  1365. 'status' => 0,
  1366. 'create_time' => date('Y-m-d H:i:s'),
  1367. 'remark' => input('remark','','trim')
  1368. ];
  1369. if(!$data['name']){
  1370. $this->error('请输入姓名');
  1371. }
  1372. if(!$data['phone']){
  1373. $this->error('请输入手机号');
  1374. }
  1375. if(!check_mobile($data['phone'])){
  1376. $this->error('手机号格式错误');
  1377. }
  1378. $ret = Db::name('dinner_apply')->insert($data);
  1379. if(!$ret){
  1380. $this->error('操作失败');
  1381. }else {
  1382. $this->redirect(url('order'));
  1383. }
  1384. }else{
  1385. return $this->fetch();
  1386. }
  1387. }
  1388. public function get_orders_json()
  1389. {
  1390. $user = $this->user;
  1391. $size = input('size');
  1392. $page = input('page');
  1393. $ret=$this->show_all_orders($user['id'], $page, $size);
  1394. if (empty($ret)) {
  1395. header('Content-Type:application/json; charset=utf-8');
  1396. exit(json_encode([]));
  1397. } else {
  1398. header('Content-Type:application/json; charset=utf-8');
  1399. exit(json_encode($ret));
  1400. }
  1401. }
  1402. public function show_all_orders($wxuserid, $page, $size)
  1403. {
  1404. $map[] = ['user_id','=',$wxuserid];
  1405. $ret = Db::name('dinner_order')
  1406. ->where($map)
  1407. ->page($page,$size)
  1408. ->order('id','desc')->select();
  1409. foreach ($ret as $key => $value) {
  1410. $ret[$key]['Todo'] = null;
  1411. $ret[$key]['pay'] = 0;
  1412. $ret[$key]['refund'] = 0;
  1413. if($value['state'] == 0){
  1414. $tt = time() - strtotime($value['create_time']);
  1415. if($tt < 30*60){ // 半小时内可以支付
  1416. $ret[$key]['pay'] = 1;
  1417. }
  1418. }
  1419. if($value['state']== 1||$value['state'] == 3){
  1420. if(in_array($value['is_ok'],[0,2])){
  1421. $day = date('Y-m-d');
  1422. $cday = date('Y-m-d',strtotime($value['create_time']));
  1423. if($day == $cday){ // 仅当天可退款
  1424. $ret[$key]['refund'] = 1;
  1425. }
  1426. }
  1427. }
  1428. }
  1429. return $ret;
  1430. }
  1431. public function order_detail(){
  1432. $id = input('id');
  1433. $info = Db::name('dinner_order')
  1434. ->where('id',$id)
  1435. ->find();
  1436. if(!$info){
  1437. $this->error('订单不存在');
  1438. }
  1439. if($info['cate'] == 0){
  1440. $all = Db::name('dinner_order_item')
  1441. ->alias('a')
  1442. ->join('dinner b','a.dinner_id=b.id')
  1443. ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.content,b.imgs')
  1444. ->where('a.order_id',$info['id'])
  1445. ->where('a.is_refuse',0)
  1446. ->select();
  1447. foreach ($all as $k=>$v){
  1448. $im = $v['imgs']?explode(',',$v['imgs']):[];
  1449. $all[$k]['img'] = $im?$im[0]:'';
  1450. }
  1451. $refuse = Db::name('dinner_order_refuse_item')
  1452. ->alias('a')
  1453. ->join('dinner_order_refuse c','a.order_refuse_id=c.id')
  1454. ->join('dinner b','a.dinner_id=b.id')
  1455. ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.money,b.content,b.imgs')
  1456. ->where('c.dinner_order_id',$info['id'])
  1457. ->select();
  1458. foreach ($refuse as $k=>$v){
  1459. $im = $v['imgs']?explode(',',$v['imgs']):[];
  1460. $refuse[$k]['img'] = $im?$im[0]:'';
  1461. }
  1462. $this->assign('item',$all);
  1463. $this->assign('refuse_item',$refuse);
  1464. }else{ // 套餐
  1465. $all = Db::name('dinner_order_item')
  1466. ->alias('a')
  1467. ->join('dinner b','a.dinner_id=b.id')
  1468. ->join('dinner_group c','a.group_id=c.id')
  1469. ->join('dinner_type d','a.dinner_type_id=d.id')
  1470. ->field('a.*,b.name as dinner_name,c.title as group_title,d.name as type_name,b.type')
  1471. ->where('a.order_id',$info['id'])
  1472. ->where('a.is_refuse',0)
  1473. ->order('a.id asc')
  1474. ->select();
  1475. $all = $all?$all:[];
  1476. $types = [];
  1477. $tids = [];
  1478. foreach ($all as $k=>$v){
  1479. $tid = $v['dinner_type_id'].'|'.$v['group_id'].'|'.$v['day'];
  1480. if(!in_array($tid,$tids)){
  1481. $tids[] = $tid;
  1482. }
  1483. }
  1484. $curday = date('Y-m-d');
  1485. // $curday = date('Y-m-d',strtotime($curday) + 24*60*60);
  1486. foreach ($tids as $tk=>$tv){
  1487. $dinners = [];
  1488. $type = 0;
  1489. $dinner_type_id = 0;
  1490. $dinner_type = '';
  1491. $group = '';
  1492. $group_id = 0;
  1493. $day = '';
  1494. foreach ($all as $k=>$v){
  1495. $tid = $v['dinner_type_id'].'|'.$v['group_id'].'|'.$v['day'];
  1496. if($tid == $tv){
  1497. $dinner_type = $v['type_name'];
  1498. $dinner_type_id = $v['dinner_type_id'];
  1499. $group = $v['group_title'];
  1500. $group_id = $v['group_id'];
  1501. $day = $v['day'];
  1502. $dinners[] = $v['dinner_name'].'*'.$v['num'];
  1503. if($v['type'] == 1){
  1504. $type = 1;
  1505. }
  1506. }
  1507. }
  1508. $options = 1;
  1509. if($info['state'] != 1){
  1510. $options = 0;
  1511. }
  1512. if($day <= $curday){
  1513. $options = 0;
  1514. }
  1515. $types[] = [
  1516. 'dinner_type_id' => $dinner_type_id,
  1517. 'dinner_type' => $dinner_type,
  1518. 'day' => $day,
  1519. 'options' => $options,
  1520. 'group' => $group,
  1521. 'group_id' => $group_id,
  1522. 'type' => $type,
  1523. 'dinners' => implode(',',$dinners),
  1524. 'sign' => think_encrypt($info['id'].'|'.$dinner_type_id.'|'.$day.'|'.$group_id)
  1525. ];
  1526. }
  1527. $info['types'] = $types;
  1528. }
  1529. $info['orgName'] = Db::name('org')
  1530. ->where('id',$info['org_id'])
  1531. ->value('name');
  1532. $this->assign('info',$info);
  1533. return $this->fetch();
  1534. }
  1535. public function canceldinner(){
  1536. $sign = think_decrypt(input('sign','','trim'));
  1537. if(!$sign){
  1538. $this->error('参数错误');
  1539. }
  1540. $signs = explode('|',$sign);
  1541. if(count($signs) != 4){
  1542. $this->error('参数错误');
  1543. }
  1544. $orderId = $signs[0];
  1545. $dinnerTypeId = $signs[1];
  1546. $day = $signs[2];
  1547. $groupId = $signs[3];
  1548. $cur = date('Y-m-d');
  1549. if($day <= $cur){
  1550. $this->error('只能取消明天或之后的餐');
  1551. }
  1552. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1553. if(!$order){
  1554. $this->error("订单不存在");
  1555. }
  1556. if($order['state'] != 1){
  1557. $this->error('订单该状态无法操作');
  1558. }
  1559. if($order['user_id'] != $this->user['id']){
  1560. $this->error('无权限操作');
  1561. }
  1562. // 取出
  1563. $ids = Db::name('dinner_order_item')
  1564. ->where('is_refuse',0)
  1565. ->where('dinner_type_id',$dinnerTypeId)
  1566. ->where('group_id',$groupId)
  1567. ->where('day',$day)
  1568. ->where('order_id',$orderId)
  1569. ->column('id');
  1570. if(!$ids){
  1571. $this->error("参数错误或已取消");
  1572. }
  1573. Db::startTrans();
  1574. try{
  1575. // 查询订单下是否还有有效餐品
  1576. $ids2 = Db::name('dinner_order_item')
  1577. ->where('is_refuse',0)
  1578. ->where('id','not in',$ids)
  1579. ->where('order_id',$orderId)
  1580. ->column('id');
  1581. if(!$ids2){ // 没有有效的餐品,订单改完已退款状态
  1582. $ret = Db::name('dinner_order')->where('id',$orderId)->update(['state'=>5,'update_time'=>date('Y-m-d H:i;s')]);
  1583. if(!$ret){
  1584. \exception('操作失败');
  1585. }
  1586. }
  1587. $ret = Db::name('dinner_order_item')->where('id','in',$ids)->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i;s')]);
  1588. if(!$ret){
  1589. \exception('操作失败');
  1590. }
  1591. Db::commit();
  1592. }catch (\Exception $e){
  1593. Db::rollback();
  1594. $this->error('操作失败');
  1595. }
  1596. $this->success('操作成功');
  1597. }
  1598. public function changedinner(){
  1599. $initsign = input('sign','','trim');
  1600. $sign = think_decrypt($initsign);
  1601. if(!$sign){
  1602. if(request()->isPost()){
  1603. $this->error('参数错误');
  1604. }else{
  1605. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1606. }
  1607. }
  1608. $signs = explode('|',$sign);
  1609. if(count($signs) != 4){
  1610. if(request()->isPost()){
  1611. $this->error('参数错误');
  1612. }else{
  1613. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1614. }
  1615. }
  1616. $orderId = $signs[0];
  1617. $dinnerTypeId = $signs[1];
  1618. $day = $signs[2];
  1619. $groupId = $signs[3];
  1620. if(request()->isPost()){
  1621. $dinnerId = input('dinner_id/d',0);
  1622. if($dinnerId <= 0){
  1623. $this->error('请选择要替换的流食或半流食餐品');
  1624. }
  1625. $info = Db::name('dinner_group_item')
  1626. ->alias('a')
  1627. ->join('dinner b','a.dinner_id = b.id')
  1628. ->where('a.id',$dinnerId)
  1629. ->where('b.dinner_type_id',$dinnerTypeId)
  1630. ->where('b.type',1)
  1631. ->where('b.enable',1)
  1632. ->field('a.*,b.money as oldmoney')
  1633. ->find();
  1634. if(!$info){
  1635. $this->error('选择的流食或半流食餐品不存在或已下架');
  1636. }
  1637. if($info['money'] == 9 && $info['old_money'] > 0){
  1638. $info['money'] = $info['old_money'];
  1639. }
  1640. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1641. if(!$order){
  1642. $this->error('订单不存在');
  1643. }
  1644. if($order['state'] != 1){
  1645. $this->error('订单该状态无法操作');
  1646. }
  1647. if($order['user_id'] != $this->user['id']){
  1648. $this->error('无权限操作');
  1649. }
  1650. // 取出要退款的餐品
  1651. $ids = Db::name('dinner_order_item')
  1652. ->where('is_refuse',0)
  1653. ->where('dinner_type_id',$dinnerTypeId)
  1654. ->where('group_id',$groupId)
  1655. ->where('day',$day)
  1656. ->where('order_id',$orderId)
  1657. ->column('id');
  1658. if(!$ids){
  1659. $this->error("参数错误或已取消");
  1660. }
  1661. $tmoney = Db::name('dinner_order_item')->where('id','in',$ids)->sum('price');
  1662. $money = $order['money'] - $tmoney + $info['money'];
  1663. $img = $order['img'];
  1664. if(!$img){
  1665. if(!$_FILES['files']['tmp_name']){
  1666. $this->error('未上传医嘱证明');
  1667. }
  1668. try{
  1669. $updir = config('upload_dir');
  1670. $file = request()->file('files');
  1671. $config = config('admin_upload_img');
  1672. $finfo = $file->validate($config)
  1673. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files');
  1674. if($finfo){
  1675. $img = '/'.$updir.'/files/' . $finfo->getSaveName();
  1676. $img = request()->domain(true).str_replace('\\', '/', $img);
  1677. }else{
  1678. // 上传失败获取错误信息
  1679. \exception($file->getError());
  1680. }
  1681. }catch (\Exception $e){
  1682. $this->error($e->getMessage());
  1683. }
  1684. }
  1685. Db::startTrans();
  1686. try{
  1687. $ret = Db::name('dinner_order')->where('id',$orderId)->update(['money'=>$money,'img'=>$img,'type'=>1,'update_time'=>date('Y-m-d H:i;s')]);
  1688. if(!$ret){
  1689. \exception('操作失败');
  1690. }
  1691. $ret = Db::name('dinner_order_item')->where('id','in',$ids)->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i;s')]);
  1692. if(!$ret){
  1693. \exception('操作失败');
  1694. }
  1695. // 添加流食商品
  1696. $ret = Db::name('dinner_order_item')->insert([
  1697. 'dinner_id' => $info['dinner_id'],
  1698. 'order_id' => $orderId,
  1699. 'price' => $info['money'],
  1700. 'original_price' => $info['money'],
  1701. 'money' => $info['money'],
  1702. 'num' => 1,
  1703. 'dinner_type_id' => $dinnerTypeId,
  1704. 'group_id' => $groupId,
  1705. 'day' => $day
  1706. ]);
  1707. if(!$ret){
  1708. \exception('操作失败');
  1709. }
  1710. Db::commit();
  1711. }catch (\Exception $e){
  1712. Db::rollback();
  1713. $this->error('操作失败');
  1714. }
  1715. $this->redirect(url('order_detail',['id'=>$orderId]));
  1716. }else{
  1717. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1718. if(!$order){
  1719. return $this->fetch('h5/msg_error',['msg'=>'订单不存在']);
  1720. }
  1721. $this->assign('order',$order);
  1722. // 获取流食商品
  1723. $dinners = Db::name('dinner_group_item')
  1724. ->alias('a')
  1725. ->join('dinner b','a.dinner_id = b.id')
  1726. ->where('a.day',$day)
  1727. ->where('a.group_id',$groupId)
  1728. ->where('b.dinner_type_id',$dinnerTypeId)
  1729. ->where('b.type',1)
  1730. ->field('a.id,b.name,b.money as oldmoney,a.money')
  1731. ->select();
  1732. $dinners = $dinners?$dinners:[];
  1733. foreach ($dinners as $k=>$v){
  1734. if($v['money'] == 0 && $v['oldmoney'] > 0){
  1735. $dinners[$k]['money'] = $v['oldmoney'];
  1736. }
  1737. }
  1738. $this->assign('dinners',$dinners);
  1739. $group = Db::name('dinner_group')->where('id',$groupId)->value('title');
  1740. $dinnertype = Db::name('dinner_type')->where('id',$dinnerTypeId)->value('name');
  1741. $ids = Db::name('dinner_order_item')
  1742. ->where('is_refuse',0)
  1743. ->where('dinner_type_id',$dinnerTypeId)
  1744. ->where('group_id',$groupId)
  1745. ->where('day',$day)
  1746. ->where('order_id',$orderId)
  1747. ->column('dinner_id');
  1748. $names = [];
  1749. if($ids){
  1750. $names = Db::name('dinner')->where('id','in',$ids)->column('name');
  1751. }
  1752. $this->assign('group',$group?$group:'');
  1753. $this->assign('dinnertype',$dinnertype?$dinnertype:'');
  1754. $this->assign('day',$day);
  1755. $this->assign('dinnernames',$names?implode(',',$names):'');
  1756. $this->assign('orderId',$orderId);
  1757. $this->assign('sign',$initsign);
  1758. return $this->fetch();
  1759. }
  1760. }
  1761. public function refunddinner(){
  1762. $initsign = input('sign','','trim');
  1763. $sign = think_decrypt($initsign);
  1764. if(!$sign){
  1765. if(request()->isPost()){
  1766. $this->error('参数错误');
  1767. }else{
  1768. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1769. }
  1770. }
  1771. $signs = explode('|',$sign);
  1772. if(count($signs) != 4){
  1773. if(request()->isPost()){
  1774. $this->error('参数错误');
  1775. }else{
  1776. return $this->fetch('h5/msg_error',['msg'=>'参数错误']);
  1777. }
  1778. }
  1779. $curday = date('Y-m-d');
  1780. // $curday = date('Y-m-d',strtotime($curday) + 24*60*60);
  1781. $orderId = $signs[0];
  1782. $dinnerTypeId = $signs[1];
  1783. $day = $signs[2];
  1784. $groupId = $signs[3];
  1785. if(request()->isPost()){
  1786. $day = input('day',0);
  1787. if(!$day){
  1788. $this->error('请选择要退餐的日期');
  1789. }
  1790. if($day <= $curday){
  1791. $this->error('已超过退餐时间,不能退餐');
  1792. }
  1793. $data = Db::name('dinner_order')
  1794. ->where('id',$orderId)
  1795. ->find();
  1796. if (!in_array($data['state'],[1])) {
  1797. $this->error('订单号不是已支付状态,不能执行退款');
  1798. }
  1799. if ($data['is_replace'] !=0) {
  1800. $this->error('不是您自己订的不能退款,退款请联系工作人员');
  1801. }
  1802. if (!in_array($data['is_ok'],[0,2])) {
  1803. $this->error('当前订单已标记完成');
  1804. }
  1805. $items = Db::name('dinner_order_item')->where('order_id',$orderId)->where('day',$day)->select();
  1806. $ids = [];
  1807. foreach ($items as $k=>$v){
  1808. if($v['is_refuse'] == 1){
  1809. $this->error('已有商品退餐,勿重复退餐');
  1810. }
  1811. $ids[] = $v['id'];
  1812. }
  1813. Db::startTrans();
  1814. try{
  1815. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  1816. 'dinner_order_id'=>$orderId
  1817. ,'refuse_price'=>0
  1818. ,'refuse_y'=>date('Y')
  1819. ,'refuse_ymd'=>date('Ymd')
  1820. ,'refuse_ym'=>date('Ym')
  1821. ,'created_user_id'=>$this->user['id']
  1822. ,'create_time'=>date('Y-m-d H:i:s')
  1823. ,'refuse_w'=>date('Y').date('W')
  1824. ,'order_type'=>$data['order_type']
  1825. ,'is_replace'=>$data['is_replace']
  1826. ,'org_id'=>$this->orgId
  1827. ,'status'=>4
  1828. ,'refuse_remark'=>''
  1829. ));
  1830. if(!$refuse_id){
  1831. \exception('退餐单添加失败');
  1832. }
  1833. $refuse_item = [];
  1834. foreach ($items as $k=>$v){
  1835. $refuse_item[] = [
  1836. 'dinner_id'=>$v['dinner_id'],
  1837. 'order_refuse_id'=>$refuse_id,
  1838. 'price'=>$v['price'],
  1839. 'num'=>$v['num'],
  1840. 'dinner_type_id'=>$v['dinner_type_id'],
  1841. 'day' => $v['day'],
  1842. 'group_id' => $v['group_id']
  1843. ];
  1844. }
  1845. $rr = Db::name('dinner_order_refuse_item')
  1846. ->insertAll($refuse_item);
  1847. if(!$rr){
  1848. \exception('操作失败2');
  1849. }
  1850. $rr = Db::name('dinner_order_item')
  1851. ->where('id','in',$ids)
  1852. ->update([
  1853. 'is_refuse'=>1
  1854. ]);
  1855. if(!$rr){
  1856. \exception('操作失败1');
  1857. }
  1858. // $res = Db::name('dinner_order')
  1859. // ->where('id',$orderId)
  1860. // ->update([
  1861. // 'state'=>4,
  1862. // 'update_time'=>getTime()
  1863. // ]);
  1864. //
  1865. // if(!$res){
  1866. // \exception('订单更新失败');
  1867. // }
  1868. Db::commit();
  1869. }catch (\Exception $e){
  1870. Db::rollback();
  1871. $this->error('操作失败'.$e->getMessage());
  1872. }
  1873. $this->redirect(url('order_detail',['id'=>$orderId]));
  1874. }else{
  1875. $order = Db::name('dinner_order')->where('id',$orderId)->find();
  1876. if(!$order){
  1877. return $this->fetch('h5/msg_error',['msg'=>'订单不存在']);
  1878. }
  1879. $this->assign('order',$order);
  1880. $days = Db::name('dinner_order_item')
  1881. ->where('is_refuse',0)
  1882. ->where('dinner_type_id',$dinnerTypeId)
  1883. ->where('group_id',$groupId)
  1884. ->where('order_id',$orderId)
  1885. ->where('day','>',$curday)
  1886. ->group('day')
  1887. ->column('day');
  1888. $this->assign('day',$day);
  1889. $this->assign('days',$days);
  1890. // $group = Db::name('dinner_group')->where('id',$groupId)->value('title');
  1891. // $dinnertype = Db::name('dinner_type')->where('id',$dinnerTypeId)->value('name');
  1892. //
  1893. // $ids = Db::name('dinner_order_item')
  1894. // ->where('is_refuse',0)
  1895. // ->where('dinner_type_id',$dinnerTypeId)
  1896. // ->where('group_id',$groupId)
  1897. // ->where('day',$day)
  1898. // ->where('order_id',$orderId)
  1899. // ->column('dinner_id');
  1900. // $names = [];
  1901. // if($ids){
  1902. // $names = Db::name('dinner')->where('id','in',$ids)->column('name');
  1903. // }
  1904. // $this->assign('group',$group?$group:'');
  1905. // $this->assign('dinnertype',$dinnertype?$dinnertype:'');
  1906. // $this->assign('day',$day);
  1907. // $this->assign('dinnernames',$names?implode(',',$names):'');
  1908. $this->assign('orderId',$orderId);
  1909. $this->assign('sign',$initsign);
  1910. return $this->fetch();
  1911. }
  1912. }
  1913. public function refuse(){
  1914. $orderid = input('id');
  1915. if (empty($orderid)) {
  1916. return $this->fetch('h5/msg_error',['msg'=>'订单号不能为空']);
  1917. }
  1918. $data = Db::name('dinner_order')
  1919. ->where('id',$orderid)
  1920. ->find();
  1921. if (!in_array($data['state'],[1])) {
  1922. return $this->fetch('h5/msg_error',['msg'=>'订单号不是已支付状态,不能执行退款']);
  1923. }
  1924. if ($data['is_replace'] !=0) {
  1925. return $this->fetch('h5/msg_error',['msg'=>'不是您自己订的不能退款,退款请联系工作人员']);
  1926. }
  1927. if (!in_array($data['is_ok'],[0,2])) {
  1928. return $this->fetch('h5/msg_error',['msg'=>'当前订单已标记完成']);
  1929. }
  1930. if($data['state'] == 1 ||$data['state'] == 3){
  1931. $day = date('Y-m-d');
  1932. $cday = date('Y-m-d',strtotime($data['create_time']));
  1933. if($day != $cday){ // 仅当天可退款
  1934. return $this->fetch('h5/msg_error',['msg'=>'订单仅当天能退款']);
  1935. }
  1936. }
  1937. $all = Db::name('dinner_order_item')
  1938. ->alias('a')
  1939. ->join('dinner b','a.dinner_id=b.id')
  1940. ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.money,b.content,b.imgs')
  1941. ->where('a.order_id',$orderid)
  1942. ->where('a.is_refuse',0)
  1943. ->select();
  1944. foreach ($all as $k=>$v){
  1945. $im = explode(',',$v['imgs']);
  1946. $all[$k]['img'] = $im[0];
  1947. }
  1948. $data['cartInfo'] = $all;
  1949. if (request()->isGet()) {
  1950. $this->assign('data',$data);
  1951. $this->assign('id',$orderid);
  1952. return $this->fetch();
  1953. } else {
  1954. $d = request()->post();
  1955. if (empty($d)) {
  1956. HelpHander::error('没有选择退单的商品');
  1957. }
  1958. $post_d = array();
  1959. foreach ($d as $key => $value) {
  1960. $arr = explode("_", $key);
  1961. $post_d[] = $arr[1];
  1962. }
  1963. $refuse_price = 0;
  1964. $items = Db::name('dinner_order_item')
  1965. ->where('id','in',$post_d)
  1966. ->select();
  1967. foreach ($items as $k=>$v){
  1968. if($v['is_refuse']!=0){
  1969. HelpHander::error('当前选择物品有已退单物品,请刷新,重新选择');
  1970. }
  1971. $refuse_price+=$v['price'];
  1972. }
  1973. try{
  1974. Db::startTrans();
  1975. if($data['is_spot']==0){//预定餐
  1976. $config3 = Db::name('dinner_config')
  1977. ->where('org_id',$data['org_id'])
  1978. ->find();
  1979. $zd = 0;//0自动退款1审核退款
  1980. if(empty($config3) || empty($config3['content3'])){
  1981. $zd =1;
  1982. }else{
  1983. $cur = date('Y-m-d H:i');
  1984. $cc = date('Y-m-d H:i',strtotime(date('Y-m-d').' '.$config3['content3']));
  1985. if($cur >$cc){
  1986. $zd = 1;
  1987. }else{
  1988. $zd = 0;
  1989. }
  1990. }
  1991. if($zd == 0){
  1992. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  1993. 'dinner_order_id'=>$orderid
  1994. ,'refuse_price'=>$refuse_price
  1995. ,'refuse_y'=>date('Y')
  1996. ,'refuse_ymd'=>date('Ymd')
  1997. ,'refuse_ym'=>date('Ym')
  1998. ,'created_user_id'=>$this->user['id']
  1999. ,'create_time'=>date('Y-m-d H:i:s')
  2000. ,'refuse_w'=>date('Y').date('W')
  2001. ,'order_type'=>$data['order_type']
  2002. ,'is_replace'=>$data['is_replace']
  2003. ,'org_id'=>$this->orgId
  2004. ,'status'=>5
  2005. ,'refuse_remark'=>'自动退款'
  2006. ));
  2007. if(!$refuse_id){
  2008. \exception('退款单添加失败');
  2009. }
  2010. $refuse_item = [];
  2011. foreach ($items as $k=>$v){
  2012. $refuse_item[] = [
  2013. 'dinner_id'=>$v['dinner_id'],
  2014. 'order_refuse_id'=>$refuse_id,
  2015. 'price'=>$v['price'],
  2016. 'num'=>$v['num'],
  2017. 'dinner_type_id'=>$v['dinner_type_id'],
  2018. ];
  2019. }
  2020. $rr = Db::name('dinner_order_refuse_item')
  2021. ->insertAll($refuse_item);
  2022. if(!$rr){
  2023. \exception('操作失败');
  2024. }
  2025. $rr = Db::name('dinner_order_item')
  2026. ->where('id','in',$post_d)
  2027. ->update([
  2028. 'is_refuse'=>1
  2029. ]);
  2030. if(!$rr){
  2031. \exception('操作失败');
  2032. }
  2033. $checkAll = Db::name('dinner_order_item')
  2034. ->where('order_id',$orderid)
  2035. ->where('is_refuse',0)
  2036. ->find();
  2037. if($checkAll){//部分退款
  2038. $res = Db::name('dinner_order')
  2039. ->where('id',$orderid)
  2040. ->update([
  2041. 'state'=>7,
  2042. 'update_time'=>getTime()
  2043. ]);
  2044. }else{//全部退款
  2045. $res = Db::name('dinner_order')
  2046. ->where('id',$orderid)
  2047. ->update([
  2048. 'state'=>5,
  2049. 'update_time'=>getTime()
  2050. ]);
  2051. }
  2052. if(!$res){
  2053. \exception('订单更新失败');
  2054. }
  2055. $config = get_pay_wechat($data['org_id']);
  2056. $app = \EasyWeChat\Factory::payment($config);
  2057. $ret = $app->refund->byOutTradeNumber($data['sn'],
  2058. 'T' . $data['sn'],
  2059. $data['price'] * 100, $refuse_price * 100);
  2060. if ($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS') {
  2061. trace(json_encode($ret));
  2062. \exception('微信退订失败:'.$ret['return_msg']);
  2063. }
  2064. }else{
  2065. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  2066. 'dinner_order_id'=>$orderid
  2067. ,'refuse_price'=>$refuse_price
  2068. ,'refuse_y'=>date('Y')
  2069. ,'refuse_ymd'=>date('Ymd')
  2070. ,'refuse_ym'=>date('Ym')
  2071. ,'created_user_id'=>$this->user['id']
  2072. ,'create_time'=>date('Y-m-d H:i:s')
  2073. ,'refuse_w'=>date('Y').date('W')
  2074. ,'order_type'=>$data['order_type']
  2075. ,'is_replace'=>$data['is_replace']
  2076. ,'org_id'=>$this->orgId
  2077. ,'status'=>4
  2078. ));
  2079. if(!$refuse_id){
  2080. \exception('退款单添加失败');
  2081. }
  2082. $refuse_item = [];
  2083. foreach ($items as $k=>$v){
  2084. $refuse_item[] = [
  2085. 'dinner_id'=>$v['dinner_id'],
  2086. 'order_refuse_id'=>$refuse_id,
  2087. 'price'=>$v['price'],
  2088. 'num'=>$v['num'],
  2089. 'dinner_type_id'=>$v['dinner_type_id'],
  2090. ];
  2091. }
  2092. $rr = Db::name('dinner_order_refuse_item')
  2093. ->insertAll($refuse_item);
  2094. if(!$rr){
  2095. \exception('操作失败');
  2096. }
  2097. $rr = Db::name('dinner_order_item')
  2098. ->where('id','in',$post_d)
  2099. ->update([
  2100. 'is_refuse'=>1
  2101. ]);
  2102. if(!$rr){
  2103. \exception('操作失败');
  2104. }
  2105. $res = Db::name('dinner_order')
  2106. ->where('id',$orderid)
  2107. ->update([
  2108. 'state'=>4,
  2109. 'update_time'=>getTime()
  2110. ]);
  2111. if(!$res){
  2112. \exception('订单更新失败');
  2113. }
  2114. }
  2115. }else{
  2116. $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array(
  2117. 'dinner_order_id'=>$orderid
  2118. ,'refuse_price'=>$refuse_price
  2119. ,'refuse_y'=>date('Y')
  2120. ,'refuse_ymd'=>date('Ymd')
  2121. ,'refuse_ym'=>date('Ym')
  2122. ,'created_user_id'=>$this->user['id']
  2123. ,'create_time'=>date('Y-m-d H:i:s')
  2124. ,'refuse_w'=>date('Y').date('W')
  2125. ,'order_type'=>$data['order_type']
  2126. ,'is_replace'=>$data['is_replace']
  2127. ,'org_id'=>$this->orgId
  2128. ,'status'=>4
  2129. ));
  2130. if(!$refuse_id){
  2131. \exception('退款单添加失败');
  2132. }
  2133. $refuse_item = [];
  2134. foreach ($items as $k=>$v){
  2135. $refuse_item[] = [
  2136. 'dinner_id'=>$v['dinner_id'],
  2137. 'order_refuse_id'=>$refuse_id,
  2138. 'price'=>$v['price'],
  2139. 'num'=>$v['num'],
  2140. 'dinner_type_id'=>$v['dinner_type_id'],
  2141. ];
  2142. }
  2143. $rr = Db::name('dinner_order_refuse_item')
  2144. ->insertAll($refuse_item);
  2145. if(!$rr){
  2146. \exception('操作失败');
  2147. }
  2148. $rr = Db::name('dinner_order_item')
  2149. ->where('id','in',$post_d)
  2150. ->update([
  2151. 'is_refuse'=>1
  2152. ]);
  2153. if(!$rr){
  2154. \exception('操作失败');
  2155. }
  2156. $res = Db::name('dinner_order')
  2157. ->where('id',$orderid)
  2158. ->update([
  2159. 'state'=>4,
  2160. 'update_time'=>getTime()
  2161. ]);
  2162. if(!$res){
  2163. \exception('订单更新失败');
  2164. }
  2165. }
  2166. Db::commit();
  2167. HelpHander::success([],'操作成功');
  2168. }catch (Exception $e){
  2169. Db::rollback();
  2170. trace($e->getMessage());
  2171. HelpHander::error($e->getMessage());
  2172. }
  2173. }
  2174. }
  2175. public function loginuser(){
  2176. if (request()->isGet()) {
  2177. return $this->fetch();
  2178. } else {
  2179. $tel = input('TEL');
  2180. $code = input('VCODE');
  2181. if(!$tel){
  2182. HelpHander::error('手机不能为空');
  2183. }
  2184. $info = Db::name('dinner_user')
  2185. ->where('org_id',$this->orgId)
  2186. ->where('mobile',$tel)
  2187. ->where('del',0)
  2188. ->where('enable',1)
  2189. ->find();
  2190. if(!$info){
  2191. HelpHander::error('点餐员不存在');
  2192. }
  2193. if(!$code){
  2194. HelpHander::error('验证码不能为空');
  2195. }
  2196. if(!verify_sms($tel,$code)){
  2197. HelpHander::error('验证码信息错误');
  2198. }
  2199. $user = $this->user;
  2200. $user['isdinder'] = 1;
  2201. encodecookie('isdinder',1);
  2202. // 清空购物车
  2203. Db::name('dinner_cart')->where('user_id')->delete();
  2204. HelpHander::success([],'操作成功');
  2205. }
  2206. }
  2207. public function sms(){
  2208. $phone = input('tel');
  2209. if(empty($phone)){
  2210. HelpHander::error('手机号不能为空');
  2211. }
  2212. $res = send_verify_sms($phone);
  2213. if(!$res){
  2214. HelpHander::error('发送失败');
  2215. }
  2216. HelpHander::success([],'发送成功');
  2217. }
  2218. }