StoreSeckillAttr.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/12/08
  5. */
  6. namespace app\admin\model\ump;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\traits\ModelTrait;
  9. class StoreSeckillAttr extends BaseModel
  10. {
  11. /**
  12. * 模型名称
  13. * @var string
  14. */
  15. protected $name = 'store_seckill_attr';
  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['pic']) || empty($value['pic']))
  53. return self::setErrorInfo('请上传商品图片');
  54. foreach ($value['detail'] as $attrName=>$attrValue){
  55. $attrName = trim($attrName);
  56. $attrValue = trim($attrValue);
  57. if(!in_array($attrName,$attrNameList,true)) return self::setErrorInfo($attrName.'规则不存在');
  58. if(!in_array($attrValue,$attrValueList,true)) return self::setErrorInfo($attrName.'属性不存在');
  59. if(empty($attrName)) return self::setErrorInfo('请输入正确的属性');
  60. $value['detail'][$attrName] = $attrValue;
  61. }
  62. $valueList[$index] = $value;
  63. }
  64. $attrGroup = [];
  65. $valueGroup = [];
  66. foreach ($attrList as $k=>$value){
  67. $attrGroup[] = [
  68. 'product_id'=>$productId,
  69. 'attr_name'=>$value['value'],
  70. 'attr_values'=>$value['detail']
  71. ];
  72. }
  73. foreach ($valueList as $k=>$value){
  74. // ksort($value['detail'],SORT_STRING);
  75. $suk = implode(',',$value['detail']);
  76. $valueGroup[$suk] = [
  77. 'product_id'=>$productId,
  78. 'suk'=>$suk,
  79. 'price'=>$value['price'],
  80. 'stock'=>$value['sales'],
  81. 'image'=>$value['pic']
  82. ];
  83. }
  84. if(!count($attrGroup) || !count($valueGroup)) return self::setErrorInfo('请设置至少一个属性!');
  85. $attrModel = new self;
  86. $attrValueModel = new StoreSeckillAttrValue;
  87. self::beginTrans();
  88. if(!self::clearProductAttr($productId)) return false;
  89. $res = false !== $attrModel->saveAll($attrGroup)
  90. && false !== $attrValueModel->saveAll($valueGroup)
  91. && false !== StoreSeckillAttrResult::setResult($result,$productId);
  92. self::checkTrans($res);
  93. if($res)
  94. return true;
  95. else
  96. return self::setErrorInfo('编辑商品属性失败!');
  97. }
  98. public static function clearProductAttr($productId)
  99. {
  100. if (empty($productId) && $productId != 0) return self::setErrorInfo('商品不存在!');
  101. $res = false !== self::where('product_id',$productId)->delete()
  102. && false !== StoreSeckillAttrValue::clearProductAttrValue($productId);
  103. if(!$res)
  104. return self::setErrorInfo('编辑属性失败,清除旧属性失败!');
  105. else
  106. return true;
  107. }
  108. }