StoreProductAttr.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\store;
  12. use basic\ModelBasic;
  13. use traits\ModelTrait;
  14. class StoreProductAttr extends ModelBasic
  15. {
  16. use ModelTrait;
  17. protected function setAttrValuesAttr($value)
  18. {
  19. return is_array($value) ? implode(',',$value) : $value;
  20. }
  21. protected function getAttrValuesAttr($value)
  22. {
  23. return explode(',',$value);
  24. }
  25. public static function createProductAttr($attrList,$valueList,$productId)
  26. {
  27. $result = ['attr'=>$attrList,'value'=>$valueList];
  28. $attrValueList = [];
  29. $attrNameList = [];
  30. foreach ($attrList as $index=>$attr){
  31. if(!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!');
  32. $attr['value'] = trim($attr['value']);
  33. if(!isset($attr['value'])) return self::setErrorInfo('请输入规则名称!!');
  34. if(!isset($attr['detail']) || !count($attr['detail'])) return self::setErrorInfo('请输入属性名称!');
  35. foreach ($attr['detail'] as $k=>$attrValue){
  36. $attrValue = trim($attrValue);
  37. if(empty($attrValue)) return self::setErrorInfo('请输入正确的属性');
  38. $attr['detail'][$k] = $attrValue;
  39. $attrValueList[] = $attrValue;
  40. $attr['detail'][$k] = $attrValue;
  41. }
  42. $attrNameList[] = $attr['value'];
  43. $attrList[$index] = $attr;
  44. }
  45. $attrCount = count($attrList);
  46. foreach ($valueList as $index=>$value){
  47. if(!isset($value['detail']) || count($value['detail']) != $attrCount) return self::setErrorInfo('请填写正确的商品信息');
  48. if(!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price'])
  49. return self::setErrorInfo('请填写正确的商品价格');
  50. if(!isset($value['sales']) || !is_numeric($value['sales']) || intval($value['sales']) != $value['sales'])
  51. return self::setErrorInfo('请填写正确的商品库存');
  52. if(!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])
  53. return self::setErrorInfo('请填写正确的商品成本价格');
  54. if(!isset($value['pic']) || empty($value['pic']))
  55. return self::setErrorInfo('请上传商品图片');
  56. foreach ($value['detail'] as $attrName=>$attrValue){
  57. $attrName = trim($attrName);
  58. $attrValue = trim($attrValue);
  59. if(!in_array($attrName,$attrNameList,true)) return self::setErrorInfo($attrName.'规则不存在');
  60. if(!in_array($attrValue,$attrValueList,true)) return self::setErrorInfo($attrName.'属性不存在');
  61. if(empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  62. $value['detail'][$attrName] = $attrValue;
  63. }
  64. $valueList[$index] = $value;
  65. }
  66. $attrGroup = [];
  67. $valueGroup = [];
  68. foreach ($attrList as $k=>$value){
  69. $attrGroup[] = [
  70. 'product_id'=>$productId,
  71. 'attr_name'=>$value['value'],
  72. 'attr_values'=>$value['detail']
  73. ];
  74. }
  75. foreach ($valueList as $k=>$value){
  76. ksort($value['detail'],SORT_STRING);
  77. $suk = implode(',',$value['detail']);
  78. $valueGroup[$suk] = [
  79. 'product_id'=>$productId,
  80. 'suk'=>$suk,
  81. 'price'=>$value['price'],
  82. 'vip_price'=>$value['vip_price'],
  83. 'cost'=>$value['cost'],
  84. 'stock'=>$value['sales'],
  85. 'image'=>$value['pic']
  86. ];
  87. }
  88. if(!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  89. $attrModel = new self;
  90. $attrValueModel = new StoreProductAttrValue;
  91. self::beginTrans();
  92. if(!self::clearProductAttr($productId)) return false;
  93. $res = false !== $attrModel->saveAll($attrGroup)
  94. && false !== $attrValueModel->saveAll($valueGroup)
  95. && false !== StoreProductAttrResult::setResult($result,$productId);
  96. self::checkTrans($res);
  97. if($res)
  98. return true;
  99. else
  100. return self::setErrorInfo('编辑商品属性失败!');
  101. }
  102. public static function clearProductAttr($productId)
  103. {
  104. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  105. $res = false !== self::where('product_id',$productId)->delete()
  106. && false !== StoreProductAttrValue::clearProductAttrValue($productId);
  107. if(!$res)
  108. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  109. else
  110. return true;
  111. }
  112. }