Ecard.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 行业分类
  7. */
  8. namespace app\allwin\controller;
  9. use app\allwin\model\AllwinEcard;
  10. use app\allwin\widget\Card;
  11. use app\allwin\model\AllwinStore;
  12. class Ecard extends Common{
  13. public function initialize() {
  14. parent::initialize();
  15. $this->assign('pathMaps',[['name'=>'惠卡配置','url'=>url("ecard/index")]]);
  16. }
  17. /**
  18. * 列表
  19. */
  20. public function index(){
  21. if(request()->isAjax()){
  22. $data = [
  23. 'id' => input('post.id/s'),
  24. 'brand_name' => input('post.brand_name/s'),
  25. 'annual_fee' => input('post.annual_fee/s'),
  26. 'gh_id' => input('post.gh_id/s'),
  27. 'title' => input('post.title/s'),
  28. 'notice' => input('post.notice/s'),
  29. 'description' => input('post.description/s'),
  30. 'quantity' => input('post.quantity/d'),
  31. 'color' => input('post.color/s'),
  32. 'picture' => input('post.picture/s'),
  33. 'end_time' => input('post.end_time/s'),
  34. 'prerogative' => input('post.prerogative/s'),
  35. 'start_time' => input('post.start_time/s'),
  36. 'member_miniapp_id' => $this->member_miniapp_id
  37. ];
  38. $data['end_time'] = empty($data['end_time']) ? 0 : strtotime($data['end_time']);
  39. $data['start_time'] = empty($data['start_time']) ? 0 : strtotime($data['start_time']);
  40. $validate = $this->validate($data,'Ecard.edit');
  41. if(true !== $validate){
  42. return enjson(0,$validate);
  43. }
  44. if(empty($data['id'])){
  45. $data['create_time'] = time();
  46. $rel = Card::create($data);
  47. if($rel['errcode'] == 0){
  48. $data['card_id'] = $rel['card_id'];
  49. $result = AllwinEcard::create($data);
  50. }else{
  51. return enjson(0,'操作失败');
  52. }
  53. }else{
  54. $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $data['id']])->find();
  55. if($info->start_time == $data['start_time'] && $info->end_time == $data['end_time']){
  56. $state = 0;
  57. }else if($info->start_time > $data['start_time'] && $info->end_time < $data['end_time']){
  58. $state = 1;
  59. }else{
  60. return enjson(0,'只支持往后延长时间');
  61. }
  62. //增加减少库存
  63. $rel['errcode'] = 0;
  64. if($info->quantity > $data['quantity']){
  65. $rel = Card::reductStock($info['card_id'],$info->quantity - $data['quantity'], $this->member_miniapp_id);
  66. }else if($info->quantity < $data['quantity']){
  67. $rel = Card::increaseStock($info['card_id'],$data['quantity'] - $info->quantity, $this->member_miniapp_id);
  68. }
  69. if($rel['errcode'] == 0){
  70. $rel = Card::update($data,$info['card_id'],$state);
  71. if($rel['errcode'] == 0){
  72. $result = AllwinEcard::where(['id' => $data['id']])->update($data);
  73. }else{
  74. return enjson(0,'操作失败');
  75. }
  76. }else{
  77. return enjson(0,'操作失败');
  78. }
  79. }
  80. if($result){
  81. return enjson(200,'操作成功',['url'=>url('ecard/index')]);
  82. }else{
  83. return enjson(0,'操作失败');
  84. }
  85. }else{
  86. $rel = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
  87. empty($rel['card_id']) ? $view['card'] = [] : $view['card'] = Card::info($rel['card_id'],$this->member_miniapp_id);
  88. $view['info'] = $rel;
  89. return view()->assign($view);
  90. }
  91. }
  92. /**
  93. * 已绑定会员卡列表
  94. */
  95. public function storeIndex(int $id){
  96. $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->find();
  97. $view['lists'] = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id])->whereIn('id',$info->ids)->order('id desc')->paginate(20);
  98. $view['ecard_id'] = $info->id;
  99. return view()->assign($view);
  100. }
  101. /**
  102. * 好店列表
  103. */
  104. public function select(int $ecard_id){
  105. $condition = [];
  106. $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
  107. $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $ecard_id])->find();
  108. if(request()->isAjax()){
  109. $ids = $this->request->param('ids');
  110. if(empty($ids)){
  111. return enjson(0,'请选择要添加会员卡的好店');
  112. }
  113. if(!empty($info->ids)){
  114. $ids = $info->ids . ',' . substr($ids,0,strlen($ids) - 1);
  115. }else{
  116. $ids = substr($ids,0,strlen($ids) - 1);
  117. }
  118. $result = AllwinEcard::where($condition)->where(['id' => $ecard_id])->update(['ids' => $ids]);
  119. if($result){
  120. return enjson(200,'好店添加会员卡成功');
  121. }else{
  122. return enjson(0,'好店添加会员卡失败');
  123. }
  124. }else{
  125. $keyword = $this->request->param('keyword');
  126. if (!empty($keyword)) {
  127. $condition[] = ['name', 'like', '%' . $keyword . '%'];
  128. }
  129. $view['lists'] = AllwinStore::where($condition)->whereNotIn('id', $info->ids)->order('id desc')->paginate(20);
  130. $view['keyword'] = $keyword;
  131. $view['ecard_id'] = $ecard_id;
  132. return view()->assign($view);
  133. }
  134. }
  135. //删除
  136. public function delete(int $id){
  137. $info = AllwinEcard::where(['id' => $id])->find();
  138. $rel = Card::delete($info->card_id,$this->member_miniapp_id);
  139. if($rel['errcode'] == 0){
  140. $result = AllwinEcard::destroy($id);
  141. if($result){
  142. return enjson(200,'删除成功');
  143. }else{
  144. return enjson(403,'删除失败');
  145. }
  146. }else{
  147. return enjson(403,'删除失败');
  148. }
  149. }
  150. //从会员卡中删除好店
  151. public function deleteStore(int $id,$ecard_id){
  152. $info = AllwinEcard::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $ecard_id])->find();
  153. $info->ids = ids(array_values_unset($id, ids($info->ids, true)));
  154. $result = $info->save();
  155. if ($result) {
  156. return enjson(200, '删除成功');
  157. } else {
  158. return enjson(403, '删除失败');
  159. }
  160. }
  161. }