You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Goods;
  5. use App\Http\Requests\GoodsRequest;
  6. use Validator;
  7. use App\Http\Model\Comment;
  8. class GoodsLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return model('Goods');
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new GoodsRequest();
  22. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  23. }
  24. //列表
  25. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  26. {
  27. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  28. if($res['count'] > 0)
  29. {
  30. foreach($res['list'] as $k=>$v)
  31. {
  32. $res['list'][$k] = $this->getDataView($v);
  33. $res['list'][$k]->price = $this->getModel()->get_goods_final_price($v);
  34. $res['list'][$k]->is_promote_goods = $this->getModel()->bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  35. }
  36. }
  37. return $res;
  38. }
  39. //分页html
  40. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  41. {
  42. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  43. return $res;
  44. }
  45. //全部列表
  46. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  47. {
  48. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  49. if($res['count'] > 0)
  50. {
  51. foreach($res['list'] as $k=>$v)
  52. {
  53. $res['list'][$k] = $this->getDataView($v);
  54. $res['list'][$k]->price = $this->getModel()->get_goods_final_price($v); //商品最终价格
  55. $res['list'][$k]->is_promote_goods = $this->getModel()->bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  56. }
  57. }
  58. return $res;
  59. }
  60. //详情
  61. public function getOne($where = array(), $field = '*')
  62. {
  63. $res = $this->getModel()->getOne($where, $field);
  64. if(!$res){return false;}
  65. $res = $this->getDataView($res);
  66. $res->price = $this->getModel()->get_goods_final_price($res); //商品最终价格
  67. $res->is_promote_goods = $this->getModel()->bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  68. //商品评论数
  69. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  70. $where2['status'] = Comment::SHOW_COMMENT;
  71. $where2['id_value'] = $res->id;
  72. $res->goods_comments_num = model('Comment')->getCount($where2);
  73. $this->getModel()->getDb()->where($where)->increment('click', 1); //点击量+1
  74. return $res;
  75. }
  76. //添加
  77. public function add($data = array(), $type=0)
  78. {
  79. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  80. $validator = $this->getValidate($data, 'add');
  81. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  82. $res = $this->getModel()->add($data,$type);
  83. if($res === false){return ReturnData::create(ReturnData::SYSTEM_FAIL);}
  84. return ReturnData::create(ReturnData::SUCCESS,$res);
  85. }
  86. //修改
  87. public function edit($data, $where = array())
  88. {
  89. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  90. $validator = $this->getValidate($data, 'edit');
  91. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  92. $res = $this->getModel()->edit($data,$where);
  93. if($res === false){return ReturnData::create(ReturnData::SYSTEM_FAIL);}
  94. return ReturnData::create(ReturnData::SUCCESS,$res);
  95. }
  96. //删除
  97. public function del($where)
  98. {
  99. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  100. $validator = $this->getValidate($where,'del');
  101. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  102. $res = $this->getModel()->del($where);
  103. if($res === false){return ReturnData::create(ReturnData::SYSTEM_FAIL);}
  104. return ReturnData::create(ReturnData::SUCCESS,$res);
  105. }
  106. /**
  107. * 数据获取器
  108. * @param array $data 要转化的数据
  109. * @return array
  110. */
  111. private function getDataView($data = array())
  112. {
  113. return getDataAttr($this->getModel(),$data);
  114. }
  115. }