Notice.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\backend\modules\goods\models;
  3. use app\common\traits\MessageTrait;
  4. /**
  5. * Created by PhpStorm.
  6. * Author: 芸众商城 www.yunzshop.com
  7. * Date: 2017/2/27
  8. * Time: 上午9:18
  9. */
  10. class Notice extends \app\common\models\Notice
  11. {
  12. static protected $needLog = true;
  13. use MessageTrait;
  14. public $timestamps = false;
  15. /**
  16. * @param $goodsId
  17. * @return \Illuminate\Database\Eloquent\Model|null|static
  18. */
  19. public static function getInfo($goodsId)
  20. {
  21. return self::where('goods_id', $goodsId)
  22. ->first();
  23. }
  24. /**
  25. * @param $notices
  26. * @return mixed
  27. */
  28. public static function createdNotices($notices)
  29. {
  30. return self::insert($notices);
  31. }
  32. /**
  33. * @param $goodsId
  34. * @param $notices
  35. * @return mixed
  36. */
  37. public static function updatedNotices($goodsId, $notices)
  38. {
  39. return self::where('goods_id', $goodsId)->update($notices);
  40. }
  41. /**
  42. * @param $goodsId
  43. * @return mixed
  44. */
  45. public static function deletedNotices($goodsId)
  46. {
  47. return self::where('goods_id', $goodsId)->delete();
  48. }
  49. public static function getList($goodsId)
  50. {
  51. return self::where('goods_id',$goodsId)
  52. ->get();
  53. }
  54. public static function relationSave($goodsId, $data, $operate = '')
  55. {
  56. if(!$goodsId){
  57. return false;
  58. }
  59. if (!$data) {
  60. return false;
  61. }
  62. self::deleteAllByGoodsId($goodsId);
  63. if(!$data['uid'] || !isset($data['type'])){
  64. return false;
  65. }
  66. $noticesData = [
  67. 'goods_id' => $goodsId,
  68. 'uid' => $data['uid']
  69. ];
  70. return self::addByGoodsId($data, $noticesData);
  71. }
  72. public static function relationValidator($goodsId, $data, $operate)
  73. {
  74. $flag = false;
  75. $model = new static;
  76. $validator = $model->validator($data);
  77. if($validator->fails()){
  78. $model->error($validator->messages());
  79. }else{
  80. $flag = true;
  81. }
  82. return $flag;
  83. }
  84. public static function deleteAllByGoodsId($goodsId)
  85. {
  86. return static::where('goods_id', $goodsId)
  87. ->delete();
  88. }
  89. public static function addByGoodsId($data,$noticesData)
  90. {
  91. foreach ($data['type'] as $type) {
  92. $saleModel = new static;
  93. $noticesData['type'] = $type;
  94. $saleModel->setRawAttributes($noticesData);
  95. $saleModel->save();
  96. }
  97. return true;
  98. }
  99. }