Package.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 套餐管理
  8. */
  9. class Package extends Apic
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = '*';
  13. //列表
  14. public function lists(){
  15. $status = input('status',1);
  16. $keyword = input('keyword','');
  17. $where = [
  18. 'p.company_id' => $this->auth->company_id,
  19. 'p.status' => $status,
  20. ];
  21. if(!empty($keyword)){
  22. $where['p.title|p.info'] = ['LIKE','%'.$keyword.'%'];
  23. }
  24. $list = Db::name('package')->alias('p')
  25. ->field('p.*,type.title as servicetype_title')
  26. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  27. ->where($where)->order('p.id desc')->autopage()->select();
  28. //追加赠送
  29. if(!empty($list)){
  30. $package_ids = array_column($list,'id');
  31. $gift = Db::name('package_gift')->alias('gift')
  32. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  33. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  34. ->where('gift.package_id','IN',$package_ids)
  35. ->where('coupons.status',1)
  36. ->select();
  37. foreach($list as $key => &$val){
  38. $val['gift'] = [];
  39. foreach($gift as $k => $v){
  40. if($val['id'] == $v['package_id']){
  41. $val['gift'][] = $v;
  42. }
  43. }
  44. }
  45. }
  46. $list = list_domain_image($list,['images','content_images']);
  47. $this->success(1,$list);
  48. }
  49. //新增
  50. public function add(){
  51. Db::startTrans();
  52. try {
  53. //验证
  54. if($this->auth->type != 1){
  55. throw new Exception('只有门店老板才能设置');
  56. }
  57. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images','type'];
  58. $data = request_post_hub($field);
  59. $data['company_id'] = $this->auth->company_id;
  60. $data['createtime'] = time();
  61. $data['updatetime'] = time();
  62. $data['status'] = 1;
  63. $package_id = Db::name('package')->insertGetId($data);
  64. if (!$package_id) {
  65. throw new Exception('添加套餐失败');
  66. }
  67. if (isset($data['type']) && $data['type'] == 2) {
  68. $gift_data = input('gift_data','','trim');
  69. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  70. if(is_array($gift_data) && !empty($gift_data)){
  71. $package_gift = [];
  72. foreach($gift_data as $key => $val){
  73. $package_gift[] = [
  74. 'package_id' => $package_id,
  75. 'coupon_id' => $val['coupon_id'],
  76. 'number' => $val['number'],
  77. ];
  78. }
  79. if(!empty($package_gift)){
  80. $rs_gift = Db::name('package_gift')->insertAll($package_gift);
  81. if($rs_gift === false){
  82. Db::rollback();
  83. $this->error('添加失败');
  84. }
  85. }
  86. }
  87. }
  88. Db::commit();
  89. $this->success('添加成功');
  90. } catch (Exception $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. }
  94. }
  95. //上下架
  96. public function changestatus(){
  97. //验证
  98. if($this->auth->type != 1){
  99. $this->error('只有门店老板才能设置');
  100. }
  101. $id = input('id',0);
  102. $status = Db::name('package')->where('id',$id)->value('status');
  103. $new_status = $status == 1 ? 0 : 1;
  104. $info = Db::name('package')->where('id',$id)->update(['status'=>$new_status,'updatetime'=>time()]);
  105. $this->success();
  106. }
  107. //详情
  108. public function info(){
  109. $id = input('id',0);
  110. $info = Db::name('package')->alias('p')
  111. ->field('p.*,type.title as servicetype_title')
  112. ->join('servicetype type','p.servicetype_id = type.id','LEFT')
  113. ->where('p.id',$id)->find();
  114. //追加赠送
  115. if(!empty($info)){
  116. $gift = Db::name('package_gift')->alias('gift')
  117. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  118. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  119. ->where('gift.package_id',$id)
  120. ->where('coupons.status',1)
  121. ->select();
  122. $info['gift'] = [];
  123. if (!empty($gift)) {
  124. foreach($gift as $k => $v){
  125. $info['gift'][] = $v;
  126. }
  127. }
  128. }
  129. $info = info_domain_image($info,['images','content_images']);
  130. $this->success(1,$info);
  131. }
  132. //编辑
  133. public function edit(){
  134. Db::startTrans();
  135. try {
  136. //验证
  137. if($this->auth->type != 1){
  138. throw new Exception('只有门店老板才能设置');
  139. }
  140. $id = input('id','');
  141. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  142. if(empty($check)){
  143. throw new Exception('不存在的套餐');
  144. }
  145. //
  146. $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
  147. $data = request_post_hub($field);
  148. $data['updatetime'] = time();
  149. $packageRes = Db::name('package')->where('id',$id)->update($data);
  150. if (!$packageRes) {
  151. throw new Exception('套餐编辑失败');
  152. }
  153. if ($check['type'] == 2) {
  154. $packageGiftWhere['package_id'] = $id;
  155. $packageGiftData = Db::name('package_gift')->where($packageGiftWhere)->select();
  156. $gift_data = input('gift_data','','trim');
  157. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  158. if(is_array($gift_data) && !empty($gift_data)) {
  159. $packageGiftCouponIds = array_column($packageGiftData,'coupon_id');
  160. $postCouponIds = array_column($gift_data,'coupon_id');
  161. $package_gift = [];
  162. $same = [];
  163. $different = [];
  164. foreach ($gift_data as $key => $val) {
  165. if (in_array($val['coupon_id'], $packageGiftCouponIds)) {
  166. $same[] = [
  167. 'package_id' => $id,
  168. 'coupon_id' => $val['coupon_id'],
  169. 'number' => $val['number'],
  170. ];
  171. } else {
  172. $different[] = [
  173. 'package_id' => $id,
  174. 'coupon_id' => $val['coupon_id'],
  175. 'number' => $val['number'],
  176. ];
  177. }
  178. }
  179. if (!empty($same)) {//相同的更新
  180. foreach ($same as $sameKey => $sameVal) {
  181. $sameWhere['package_id'] = $sameVal['package_id'];
  182. $sameWhere['coupon_id'] = $sameVal['coupon_id'];
  183. $sameData['number'] = $sameVal['number'];
  184. Db::name('package_gift')->where($sameWhere)->update($sameData);
  185. }
  186. }
  187. if (!empty($different)) {//新增的添加
  188. $packageGiftRes = Db::name('package_gift')->where($sameWhere)->insertAll($different);
  189. if (!$packageGiftRes) {
  190. throw new Exception('套餐卡券添加失败');
  191. }
  192. }
  193. //多余的删除
  194. $delIds = array_diff($packageGiftCouponIds,$postCouponIds);
  195. if (!empty($delIds)) {
  196. $packageGiftDelWhere['package_id'] = $id;
  197. $packageGiftDelWhere['coupon_id'] = ['in',$delIds];
  198. Db::name('package_gift')->where($packageGiftDelWhere)->delete();
  199. }
  200. } else {
  201. if (!empty($packageGiftData)) {
  202. $packageGiftDelRes = Db::name('package_gift')->where($packageGiftWhere)->delete();
  203. if (!$packageGiftDelRes) {
  204. throw new Exception('删除卡券失败');
  205. }
  206. }
  207. }
  208. }
  209. Db::commit();
  210. $this->success('编辑成功');
  211. } catch (Exception $e) {
  212. Db::rollback();
  213. $this->error($e->getMessage());
  214. }
  215. }
  216. //删除
  217. public function delete(){
  218. //验证
  219. if($this->auth->type != 1){
  220. $this->error('只有门店老板才能设置');
  221. }
  222. $id = input('id','');
  223. $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  224. if(empty($check)){
  225. $this->error('不存在的套餐');
  226. }
  227. Db::name('package')->where('id',$id)->delete();
  228. Db::name('package_gift')->where('package_id',$id)->delete();
  229. $this->success('删除成功');
  230. }
  231. }