GoodsStyleClass.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace biz\goods\classes;
  3. use common\components\business;
  4. use Yii;
  5. use biz\base\classes\BaseClass;
  6. class GoodsStyleClass extends BaseClass
  7. {
  8. public static $baseFile = '\biz\goods\models\GoodsStyle';
  9. //组装下款式需要用的信息 shish 2019.12.4
  10. public static function groupStyleInfo($list)
  11. {
  12. foreach ($list as $key => $val) {
  13. //大中小图片转化
  14. $val = business::formatGoodsStyleImg($val);
  15. $list[$key] = $val;
  16. }
  17. return $list;
  18. }
  19. //取商品的款式和价格
  20. public static function getStyleList($id)
  21. {
  22. $list = self::getAllByCondition(['goodsId' => $id], 'inTurn DESC', '*', 'id');
  23. if (empty($list)) {
  24. return $list;
  25. }
  26. $new = self::groupStyleInfo($list);
  27. return $new;
  28. }
  29. /*
  30. 更新商品的款式 shish 2020.2.27
  31. 输入的数据格式 [
  32. ['styleName' => '红色1', 'price' => 0.1, 'picture' => '', 'action' => 'add', 'id' => 0],
  33. ['styleName' => '蓝色', 'price' => 0.1, 'picture' => '', 'action' => 'update', 'id' => 1],
  34. ['styleName' => '黑色2', 'price' => 0.1, 'picture' => '', 'action' => 'delete', 'id' => 2]
  35. ];
  36. */
  37. public static function updateGoodsStyle($goodsStyleList, $goodsId)
  38. {
  39. $add = [];
  40. $update = [];
  41. $delete = [];
  42. foreach ($goodsStyleList as $goodsStyle) {
  43. $action = $goodsStyle['action'];
  44. $id = $goodsStyle['id'];
  45. switch ($action) {
  46. case 'add':
  47. unset($goodsStyle['id']);
  48. $add[] = $goodsStyle;
  49. break;
  50. case 'update':
  51. $update[] = $goodsStyle;
  52. break;
  53. case 'delete':
  54. $delete[] = $id;
  55. break;
  56. default:
  57. $add[] = $goodsStyle;
  58. }
  59. }
  60. if (!empty($add)) {
  61. $addStyle = [];
  62. foreach ($add as $style) {
  63. $addStyle[] = ['styleName' => $style['styleName'], 'goodsId' => $goodsId, 'picture' => $style['picture'], 'price' => $style['price']];
  64. }
  65. GoodsStyleClass::batchAdd($addStyle);
  66. }
  67. if (!empty($delete)) {
  68. GoodsStyleClass::deleteByIds($delete);
  69. }
  70. if (!empty($update)) {
  71. foreach ($update as $up) {
  72. $currentId = $up['id'];
  73. unset($up['id']);
  74. self::updateById($currentId, $up);
  75. }
  76. }
  77. }
  78. }