Goods.php 4.1 KB

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