GiftPackage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\shop\model\plus\giftpackage;
  3. use app\common\model\plus\giftpackage\GiftPackage as GiftPackageModel;
  4. use app\shop\model\plus\coupon\Coupon;
  5. use app\shop\model\product\Product;
  6. use app\shop\model\plus\giftpackage\Code as CodeModel;
  7. /**
  8. * 礼包购模型
  9. */
  10. class GiftPackage extends GiftPackageModel
  11. {
  12. /**
  13. * 礼包列表
  14. * @param $data
  15. */
  16. public function getList($data)
  17. {
  18. $model = $this;
  19. //检索活动名称
  20. if ($data['search'] != '') {
  21. $model = $model->where('name', 'like', '%' . trim($data['search']) . '%');
  22. }
  23. return $model->where('is_delete', '=', 0)
  24. ->order('create_time', 'desc')
  25. ->paginate($data);
  26. }
  27. /**
  28. * 获取未结束的数据列表
  29. * 单码
  30. */
  31. public function getDatas()
  32. {
  33. return $this->where('status', '=', 0)
  34. ->where('code_type', '=', 10)
  35. ->where('end_time', '>', time())
  36. ->select();
  37. }
  38. /**
  39. * 获取数据
  40. * @param $id
  41. */
  42. public function getGiftPackage($id)
  43. {
  44. $data = $this->where('gift_package_id', '=', $id)->with(['image'])->find();
  45. $data = $data->toArray();
  46. $data['value1'][] = $data['start_time']['text'];
  47. $data['value1'][] = $data['end_time']['text'];
  48. $data['grade_ids'] = explode(',', $data['grade_ids']);
  49. if ($data['is_coupon'] == '1') {
  50. $data['is_coupon'] = true;
  51. }
  52. if ($data['is_product'] == '1') {
  53. $data['is_product'] = true;
  54. }
  55. if ($data['is_point'] == '1') {
  56. $data['is_point'] = true;
  57. }
  58. if ($data['coupon_ids'] != '') {
  59. $model = new Coupon();
  60. $coupon = json_decode($data['coupon_ids'],true);
  61. foreach ($coupon as $key => &$value) {
  62. $couponInfo = $model->getCouponInfo($value['coupon_id']);
  63. $value['name'] = $couponInfo['name'];
  64. }
  65. $data['coupon_list'] = $coupon;
  66. $data['coupon'] = explode(',', $data['coupon_ids']);
  67. }
  68. if ($data['product_ids'] != '') {
  69. $ProductModel = new Product();
  70. $product = $ProductModel->getProduct($data['product_ids']);
  71. $data['product_list'] = $product->toArray();
  72. $data['product'] = explode(',', $data['product_ids']);
  73. }
  74. return $data;
  75. }
  76. /**
  77. * 保存数据
  78. * @param $data
  79. */
  80. public function saveGift($data)
  81. {
  82. $this->startTrans();
  83. try {
  84. $this->buildData($data);
  85. $data['app_id'] = self::$app_id;
  86. // 保存主表
  87. $this->save($data);
  88. // 保存码
  89. (new CodeModel())->geneCode($this['gift_package_id'], $this['code_type'], $this['total_num']);
  90. $this->commit();
  91. return true;
  92. } catch (\Exception $e) {
  93. $this->error = $e->getMessage();
  94. $this->rollback();
  95. return false;
  96. }
  97. }
  98. /**
  99. * 修改
  100. * @param $value
  101. */
  102. public function edit($data)
  103. {
  104. $this->startTrans();
  105. try {
  106. $this->buildData($data);
  107. // 一批一码
  108. if($this['code_type'] == 10){
  109. $data['total_num'] = $this['total_num'] + $data['add_num'];
  110. }
  111. // 保存主表
  112. $this->save($data);
  113. // 保存码
  114. if($this['code_type'] == 20) {
  115. (new CodeModel())->geneCode($this['gift_package_id'], $this['code_type'], $this['add_num']);
  116. }
  117. $this->commit();
  118. return true;
  119. } catch (\Exception $e) {
  120. $this->error = $e->getMessage();
  121. $this->rollback();
  122. return false;
  123. }
  124. }
  125. /**
  126. * 构造数据
  127. */
  128. private function buildData(&$data){
  129. // 优惠券
  130. if ($data['is_coupon'] == 'true') {
  131. $data['is_coupon'] = 1;
  132. $data['coupon_ids'] = $data['coupon'];
  133. } else {
  134. $data['is_coupon'] = 0;
  135. $data['coupon_ids'] = '';
  136. }
  137. // 商品
  138. if ($data['is_product'] == 'true') {
  139. $data['is_product'] = 1;
  140. $data['product_ids'] = implode(',', array_unique($data['product']));
  141. } else {
  142. $data['is_product'] = 0;
  143. $data['product_ids'] = '';
  144. }
  145. // 积分
  146. if ($data['is_point'] == 'true') {
  147. $data['is_point'] = 1;
  148. } else {
  149. $data['is_point'] = 0;
  150. }
  151. // 等级限制
  152. if ($data['is_grade'] == 1) {
  153. $data['grade_ids'] = implode(',', $data['grade_ids']);
  154. } else {
  155. $data['grade_ids'] = '';
  156. }
  157. // 购买次数
  158. if ($data['is_times'] == 0) {
  159. $data['times'] = 0;
  160. }
  161. $data['start_time'] = strtotime(array_shift($data['value1']));
  162. $data['end_time'] = strtotime(array_pop($data['value1']));
  163. }
  164. /**
  165. * 发布
  166. */
  167. public function send($id)
  168. {
  169. return $this->where('gift_package_id', '=', $id)->update(['status' => 0]);
  170. }
  171. /**
  172. * 终止
  173. */
  174. public function end($id)
  175. {
  176. return $this->where('gift_package_id', '=', $id)->update(['status' => 1]);
  177. }
  178. /**
  179. * 删除
  180. * @param $id
  181. */
  182. public function del($id)
  183. {
  184. return $this->where('gift_package_id', '=', $id)->update(['is_delete' => 1]);
  185. }
  186. }