Goods.php 4.0 KB

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