Goods.php 4.2 KB

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