MdGodsForm.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * MdGodsForm.php
  4. * todo 文件描述
  5. * Created on 2024/4/10 09:22
  6. * @author: hankaige
  7. */
  8. namespace app\modules\client\models\v1;
  9. use app\models\Goods;
  10. use app\models\MdGoods;
  11. use app\modules\client\models\ApiModel;
  12. class MdGodsForm extends ApiModel
  13. {
  14. public $store_id;
  15. public $md_id;
  16. public $keyword;
  17. public $status;//状态字段 -1全部 1售卖中 0已下架 2已售空
  18. public $ids;// 要修改的商品ID
  19. public $id;// 单独修改商品价格与库存
  20. public $goods_price;// 默认规格商品价格
  21. public $goods_num;// 默认规格商品库存
  22. public $attr;// 多规格规格数据
  23. public function rules()
  24. {
  25. return [
  26. [
  27. [
  28. 'md_id',
  29. 'store_id'
  30. ],
  31. 'integer'
  32. ],
  33. [
  34. ['md_id'],
  35. 'required'
  36. ],
  37. ];
  38. }
  39. public function getGoodsList(): array
  40. {
  41. if (!$this->validate()) {
  42. return [
  43. 'code' => 1,
  44. 'msg' => $this->getErrorSummary(FALSE)[0]
  45. ];
  46. }
  47. $query = MdGoods::find()->alias('mg')->leftJoin(['g' => Goods::tableName()], 'mg.goods_id=g.id')->where([
  48. 'mg.md_id' => $this->md_id,
  49. 'g.is_delete' => 0,
  50. 'g.store_id' => $this->store_id,
  51. 'g.status' => Goods::STATUS_NORMAL
  52. ])->select([
  53. 'g.name',
  54. 'g.id',
  55. 'mg.virtual_sales',
  56. 'mg.goods_num',
  57. 'md_price' => 'mg.price',
  58. 'g.price',
  59. 'mg.status',
  60. 'g.cover_pic',
  61. 'g.use_attr'
  62. ]);
  63. if (!empty($this->keyword)) {
  64. $query->andWhere([
  65. 'like',
  66. 'g.name',
  67. $this->keyword
  68. ]);
  69. }
  70. switch ($this->status) {
  71. case 1:
  72. $query->andWhere(['mg.status' => 1]);
  73. break;
  74. case 0:
  75. $query->andWhere(['mg.status' => 0]);
  76. break;
  77. case 2:
  78. $query->andWhere(['mg.goods_num' => 0]);
  79. break;
  80. default:
  81. break;
  82. }
  83. $result = pagination_make($query, TRUE, 'mg.created_at DESC');
  84. foreach($result['list'] as &$item){
  85. $item['checked'] = 0;
  86. }
  87. return [
  88. 'code' => 0,
  89. 'msg' => 'ok',
  90. 'data' => $result
  91. ];
  92. }
  93. // 批量修改门店商品上下架状态
  94. public function batchUpdateStatus(): array
  95. {
  96. if (!$this->validate()) {
  97. return [
  98. 'code' => 1,
  99. 'msg' => $this->getErrorSummary(FALSE)[0]
  100. ];
  101. }
  102. if (empty($this->ids)) {
  103. return [
  104. 'code' => 1,
  105. 'msg' => '请选择要修改的商品'
  106. ];
  107. }
  108. if(!strstr($this->ids,',')){
  109. $ids = $this->ids;
  110. }else{
  111. $ids = explode(',', $this->ids);
  112. }
  113. $res = MdGoods::updateAll(['status' => $this->status], ['goods_id' => $ids]);
  114. if ($res > 0) {
  115. return [
  116. 'code' => 0,
  117. 'msg' => '修改成功',
  118. ];
  119. }
  120. return [
  121. 'code' => 1,
  122. 'msg' => '没有找到要修改的数据',
  123. ];
  124. }
  125. public function getGoodsAttr()
  126. {
  127. try {
  128. $goods = MdGoods::find()->where([
  129. 'md_id' => $this->md_id,
  130. 'goods_id' => $this->id,
  131. ])->select('id, attr')->one();
  132. $baseGoods = Goods::findOne($goods->goods_id);
  133. if (empty($goods->attr) || empty($goods)) {
  134. throw new \Exception("获取商品规格库存失败");
  135. }
  136. $attr = !empty($goods->attr) ? json_decode($goods->attr, TRUE) : [];
  137. $getAttrGroupList = $goods->getAttrGroupList($baseGoods->use_attr);
  138. $getAttrGroupList = json_decode(json_encode($getAttrGroupList), TRUE);
  139. $title = NULL;
  140. if (!empty($getAttrGroupList)) {
  141. $title = $getAttrGroupList[0]['attr_list'];
  142. }
  143. $arr = [];
  144. if (!empty($title) && !empty($attr)) {
  145. foreach ($title as $index => $item) {
  146. foreach ($attr as $at) {
  147. if (!empty($at)) {
  148. foreach ($at['attr_list'] as $al) {
  149. if ($item['attr_id'] === $al['attr_id']) {
  150. $arr[$index]['list'][] = $at;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. return [
  158. 'code' => 0,
  159. 'msg' => "获取成功",
  160. 'data' => [
  161. 'attr' => $attr,
  162. 'attr_group' => $arr,
  163. 'title' => $title
  164. ]
  165. ];
  166. } catch (\Exception $e) {
  167. return [
  168. 'code' => 1,
  169. 'msg' => $e->getMessage()
  170. ];
  171. }
  172. }
  173. public function setPrice()
  174. {
  175. try {
  176. $goods = MdGoods::find()->where(['md_id' => $this->md_id])->andWhere(['goods_id' => $this->id])->one();
  177. $baseGoods = Goods::findOne($this->id);
  178. if (empty($goods) || empty($baseGoods)) {
  179. throw new \Exception("获取产品信息失败");
  180. }
  181. //判断是否存在以及开启规格
  182. if ($baseGoods->use_attr == 1 && !empty($this->attr)) {
  183. // 传过来的规格信息
  184. $attr = $this->attr;
  185. $goodsAttr = json_decode($goods['attr'], TRUE);
  186. $goodsNum = 0;
  187. foreach ($attr as $item) {
  188. // 去匹配已经存在的规格信息
  189. $attrIds = array_column($item['attr_list'], 'attr_id');
  190. sort($attrIds);
  191. foreach ($goodsAttr as &$goodsAttrItem) {
  192. $goodsAttrId = array_column($goodsAttrItem['attr_list'], 'attr_id');
  193. sort($goodsAttrId);
  194. if (!array_diff($attrIds, $goodsAttrId)) {
  195. $goodsAttrItem['price'] = $item['price'];
  196. $goodsAttrItem['num'] = $item['num'];
  197. $goodsNum += $goodsAttrItem['num'];
  198. }
  199. }
  200. }
  201. $goods->attr = json_encode($goodsAttr);
  202. // 同步最新的库存
  203. $goods->goods_num = $goodsNum;
  204. // 同步最新的价格
  205. $goods->price = min(array_column($goodsAttr, 'price'));
  206. } elseif ($baseGoods->use_attr == 0) {
  207. $attr = json_decode($goods->attr, true);
  208. $attr[0]['price'] = $this->goods_price;
  209. $attr[0]['num'] = $this->goods_num;
  210. $goods->attr = json_encode($attr);
  211. $goods->price = $this->goods_price;
  212. //如果库存不足,则下架处理
  213. if ($this->goods_num == 0) {
  214. $goods->status = 0;
  215. }
  216. }
  217. if (!$goods->save()) {
  218. throw new \Exception('数据保存错误' . json_encode($goods->errors));
  219. }
  220. return [
  221. 'code' => 0,
  222. 'msg' => '修改成功'
  223. ];
  224. } catch (\Exception $e) {
  225. return [
  226. 'code' => 1,
  227. 'msg' => $e->getMessage()
  228. ];
  229. }
  230. }
  231. }