Goods.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model\sharing;
  10. use app\common\model\sharing\Goods as GoodsModel;
  11. /**
  12. * 拼团商品模型
  13. * Class Goods
  14. * @package app\store\model\sharing
  15. */
  16. class Goods extends GoodsModel
  17. {
  18. /**
  19. * 添加商品
  20. * @param array $data
  21. * @return bool
  22. * @throws \think\exception\PDOException
  23. */
  24. public function add(array $data)
  25. {
  26. if (!isset($data['images']) || empty($data['images'])) {
  27. $this->error = '请上传商品图片';
  28. return false;
  29. }
  30. $data['content'] = isset($data['content']) ? $data['content'] : '';
  31. $data['wxapp_id'] = $data['sku']['wxapp_id'] = self::$wxapp_id;
  32. // 开启事务
  33. $this->startTrans();
  34. try {
  35. // 添加商品
  36. $this->allowField(true)->save($data);
  37. // 商品规格
  38. $this->addGoodsSpec($data);
  39. // 商品图片
  40. $this->addGoodsImages($data['images']);
  41. $this->commit();
  42. return true;
  43. } catch (\Exception $e) {
  44. $this->error = $e->getMessage();
  45. $this->rollback();
  46. return false;
  47. }
  48. }
  49. /**
  50. * 添加商品图片
  51. * @param $images
  52. * @return int
  53. * @throws \think\Exception
  54. * @throws \think\exception\PDOException
  55. */
  56. private function addGoodsImages($images)
  57. {
  58. $this->image()->delete();
  59. $data = array_map(function ($image_id) {
  60. return [
  61. 'image_id' => $image_id,
  62. 'wxapp_id' => self::$wxapp_id
  63. ];
  64. }, $images);
  65. return $this->image()->saveAll($data);
  66. }
  67. /**
  68. * 编辑商品
  69. * @param $data
  70. * @return bool
  71. * @throws \think\exception\PDOException
  72. */
  73. public function edit($data)
  74. {
  75. if (!isset($data['images']) || empty($data['images'])) {
  76. $this->error = '请上传商品图片';
  77. return false;
  78. }
  79. $data['content'] = isset($data['content']) ? $data['content'] : '';
  80. $data['wxapp_id'] = $data['sku']['wxapp_id'] = self::$wxapp_id;
  81. // 开启事务
  82. $this->startTrans();
  83. try {
  84. // 保存商品
  85. $this->allowField(true)->save($data);
  86. // 商品规格
  87. $this->addGoodsSpec($data, true);
  88. // 商品图片
  89. $this->addGoodsImages($data['images']);
  90. $this->commit();
  91. return true;
  92. } catch (\Exception $e) {
  93. $this->rollback();
  94. $this->error = $e->getMessage();
  95. return false;
  96. }
  97. }
  98. /**
  99. * 添加商品规格
  100. * @param $data
  101. * @param $isUpdate
  102. * @throws \Exception
  103. */
  104. private function addGoodsSpec($data, $isUpdate = false)
  105. {
  106. // 更新模式: 先删除所有规格
  107. $model = new GoodsSku;
  108. $isUpdate && $model->removeAll($this['goods_id']);
  109. // 添加规格数据
  110. if ($data['spec_type'] == '10') {
  111. // 单规格
  112. $this->sku()->save($data['sku']);
  113. } else if ($data['spec_type'] == '20') {
  114. // 添加商品与规格关系记录
  115. $model->addGoodsSpecRel($this['goods_id'], $data['spec_many']['spec_attr']);
  116. // 添加商品sku
  117. $model->addSkuList($this['goods_id'], $data['spec_many']['spec_list']);
  118. }
  119. }
  120. /**
  121. * 修改商品状态
  122. * @param $state
  123. * @return false|int
  124. */
  125. public function setStatus($state)
  126. {
  127. return $this->allowField(true)->save(['goods_status' => $state ? 10 : 20]) !== false;
  128. }
  129. /**
  130. * 软删除
  131. * @return false|int
  132. */
  133. public function setDelete()
  134. {
  135. return $this->allowField(true)->save(['is_delete' => 1]);
  136. }
  137. /**
  138. * 获取当前商品总数
  139. * @param array $where
  140. * @return int|string
  141. * @throws \think\Exception
  142. */
  143. public function getGoodsTotal($where = [])
  144. {
  145. return $this->where('is_delete', '=', 0)->where($where)->count();
  146. }
  147. }