Share.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * 商品分享权限关联表数据操作
  4. * Created by PhpStorm.
  5. * Author: 芸众商城 www.yunzshop.com
  6. * Date: 2017/2/24
  7. * Time: 下午2:31
  8. */
  9. namespace app\backend\modules\goods\models;
  10. use app\common\traits\MessageTrait;
  11. class Share extends \app\common\models\goods\Share
  12. {
  13. static protected $needLog = true;
  14. use MessageTrait;
  15. public $timestamps = true;
  16. /**
  17. * 获取商品分享关注数据
  18. * @param int $goodsId
  19. * @return array
  20. */
  21. public static function getInfo($goodsId)
  22. {
  23. return self::getGoodsShareInfo($goodsId);
  24. }
  25. public static function relationSave($goodsId, $data, $operate)
  26. {
  27. if(!$goodsId){
  28. return false;
  29. }
  30. if (!$data) {
  31. return false;
  32. }
  33. $shareModel = self::getModel($goodsId, $operate);
  34. //判断deleted
  35. if ($operate == 'deleted') {
  36. return $shareModel->delete();
  37. }
  38. $data['goods_id'] = $goodsId;
  39. $shareModel->setRawAttributes($data);
  40. return $shareModel->save();
  41. }
  42. public static function relationValidator($goodsId, $data, $operate)
  43. {
  44. $flag = false;
  45. $model = new static;
  46. $validator = $model->validator($data);
  47. if($validator->fails()){
  48. $model->error($validator->messages());
  49. }else{
  50. $flag = true;
  51. }
  52. return $flag;
  53. }
  54. public static function getModel($goodsId,$operate)
  55. {
  56. $model = false;
  57. if($operate != 'created') {
  58. $model = static::where(['goods_id' => $goodsId])->first();
  59. }
  60. !$model && $model = new static;
  61. return $model;
  62. }
  63. /**
  64. * 商品分享关注数据添加
  65. * @param array $shareInfo
  66. * @return bool
  67. */
  68. public static function createdShare($shareInfo)
  69. {
  70. return self::insert($shareInfo);
  71. }
  72. /**
  73. * 商品分享关注数据更新
  74. * @param array $shareInfo
  75. * @return mixed
  76. */
  77. public static function updatedShare($goodsId, $shareInfo)
  78. {
  79. return self::where('goods_id', $goodsId)->update($shareInfo);
  80. }
  81. /**
  82. * 商品分享关注数据删除
  83. * @param int $goodsId
  84. * @return mixed
  85. */
  86. public static function deletedShare($goodsId)
  87. {
  88. return self::where('goods_id', $goodsId)->delete();
  89. }
  90. }