GoodsModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * GoodsModel.php UTF-8
  4. * 商品表
  5. *
  6. * @date : 2018/5/7 18:35
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\shop;
  13. use huo\model\common\CommonModel;
  14. class GoodsModel extends CommonModel {
  15. protected $name = 'goods';
  16. //设置只读字段
  17. protected $readonly = ['admin_id'];
  18. // 开启自动写入时间戳字段
  19. protected $autoWriteTimestamp = true;
  20. /**
  21. * 基础查询
  22. * 上线未删除
  23. *
  24. * @param $query
  25. */
  26. protected function base($query) {
  27. $query->where('delete_time', 0)
  28. ->where('is_on_sale', 2);
  29. }
  30. /**
  31. * goods_content 自动转化
  32. *
  33. * @param $value
  34. *
  35. * @return string
  36. */
  37. public function getGoodsContentAttr($value) {
  38. return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  39. }
  40. /**
  41. * @param $value
  42. *
  43. * @return string
  44. */
  45. public function getOriginalImgAttr($value) {
  46. if (!empty($value)) {
  47. return cmf_get_image_url($value);
  48. }
  49. return $value;
  50. }
  51. /**
  52. * image 自动转化
  53. *
  54. *
  55. * @param $value
  56. *
  57. * @return array
  58. */
  59. public function setOriginalImgAttr($value) {
  60. if (!empty($value)) {
  61. return str_replace(STATICSITE.'/upload/', '', $value);
  62. }
  63. return $value;
  64. }
  65. /**
  66. * goods_content 自动转化
  67. *
  68. * @param $value
  69. *
  70. * @return string
  71. */
  72. public function setGoodsContentAttr($value) {
  73. return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
  74. }
  75. /**
  76. * 添加商品
  77. *
  78. * @param $data
  79. *
  80. * @return bool|mixed
  81. */
  82. public function addGoods($data) {
  83. if (empty($data)) {
  84. return false;
  85. }
  86. if ($_obj = self::create($data, true)) {
  87. return $_obj->id;
  88. } else {
  89. return false;
  90. }
  91. }
  92. /**
  93. * 更新商品
  94. *
  95. * @param array $goods_data
  96. * @param string $goods_id
  97. *
  98. * @return bool
  99. */
  100. public function updateGoods($goods_data, $goods_id) {
  101. $_map['id'] = $goods_id;
  102. $_data = $goods_data;
  103. $_rs = self::update($_data, $_map, true);
  104. if (false == $_rs) {
  105. return false;
  106. } else {
  107. return true;
  108. }
  109. }
  110. }