Goods.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. namespace app\admin\model;
  8. use think\model;
  9. class Goods extends Model
  10. {
  11. public static $Type = ['普通','精华','热门','推荐'];
  12. //插入关键字
  13. public function insertKeyword($keywordArray = [], $aid = 0)
  14. {
  15. $insert = [];
  16. $time = time();
  17. foreach ($keywordArray as $key => $value) {
  18. if (!$value) {
  19. continue;
  20. }
  21. $keywords_id = (new Keywords())->increase($value);
  22. $insert[] = ['aid' => $aid,
  23. 'keywords_id' => $keywords_id,
  24. 'create_time' => $time,
  25. ];
  26. }
  27. $res = \think\facade\Db::name('GoodsKeywords')->strict(false)->field(true)->insertAll($insert);
  28. return $res;
  29. }
  30. /**
  31. * 获取分页列表
  32. * @param $where
  33. * @param $param
  34. */
  35. public function getGoodsList($where, $param)
  36. {
  37. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  38. $order = empty($param['order']) ? 'a.id desc' : $param['order'];
  39. $list = self::where($where)
  40. ->field('a.*,c.title as cate_title,u.nickname as admin_name')
  41. ->alias('a')
  42. ->join('GoodsCate c', 'a.cate_id = c.id')
  43. ->join('Admin u', 'a.admin_id = u.id')
  44. ->order($order)
  45. ->paginate($rows, false, ['query' => $param])
  46. ->each(function ($item, $key) {
  47. $type = (int)$item->type;
  48. $item->type_str = self::$Type[$type];
  49. });
  50. return $list;
  51. }
  52. /**
  53. * 添加数据
  54. * @param $param
  55. */
  56. public function addGoods($param)
  57. {
  58. $insertId = 0;
  59. try {
  60. $param['create_time'] = time();
  61. $insertId = $this->strict(false)->field(true)->insertGetId($param);
  62. //关联关键字
  63. if (isset($param['keyword_names']) && $param['keyword_names']) {
  64. $keywordArray = explode(',', $param['keyword_names']);
  65. $res_keyword = $this->insertKeyword($keywordArray,$insertId);
  66. }
  67. add_log('add', $insertId, $param);
  68. } catch(\Exception $e) {
  69. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  70. }
  71. return to_assign(0,'操作成功',['aid'=>$insertId]);
  72. }
  73. /**
  74. * 编辑信息
  75. * @param $param
  76. */
  77. public function editGoods($param)
  78. {
  79. try {
  80. $param['update_time'] = time();
  81. $this->where('id', $param['id'])->strict(false)->field(true)->update($param);
  82. //关联关键字
  83. if (isset($param['keyword_names']) && $param['keyword_names']) {
  84. \think\facade\Db::name('GoodsKeywords')->where(['aid'=>$param['id']])->delete();
  85. $keywordArray = explode(',', $param['keyword_names']);
  86. $res_keyword = $this->insertKeyword($keywordArray,$param['id']);
  87. }
  88. add_log('edit', $param['id'], $param);
  89. } catch(\Exception $e) {
  90. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  91. }
  92. return to_assign();
  93. }
  94. /**
  95. * 根据id获取信息
  96. * @param $id
  97. */
  98. public function getGoodsById($id)
  99. {
  100. $info = $this->where('id', $id)->find();
  101. return $info;
  102. }
  103. /**
  104. * 删除信息
  105. * @param $id
  106. * @return array
  107. */
  108. public function delGoodsById($id,$type=0)
  109. {
  110. if($type==0){
  111. //逻辑删除
  112. try {
  113. $param['delete_time'] = time();
  114. $this->where('id', $id)->update(['delete_time'=>time()]);
  115. add_log('delete', $id);
  116. } catch(\Exception $e) {
  117. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  118. }
  119. }
  120. else{
  121. //物理删除
  122. try {
  123. $this->where('id', $id)->delete();
  124. add_log('delete', $id);
  125. } catch(\Exception $e) {
  126. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  127. }
  128. }
  129. return to_assign();
  130. }
  131. }