ProductDao.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\product;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\product\Product as model;
  14. use app\common\repositories\store\product\SpuRepository;
  15. use think\db\BaseQuery;
  16. use think\db\exception\DbException;
  17. use think\Exception;
  18. use think\facade\Db;
  19. class ProductDao extends BaseDao
  20. {
  21. protected function getModel(): string
  22. {
  23. return model::class;
  24. }
  25. /**
  26. * 创建或更新模型的属性。
  27. *
  28. * 本函数通过给定的模型ID,和一组属性数据,来创建或更新该模型的属性信息。
  29. * 它首先检索指定ID的模型实例,即使该模型已被删除(使用withTrashed()确保)。
  30. * 然后,它使用给定的数据批保存更新模型的属性。
  31. *
  32. * @param int $id 模型的唯一标识ID。
  33. * @param array $data 包含属性数据的数组,每个属性作为一个子数组。
  34. */
  35. public function createAttr(int $id, array $data)
  36. {
  37. ($this->getModel()::withTrashed()->find($id))->attr()->saveAll($data);
  38. }
  39. /**
  40. * 创建属性值
  41. *
  42. * 本函数用于根据提供的ID和数据数组创建属性值。它首先检索具有给定ID的模型实体,
  43. * 然后通过关联方法保存传入的数据数组。这个函数特别处理了软删除的情况,
  44. * 可以恢复和保存被软删除的模型的属性值。
  45. *
  46. * @param int $id 属性值关联模型的ID。这个ID用于定位特定的模型实例。
  47. * @param array $data 包含要保存的属性值数据的数组。每个元素都应该符合属性值的保存要求。
  48. */
  49. public function createAttrValue(int $id, array $data)
  50. {
  51. // 使用withTrashed()来包含软删除的记录, find($id)找回指定ID的记录,然后通过attrValue()方法关联属性值,最后使用saveAll()保存数据数组
  52. ($this->getModel()::withTrashed()->find($id))->attrValue()->saveAll($data);
  53. }
  54. /**
  55. * 创建或更新内容信息。
  56. *
  57. * 本函数通过给定的ID检索特定的模型实体,即使该实体已被删除(使用withTrashed()确保能检索到软删除的记录)。
  58. * 然后,它使用关联的方法(content())来保存或更新给定的数据数组到这个实体的内容字段。
  59. * 这种方法的设计允许在不直接触碰实体本身的情况下,灵活地处理与实体相关的内容数据。
  60. *
  61. * @param int $id 模型实体的唯一标识ID,用于检索特定的实体。
  62. * @param array $data 包含要保存或更新的内容数据的数组。
  63. */
  64. public function createContent(int $id, array $data)
  65. {
  66. ($this->getModel()::withTrashed()->find($id))->content()->save($data);
  67. }
  68. /**
  69. * 创建或更新预约商品信息
  70. *
  71. * @param integer $id
  72. * @param array $data
  73. * @return void
  74. */
  75. public function createReservation(int $id, array $data)
  76. {
  77. ($this->getModel()::withTrashed()->find($id))->reservation()->save($data);
  78. }
  79. /**
  80. * 检查指定字段的值在数据库中是否存在。
  81. *
  82. * 此函数用于确定给定字段的特定值是否在数据库中出现。它支持排除特定值的检查,
  83. * 以及针对特定商户ID的检查。这在处理数据唯一性或验证数据是否存在时非常有用。
  84. *
  85. * @param int|null $merId 商户ID,用于限定查询范围。如果为null,则不进行商户ID的筛选。
  86. * @param string $field 要检查的字段名。
  87. * @param string $value 要检查的字段值。
  88. * @param string|null $except 排除的特定值,如果不为null,则查询时会排除这个值。
  89. * @return bool 如果存在返回true,否则返回false。
  90. * @throws DbException
  91. */
  92. public function merFieldExists(?int $merId, $field, $value, $except = null)
  93. {
  94. // 使用withTrashed()确保查询时包括已删除的数据
  95. return model::withTrashed()->when($except, function ($query, $except) use ($field) {
  96. // 如果有需要排除的值,则添加不等于条件
  97. $query->where($field, '<>', $except);
  98. })->when($merId, function ($query, $merId) {
  99. // 如果提供了商户ID,则添加条件筛选特定商户的数据
  100. $query->where('mer_id', $merId);
  101. })->where($field, $value)->count() > 0;
  102. }
  103. /**
  104. * 检查指定字段的值在数据库中是否存在。
  105. *
  106. * 此函数用于通过字段值查询数据库记录,判断是否存在满足条件的记录。
  107. * 它支持排除特定的值和特定的商户ID,同时确保查询的记录状态为有效。
  108. *
  109. * @param int $merId 商户ID,可选参数,用于限定查询的商户范围。
  110. * @param string $field 要查询的字段名。
  111. * @param mixed $value 字段的值,用于查询条件。
  112. * @param mixed $except 排除的值,可选参数,用于不在指定值范围内的查询。
  113. * @return bool 如果存在满足条件的记录,则返回true;否则返回false。
  114. */
  115. public function apiFieldExists(int $merId, $field, $value, $except = null)
  116. {
  117. // 获取数据库实例
  118. $db = ($this->getModel())::getDB();
  119. // 如果有指定的排除值,添加不等于排除值的查询条件
  120. $db->when($except, function ($query, $except) use ($field) {
  121. $query->where($field, '<>', $except);
  122. });
  123. // 如果指定了商户ID,添加商户ID的查询条件
  124. $db->when($merId, function ($query, $merId) {
  125. $query->where('mer_id', $merId);
  126. });
  127. // 确保查询的状态为有效
  128. $db->where(['status' => 1]);
  129. // 添加字段值的查询条件
  130. $db->where($field, $value);
  131. // 判断满足所有条件的记录数是否大于0,存在则返回true,否则返回false
  132. return $db->count() > 0;
  133. }
  134. /**
  135. * 检查是否存在已被删除的商品
  136. *
  137. * 本函数用于确定给定商家ID和产品ID对应的商品是否曾经被删除过。
  138. * 这是通过查询数据库中被软删除(即使用了`onlyTrashed`方法)的商品记录来实现的。
  139. * 如果找到了匹配的被删除的商品记录,则说明该商品曾经被删除过,返回true;
  140. * 如果没有找到匹配的记录,则说明该商品从未被删除过,返回false。
  141. *
  142. * @param int $merId 商家ID,用于限定查询的商家范围
  143. * @param int $productId 产品ID,用于查询特定的产品
  144. * @return bool 如果存在被删除的商品记录则返回true,否则返回false
  145. */
  146. public function getDeleteExists(int $merId, int $productId)
  147. {
  148. // 使用onlyTrashed方法查询被删除的商品记录,限定查询条件为商家ID和产品ID
  149. // 然后通过count方法统计匹配记录的数量,判断是否存在被删除的商品
  150. return ($this->getModel())::onlyTrashed()->where('mer_id', $merId)->where($this->getPk(), $productId)->count() > 0;
  151. }
  152. /**
  153. * 根据条件搜索产品信息。
  154. *
  155. * 该方法用于构建并执行产品搜索查询。它根据传入的条件数组来筛选产品,
  156. * 并支持多种条件组合以满足不同的搜索需求。搜索条件包括但不限于产品属性、
  157. * 商家标识、状态、标签等。此外,方法还处理了不同条件下的查询逻辑,如软删除、
  158. * 商家关联、产品类型等。
  159. *
  160. * @param string $merId 商家ID,用于限制搜索范围。
  161. * @param array $where 搜索条件数组,包含各种过滤条件。
  162. * @return \think\db\Query 查询对象,可用于进一步的查询操作或获取结果。
  163. */
  164. public function search($merId, array $where, $search = '')
  165. {
  166. // 初始化用于构建查询条件的数组
  167. $keyArray = $whereArr = [];
  168. // 定义需要排除的搜索字段
  169. $out = ['soft', 'us_status', 'mer_labels', 'sys_labels', 'order', 'hot_type', 'is_action', 'seckill_active_id','spu_ids'];
  170. // 遍历搜索条件,构建查询字段和值的映射
  171. foreach ($where as $key => $item) {
  172. if ($item !== '' && !in_array($key, $out)) {
  173. $keyArray[] = $key;
  174. $whereArr[$key] = $item;
  175. }
  176. }
  177. // 根据是否有软删除条件,构建查询对象
  178. $query = isset($where['soft']) ? model::onlyTrashed()->alias('Product')->where('delete', 0) : model::alias('Product');
  179. if($search) {
  180. $query->hasWhere('attrValue');
  181. $query->where(function ($query) use($search) {
  182. $query->hasWhere('attrValue', ['bar_code_number' => $search])->whereOr("Product.store_name|Product.product_id", "like", "%{$search}%");
  183. });
  184. }
  185. // 分组权限
  186. if (app('request')->hasMacro('regionAuthority') && $region = app('request')->regionAuthority()) {
  187. $query->whereIn('Product.mer_id', $region);
  188. }
  189. // 处理商家类型条件
  190. if (isset($where['is_trader']) && $where['is_trader'] !== '') {
  191. $query->hasWhere('merchant', function ($query) use ($where) {
  192. $query->where('is_trader', $where['is_trader']);
  193. });
  194. }
  195. // 处理搜索关键字和关联查询
  196. $query->withSearch($keyArray, $whereArr)->Join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', $where['product_type'] ?? 0);
  197. // 处理类别ID条件
  198. $query->when((isset($where['spu_ids']) && !empty($where['spu_ids'])), function ($query) use ($where) {
  199. $query->whereIn('spu_id',$where['spu_ids']);
  200. });
  201. // 处理类别ID条件
  202. $query->when((isset($where['cate_id']) && !empty($where['cate_id'])), function ($query) use ($where) {
  203. $query->where(['cate_id' => $where['cate_id']]);
  204. });
  205. // 处理秒杀活动ID条件
  206. $query->when((isset($where['seckill_active_id']) && is_array($where['seckill_active_id']) && !empty($where['seckill_active_id'])), function ($query) use ($where) {
  207. $query->whereIn('Product.active_id', $where['seckill_active_id']);
  208. });
  209. $query->when((isset($where['seckill_active_id']) && !is_array($where['seckill_active_id']) && $where['seckill_active_id']), function ($query) use ($where) {
  210. $query->where('Product.active_id', $where['seckill_active_id']);
  211. });
  212. // 处理商家ID条件
  213. $query->when($merId, function ($query) use ($merId) {
  214. if (is_array($merId)) {
  215. $query->whereIn('Product.mer_id', $merId);
  216. } else if (!is_array($merId)) {
  217. $query->where('Product.mer_id', $merId);
  218. }
  219. })
  220. // 处理热门类型条件
  221. ->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) {
  222. if ($where['hot_type'] == 'new')
  223. $query->where('is_new', 1);
  224. else if ($where['hot_type'] == 'hot')
  225. $query->where('is_hot', 1);
  226. else if ($where['hot_type'] == 'best')
  227. $query->where('is_best', 1);
  228. else if ($where['hot_type'] == 'good')
  229. $query->where('is_benefit', 1);
  230. })
  231. // 处理使用状态条件
  232. ->when(isset($where['us_status']) && $where['us_status'] !== '', function ($query) use ($where) {
  233. if ($where['us_status'] == 0) {
  234. $query->where('Product.is_show', 0)->where('Product.is_used', 1)->where('Product.status', 1);
  235. }
  236. if ($where['us_status'] == 1) {
  237. $query->where('Product.is_show', 1)->where('Product.is_used', 1)->where('Product.status', 1);
  238. }
  239. if ($where['us_status'] == -1) {
  240. $query->where(function ($query) {
  241. $query->where('Product.is_used', 0)->whereOr('Product.status', '<>', 1);
  242. });
  243. }
  244. })
  245. // 处理标签条件
  246. ->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) {
  247. $query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%");
  248. })
  249. // 处理活动商品条件
  250. ->when(isset($where['is_action']) && $where['is_action'] !== '', function ($query) use ($where) {
  251. $query->where('type', '<>', 2);
  252. })
  253. // 处理系统标签条件
  254. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) {
  255. $query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%");
  256. })
  257. // 处理VIP价格类型条件
  258. ->when(isset($where['svip_price_type']) && $where['svip_price_type'] !== '', function ($query) use ($where) {
  259. $query->where('Product.svip_price_type', $where['svip_price_type']);
  260. })
  261. // 处理产品类型条件
  262. ->when(isset($where['product_type']) && $where['product_type'] == 1, function ($query) use ($where) {
  263. $query->where('active_id', '>', 0);
  264. })
  265. // 处理排序条件
  266. ->when(isset($where['order']), function ($query) use ($where, $merId) {
  267. if (in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sales'])) {
  268. if ($where['order'] == 'price_asc') {
  269. $where['order'] = 'price ASC';
  270. } else if ($where['order'] == 'price_desc') {
  271. $where['order'] = 'price DESC';
  272. } else {
  273. $where['order'] = $where['order'] . ' DESC';
  274. }
  275. $query->order($where['order'] . ',rank DESC ,create_time DESC ');
  276. } else if ($where['order'] !== '') {
  277. $query->order('U.' . $where['order'] . ' DESC,U.create_time DESC');
  278. } else {
  279. $query->order('U.create_time DESC');
  280. }
  281. })
  282. // 处理星级条件
  283. ->when(isset($where['star']), function ($query) use ($where) {
  284. $query->when($where['star'] !== '', function ($query) use ($where) {
  285. $query->where('U.star', $where['star']);
  286. });
  287. $query->order('U.star DESC,U.rank DESC,Product.create_time DESC');
  288. })
  289. // 处理排序条件
  290. ->when(isset($where['sort']), function ($query) use ($where) {
  291. $query->when($where['sort'] !== '', function ($query) use ($where) {
  292. $query->where('U.sort', $where['sort']);
  293. });
  294. $query->order('Product.sort DESC,Product.create_time DESC');
  295. })
  296. // 处理优质产品条件
  297. ->when(isset($where['is_good']) && $where['is_good'] !== '', function ($query) use ($where) {
  298. $query->where('Product.is_good', $where['is_good']);
  299. });
  300. return $query;
  301. }
  302. /**
  303. * 搜索秒杀活动商品
  304. *
  305. * 本函数用于构建查询秒杀活动商品的条件,根据传入的$where数组来筛选符合条件的秒杀活动及商品。
  306. * 这其中包括对秒杀活动状态的检查,商品的各种状态过滤,以及针对商家和活动ID的条件查询。
  307. * 最后返回构建好的查询对象,可用于进一步的查询操作或数据获取。
  308. *
  309. * @param array $where 查询条件数组,包含各种过滤条件如日期、时间、商家ID、活动ID等。
  310. * @return \think\db\Query 返回构建好的查询对象,包含所有设定的查询条件。
  311. */
  312. public function seckillSearch(array $where)
  313. {
  314. // 根据$where数组中的条件构建秒杀活动(seckillActive)的查询条件
  315. $query = model::hasWhere('seckillActive', function ($query) use ($where) {
  316. // 仅查询状态为1(有效)的秒杀活动
  317. $query->where('status', 1);
  318. // 注释掉的代码是原本用于查询日期范围的条件,根据具体业务需求可能启用或禁用
  319. });
  320. // 加入关联查询,关联商品表中的SPU信息,过滤出产品类型为1的商品
  321. $query->join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', 1);
  322. // 设置一系列固定的查询条件,包括商品的状态、是否展示、是否可用、商家状态等
  323. $query->where([
  324. 'Product.is_show' => 1,
  325. 'Product.status' => 1,
  326. 'Product.is_used' => 1,
  327. 'Product.mer_status' => 1,
  328. 'Product.product_type' => 1,
  329. 'Product.is_gift_bag' => 0,
  330. ])
  331. // 当$where数组中包含mer_id时,添加商家ID的查询条件
  332. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  333. $query->where('Product.mer_id', $where['mer_id']);
  334. })
  335. // 当$where数组中包含active_id且不为空(可能是数组)时,添加活动ID的查询条件
  336. ->when(isset($where['active_id']) && !empty($where['active_id']) && is_array($where['active_id']), function ($query) use ($where) {
  337. $query->whereIn('Product.active_id', $where['active_id']);
  338. })
  339. // 当$where数组中包含star时,添加商品星级的查询条件,并按星级和排名排序
  340. ->when(isset($where['star']), function ($query) use ($where) {
  341. $query->when($where['star'] !== '', function ($query) use ($where) {
  342. $query->where('U.star', $where['star']);
  343. });
  344. // 按星级和排名降序排序
  345. $query->order('U.star DESC,Product.rank DESC');
  346. });
  347. // 返回构建好的查询对象
  348. return $query;
  349. }
  350. /**
  351. * 删除指定ID的记录。
  352. *
  353. * 此方法提供了软删除和硬删除两种方式,软删除会将记录移动到回收站,硬删除则会直接从数据库中删除。
  354. * 同时,此方法还会更新关联的SPU信息,并触发一个产品删除事件。
  355. *
  356. * @param int $id 需要删除的记录的ID。
  357. * @param bool $soft 是否执行软删除,默认为false表示硬删除。
  358. */
  359. public function delete(int $id, $soft = false)
  360. {
  361. // 根据$soft参数决定使用软删除还是硬删除
  362. if ($soft) {
  363. // 执行软删除,找到指定ID的回收站中的记录,并强制删除
  364. (($this->getModel())::onlyTrashed()->find($id))->force()->delete();
  365. } else {
  366. // 执行硬删除,通过更新is_del字段为1来标记删除
  367. $this->getModel()::where($this->getPk(), $id)->update(['is_del' => 1]);
  368. }
  369. // 更新关联的SPU信息,将其标记为删除状态并禁用
  370. app()->make(SpuRepository::class)->getSearch(['product_id' => $id])->update(['is_del' => 1, 'status' => 0]);
  371. // 触发产品删除事件,传递删除的ID作为参数
  372. event('product.delete', compact('id'));
  373. }
  374. /**
  375. * 删除产品及相关信息。
  376. *
  377. * 本函数用于彻底删除指定ID的产品及其在搜索索引中的信息,并触发相应的删除事件。
  378. * 具体操作包括:
  379. * 1. 更新数据库中该产品的删除状态为1,实现逻辑删除。
  380. * 2. 删除搜索索引中该产品的信息,确保搜索结果不再包含该产品。
  381. * 3. 触发产品删除事件,允许其他监听器对此操作做出响应。
  382. *
  383. * @param int $id 产品的ID。
  384. */
  385. public function destory(int $id)
  386. {
  387. try {
  388. // 更新数据库中指定产品ID的删除状态为1,同时处理已删除的记录。
  389. $this->getModel()::withTrashed()->where('product_id', $id)->update(['delete' => 1]);
  390. // 删除搜索索引中与指定产品ID相关的信息。
  391. app()->make(SpuRepository::class)->getSearch(['product_id' => $id])->delete();
  392. // 触发产品删除事件,传递删除的产品ID作为参数。
  393. event('product.delete', compact('id'));
  394. } catch (Exception $e) {
  395. // 捕获并处理任何异常,避免删除操作影响到整个程序的执行。
  396. }
  397. }
  398. /**
  399. * 恢复已删除的资源。
  400. *
  401. * 此方法用于恢复数据库中被软删除的记录。它首先通过给定的ID找到被删除的记录,
  402. * 然后将其恢复到原始状态。这个过程涉及到两个主要步骤:
  403. * 1. 使用onlyTrashed()方法定位到被删除的记录,并通过find()方法找到特定ID的记录。
  404. * 2. 调用restore()方法来恢复该记录到原来的表中。
  405. *
  406. * @param int $id 要恢复的资源的ID。
  407. * @return \Illuminate\Database\Eloquent\Model 恢复的资源模型。
  408. */
  409. public function restore($id)
  410. {
  411. // 通过ID找到被删除的资源。
  412. $res = ($this->getModel())::onlyTrashed()->find($id);
  413. // 从产品仓库中删除该产品,将其状态重置为未删除。
  414. app()->make(SpuRepository::class)->delProduct($id, 0);
  415. // 恢复该资源到原始状态。
  416. return $res->restore();
  417. }
  418. /**
  419. * 获取只被软删除的数据
  420. *
  421. * 本函数用于查询特定条件下的只有被软删除的数据。它利用了Laravel框架的Eloquent ORM提供的onlyTrashed方法,
  422. * 该方法用于获取仅被软删除的数据。通过结合where方法和order方法,可以对查询条件和结果排序进行进一步的定制。
  423. *
  424. * @param array|string $where 查询条件,可以是数组或字符串形式的SQL WHERE子句。
  425. * @return \Illuminate\Database\Eloquent\Builder|static 只被软删除的数据的查询构建器。
  426. */
  427. public function getOnlyTranshed($where)
  428. {
  429. // 调用getModel方法获取模型实例,并立即使用onlyTrashed方法查询只被软删除的数据
  430. // 然后使用where方法添加额外的查询条件,最后使用order方法按照product_id降序排序
  431. return ($this->getModel())::onlyTrashed()->where($where)->order('product_id DESC');
  432. }
  433. /**
  434. * 切换实体的状态
  435. *
  436. * 本函数用于根据给定的ID和新的状态数组,更新数据库中相应实体的状态。
  437. * 它首先通过getModel方法获取当前实体的模型,然后利用模型的getDB方法获取数据库连接。
  438. * 最后,使用where方法指定ID条件,并通过update方法更新实体的状态。
  439. *
  440. * @param int $id 实体的唯一标识符,用于在数据库中定位特定的实体。
  441. * @param array $status 包含新状态值的数组,这些值将被更新到数据库中。
  442. * @return int 返回更新操作影响的行数,用于确认更新是否成功。
  443. */
  444. public function switchStatus(int $id, array $status)
  445. {
  446. // 通过模型和数据库操作符更新实体的状态
  447. return ($this->getModel()::getDB())->where($this->getPk(), $id)->update($status);
  448. }
  449. /**
  450. * 根据商家ID和产品ID数组,获取对应的产品图片信息
  451. *
  452. * 此函数用于查询数据库,获取指定商家ID下,指定产品ID列表中每个产品的ID和图片信息。
  453. * 主要用于在前端展示产品图片,或者进行与产品图片相关的操作。
  454. *
  455. * @param int $merId 商家ID,用于限定查询的商家范围。
  456. * @param array $productIds 产品ID数组,指定需要查询的产品。
  457. * @return array 返回一个包含产品ID和图片信息的数组。
  458. */
  459. public function productIdByImage(int $merId, array $productIds)
  460. {
  461. // 使用模型的数据库访问方法,查询满足条件的产品ID和图片信息
  462. return model::getDB()->where('mer_id', $merId)->whereIn('product_id', $productIds)->column('product_id,image');
  463. }
  464. /**
  465. * 获取与给定ID数组交集的产品ID数组
  466. *
  467. * 本函数用于查询数据库中,产品表中产品ID存在于给定ID数组中的所有产品ID。
  468. * 通过使用whereIn查询条件,筛选出产品ID在给定数组中的记录,并只返回product_id列的值。
  469. *
  470. * @param array $ids 给定的产品ID数组,用于查询的条件
  471. * @return array 返回查询结果中product_id列的值组成的数组,即与给定ID数组的交集
  472. */
  473. public function intersectionKey(array $ids): array
  474. {
  475. // 使用model的getDB方法获取数据库对象,然后通过whereIn方法查询产品表中product_id在$ids数组中的记录,最后使用column方法仅返回product_id列的值
  476. return model::getDB()->whereIn('product_id', $ids)->column('product_id');
  477. }
  478. /**
  479. * 根据产品ID获取商家ID
  480. *
  481. * 本函数旨在通过产品ID查询数据库,获取对应产品的商家ID。
  482. * 使用了模型类的数据库操作方法,通过指定的条件查询数据库,并返回查询结果中特定列的值。
  483. *
  484. * @param int $id 产品ID,作为查询条件
  485. * @return int 商家ID,如果找不到对应产品则返回null
  486. */
  487. public function productIdByMerId($id)
  488. {
  489. // 使用模型的数据库操作方法,根据产品ID查询商家ID
  490. return model::getDB()->where('product_id', $id)->value('mer_id');
  491. }
  492. /**
  493. * 减少产品库存并增加销售数量
  494. *
  495. * 该方法用于更新数据库中指定产品的库存和销售数量。
  496. * 它通过减少库存量和增加销售量来反映产品的销售情况。
  497. *
  498. * @param int $productId 产品的ID,用于定位特定的产品记录。
  499. * @param int $desc 库存减少的数量,同时也是销售数量增加的数量。
  500. * @return mixed 返回数据库更新操作的结果,可能是布尔值或影响的行数。
  501. * @throws DbException
  502. */
  503. public function descStock(int $productId, int $desc)
  504. {
  505. // 使用Db::raw来执行原生的SQL片段,这里用于更新库存和销售数量。
  506. // 这种方法允许直接操作数据库字段的值,而不是通过变量绑定。
  507. return model::getDB()->where('product_id', $productId)->update([
  508. 'stock' => Db::raw('stock-' . $desc),
  509. 'sales' => Db::raw('sales+' . $desc)
  510. ]);
  511. }
  512. /**
  513. * 增加商品库存并减少对应销售量
  514. * 此函数用于在数据库中增加指定商品的库存量,并同时减少该商品的销售量。
  515. * 这种操作常见于处理商品退货或库存调整的情况,需要在库存和销售数据中做出相应的调整。
  516. *
  517. * @param int $productId 商品ID,用于指定需要调整库存的商品。
  518. * @param int $inc 库存增加的数量。此参数同时用于增加库存和减少销售量。
  519. */
  520. public function incStock(int $productId, int $inc)
  521. {
  522. // 增加商品库存
  523. model::getDB()->where('product_id', $productId)->inc('stock', $inc)->update();
  524. // 减少商品销售量,仅针对之前已销售的数量大于等于当前增加的库存量的部分进行调整
  525. model::getDB()->where('product_id', $productId)->where('sales', '>=', $inc)->dec('sales', $inc)->update();
  526. }
  527. /**
  528. * 减少商品销量
  529. *
  530. * 该方法用于更新数据库中指定商品的销量,将其减少指定的数量。
  531. * 主要用于处理商品销售时的库存更新或其他需要减少销量的场景。
  532. *
  533. * @param int $productId 商品ID,用于定位要更新销量的商品。
  534. * @param int $desc 销量减少的数量,一个正整数,表示要从当前销量中减去的数值。
  535. * @return mixed 返回数据库操作的结果,可能是布尔值或影响行数。
  536. * @throws DbException
  537. */
  538. public function descSales(int $productId, int $desc)
  539. {
  540. // 使用Db::raw处理SQL中的减法操作,确保操作的安全性和正确性。
  541. return model::getDB()->where('product_id', $productId)->update([
  542. 'sales' => Db::raw('sales-' . $desc)
  543. ]);
  544. }
  545. /**
  546. * 增加商品销量
  547. *
  548. * 该方法用于更新指定商品的销量,销量增加的数值由参数$inc指定。
  549. * 通过传入商品ID($productId)来定位到特定商品,并对销量进行增加操作。
  550. * 使用Db::raw()来构建SQL的计算表达式,确保销量是原销量基础上增加$inc。
  551. *
  552. * @param int $productId 商品ID,用于确定要更新销量的具体商品。
  553. * @param int $inc 销量增加的数值,表示要将商品的销量增加多少。
  554. * @return bool 更新操作的结果,成功返回true,失败返回false。
  555. * @throws DbException
  556. */
  557. public function incSales(int $productId, int $inc)
  558. {
  559. // 根据商品ID更新数据库中对应商品的销量,销量增加$inc
  560. return model::getDB()->where('product_id', $productId)->update([
  561. 'sales' => Db::raw('sales+' . $inc)
  562. ]);
  563. }
  564. /**
  565. * 减少商品的积分总量和积分价格总量
  566. *
  567. * 本函数用于更新数据库中指定商品的积分总量和积分价格总量,实现减少操作。
  568. * 主要用于处理商品积分的扣减逻辑,例如在商品退货、积分抵扣减少等场景下。
  569. *
  570. * @param int $productId 商品ID,用于定位要更新的商品记录。
  571. * @param int $integral_total 需要减少的积分总量,表示本次操作将从商品的积分总量中减去的数值。
  572. * @param int $integral_price_total 需要减少的积分价格总量,表示本次操作将从商品的积分价格总量中减去的数值。
  573. * @return int 返回更新操作的影响行数,用于判断操作是否成功。
  574. * @throws DbException
  575. */
  576. public function descIntegral(int $productId, $integral_total, $integral_price_total)
  577. {
  578. // 使用Db::raw包裹更新表达式,直接在SQL中进行减法操作,避免因类型转换等问题造成的错误。
  579. // 通过where条件定位到指定ID的商品记录,然后更新其积分总量和积分价格总量。
  580. return model::getDB()->where('product_id', $productId)->update([
  581. 'integral_total' => Db::raw('integral_total-' . $integral_total),
  582. 'integral_price_total' => Db::raw('integral_price_total-' . $integral_price_total),
  583. ]);
  584. }
  585. /**
  586. * 增加产品的积分和积分金额
  587. *
  588. * 该方法用于更新数据库中指定产品的积分总数和积分金额总数。
  589. * 它通过传入的产品ID定位到特定的产品记录,然后分别增加积分总数和积分金额总数。
  590. * 这种设计用于在进行相关交易或操作时,自动更新产品的积分相关数据,保持数据的实时性。
  591. *
  592. * @param int $productId 产品的ID,用于在数据库中定位到特定产品记录。
  593. * @param int $integral_total 需要增加的积分总数,表示产品积分的增量。
  594. * @param int $integral_price_total 需要增加的积分金额总数,表示产品积分金额的增量。
  595. */
  596. public function incIntegral(int $productId, $integral_total, $integral_price_total)
  597. {
  598. // 使用模型的数据库访问方法,定位到指定产品ID的记录,然后分别增加积分总数和积分金额总数,并执行更新操作。
  599. model::getDB()->where('product_id', $productId)->inc('integral_total', $integral_total)->inc('integral_price_total', $integral_price_total)->update();
  600. }
  601. /**
  602. * 访问产品组的方法
  603. * 该方法用于查询指定日期内,每个产品组的访问量和相关信息。
  604. * 可以根据日期和商家ID进行过滤,返回最近访问量最高的产品组列表。
  605. *
  606. * @param string $date 查询的日期,格式为YYYY-MM-DD。如果不提供,则查询最近7天的数据。
  607. * @param int $merId 商家ID,可选参数。如果提供了商家ID,则只查询该商家的产品组。
  608. * @param int $limit 返回结果的数量限制,默认为7。
  609. * @return array 返回一个包含产品组信息的数组,每个元素包含产品ID、商店名称、图片和访问量。
  610. */
  611. public function visitProductGroup($date, $merId = null, $limit = 7)
  612. {
  613. // 从数据库中获取数据
  614. return model::getDB()->alias('A')->leftJoin('UserRelation B', 'A.product_id = B.type_id')
  615. ->field(Db::raw('count(B.type_id) as total,A.product_id,A.store_name,A.image'))
  616. ->when($date, function ($query, $date) {
  617. // 如果提供了日期,则根据日期筛选数据
  618. getModelTime($query, $date, 'B.create_time');
  619. })->when($merId, function ($query, $merId) {
  620. // 如果提供了商家ID,则根据商家ID筛选数据
  621. $query->where('A.mer_id', $merId);
  622. })->where('B.type', 1)->group('A.product_id')->limit($limit)->order('total DESC')->select();
  623. }
  624. /**
  625. * 获取购物车产品分组信息
  626. * 该方法用于查询指定日期内,每个产品的购物车数量总和,可选地根据商家ID进行筛选。
  627. * 结果将按照购物车数量降序排列,并限制返回结果的数量。
  628. *
  629. * @param string $date 查询的日期,格式为YYYY-MM-DD。用于筛选在指定日期内添加到购物车的产品。
  630. * @param int|null $merId 商家ID,可选参数。用于筛选属于特定商家的产品。
  631. * @param int $limit 返回结果的数量限制,默认为7。用于限制返回的产品分组数量。
  632. * @return array 返回一个包含产品分组信息的数组,每个分组包含产品ID、商家名称、产品图片和购物车总数。
  633. */
  634. public function cartProductGroup($date, $merId = null, $limit = 7)
  635. {
  636. // 从数据库中获取数据
  637. return model::getDB()->alias('A')->leftJoin('StoreCart B', 'A.product_id = B.product_id')
  638. ->field(Db::raw('sum(B.cart_num) as total,A.product_id,A.store_name,A.image'))
  639. // 当传入日期时,根据日期筛选数据
  640. ->when($date, function ($query, $date) {
  641. getModelTime($query, $date, 'B.create_time');
  642. })
  643. // 当传入商家ID时,根据商家ID筛选数据
  644. ->when($merId, function ($query, $merId) {
  645. $query->where('A.mer_id', $merId);
  646. })
  647. // 筛选条件:产品类型为0,未支付,未删除,非新购,未失败
  648. ->where('B.product_type', 0)->where('B.is_pay', 0)->where('B.is_del', 0)
  649. ->where('B.is_new', 0)->where('B.is_fail', 0)
  650. // 按产品ID分组,限制返回结果的数量,并按购物车总数降序排列
  651. ->group('A.product_id')->limit($limit)->order('total DESC')->select();
  652. }
  653. /**
  654. * 更新商家产品的信息
  655. *
  656. * 本函数用于根据给定的商家ID和数据集,更新对应商家产品的信息。
  657. * 它通过查询数据库中与给定商家ID匹配的记录,并应用提供的数据更新。
  658. *
  659. * @param string $merId 商家的唯一标识符。用于确定要更新哪个商家的产品信息。
  660. * @param array $data 包含要更新的产品信息的数据数组。数组的键值对表示要更新的字段及其新值。
  661. */
  662. public function changeMerchantProduct($merId, $data)
  663. {
  664. // 通过模型获取数据库实例,并使用where子句指定mer_id,然后执行更新操作。
  665. ($this->getModel()::getDB())->where('mer_id', $merId)->update($data);
  666. }
  667. /**
  668. * 增加产品关注数
  669. *
  670. * 该方法用于指定产品的关注数增加1。它通过调用getModel方法获取模型实例,
  671. * 进而连接数据库,并根据产品的主键($productId)定位到相应记录,
  672. * 然后将该记录的care_count字段值增加1。
  673. *
  674. * @param int $productId 产品的唯一标识符,用于在数据库中定位到具体产品记录。
  675. */
  676. public function incCareCount(int $productId)
  677. {
  678. // 通过模型实例的getDB方法获取数据库连接,然后使用where方法指定条件,
  679. // 使用inc方法增加care_count字段的值,并通过update方法更新数据库记录。
  680. ($this->getModel()::getDB())->where($this->getPk(), $productId)->inc('care_count', 1)->update();
  681. }
  682. /**
  683. * 减少指定产品关注数
  684. *
  685. * 此方法用于减少数据库中指定产品ID的关注数。它首先通过产品ID数组筛选出相关记录,
  686. * 然后针对这些记录将关注数减少1。这种方法适用于批量处理,例如在用户取消关注多个产品时。
  687. *
  688. * @param array $productId 产品ID数组,表示需要减少关注数的产品集合。
  689. */
  690. public function decCareCount(array $productId)
  691. {
  692. // 检查$productId是否为空,避免不必要的数据库操作
  693. if (empty($productId)) {
  694. return; // 或者记录日志、抛出异常等,根据实际需求决定
  695. }
  696. // 通过模型获取数据库实例,并使用whereIn和where条件构建查询,然后减少care_count列的值并更新记录。
  697. ($this->getModel()::getDB())->whereIn($this->getPk(), $productId)->where('care_count', '>', 0)->dec('care_count', 1)->update();
  698. }
  699. /**
  700. * 获取商品展示配置
  701. *
  702. * 该方法用于返回一个配置数组,定义了商品在前端的展示行为和状态。
  703. * 返回的配置包括:
  704. * - is_show: 商品是否上架,值为1表示上架。
  705. * - status: 商品审核状态,值为1表示审核通过。
  706. * - is_used: 商品是否启用,值为1表示启用。
  707. * - product_type: 商品类型,值为0表示普通商品。
  708. * - mer_status: 商铺状态,值为1表示商铺正常运营。
  709. * - is_gift_bag: 商品是否为礼盒,值为0表示不是礼盒。
  710. *
  711. * @return array 商品展示配置数组
  712. */
  713. public function productShow()
  714. {
  715. return [
  716. 'is_show' => 1, // 上架
  717. 'status' => 1, // 审核通过
  718. 'is_used' => 1, // 显示
  719. 'product_type' => 0, // 普通商品
  720. 'mer_status' => 1, //商铺状态正常
  721. 'is_gift_bag' => 0, //不是礼包
  722. ];
  723. }
  724. /**
  725. * api展示的礼包商品条件
  726. * @return array
  727. * @author Qinii
  728. * @day 2020-08-18
  729. */
  730. public function bagShow()
  731. {
  732. return [
  733. 'is_show' => 1,
  734. 'status' => 1,
  735. 'is_used' => 1,
  736. 'mer_status' => 1,
  737. 'product_type' => 0,
  738. 'is_gift_bag' => 1,
  739. ];
  740. }
  741. /**
  742. * api展示的秒杀商品条件
  743. * @return array
  744. * @author Qinii
  745. * @day 2020-08-18
  746. */
  747. public function seckillShow()
  748. {
  749. return [
  750. 'is_show' => 1,
  751. 'status' => 1,
  752. 'is_used' => 1,
  753. 'mer_status' => 1,
  754. 'product_type' => 1,
  755. 'is_gift_bag' => 0,
  756. ];
  757. }
  758. /**
  759. * 根据产品ID获取产品类型,并检查是否与传入的现有类型匹配。
  760. *
  761. * 此方法主要用于查询数据库中指定产品ID对应的产品类型,并根据需求判断是否与预设的存在类型相匹配。
  762. * 如果传入的存在类型不为空,则会在查询时添加一个额外的条件来筛选产品类型。
  763. * 方法返回一个布尔值,表示查询到的产品类型是否为0(即true表示是,false表示不是)。
  764. *
  765. * @param int $productId 产品的唯一标识ID。
  766. * @param int|null $exsistType 存在的产品类型ID,用于查询时的条件筛选。
  767. * @return bool 如果查询到的产品类型为0,则返回true,否则返回false。
  768. */
  769. public function getProductTypeById(int $productId, ?int $exsistType)
  770. {
  771. // 通过模型获取数据库实例,并根据条件构造查询语句。
  772. $product_type = $this->getModel()::getDB()
  773. ->when($exsistType, function ($query) use ($exsistType) {
  774. // 如果存在类型ID,则在查询时添加对应的产品类型条件。
  775. $query->where('product_type', $exsistType);
  776. })
  777. ->where($this->getPk(), $productId) // 根据产品ID进行查询。
  778. ->where('is_del', 0) // 排除已删除的产品。
  779. ->value('product_type'); // 只返回产品类型这一列的值。
  780. // 判断查询到的产品类型是否为0,返回相应的布尔值。
  781. return $product_type == 0 ? true : false;
  782. }
  783. /**
  784. * 根据产品ID获取失败的产品信息
  785. *
  786. * 本函数旨在通过产品ID检索特定产品的详细信息。特别地,它包括了产品是否被删除的状态,
  787. * 这是通过使用`withTrashed`方法来实现的,意味着即使产品已被标记为删除,也能被检索到。
  788. * 它返回的产品信息精简到了最相关和必要的字段,以提高查询效率和数据使用的针对性。
  789. *
  790. * @param int $productId 产品ID,用于精确查找特定产品。
  791. * @return \think\Model|null 返回匹配给定产品ID的模型对象,如果找不到则返回null。
  792. */
  793. public function getFailProduct(int $productId)
  794. {
  795. // 使用withTrashed确保可以查询到已删除的数据
  796. // 精选查询字段,只获取必要信息,提高查询效率
  797. return $this->getModel()::withTrashed()->field('product_type,product_id,image,store_name,is_show,status,is_del,unit_name,price,mer_status,is_used,mer_form_id')->find($productId);
  798. }
  799. /**
  800. * 获取已删除的产品信息
  801. *
  802. * 本方法用于查询一个特定ID的产品,包括已经被软删除的产品。通过使用`withTrashed`方法,可以包含已被删除的数据在查询结果中。
  803. * 这对于需要恢复已删除数据或者查看删除状态下的数据是非常有用的。
  804. *
  805. * @param int $id 产品的唯一标识ID
  806. * @return \Illuminate\Database\Eloquent\Model 返回查询到的产品模型,可能包括已被删除的产品。
  807. */
  808. public function geTrashedtProduct(int $id)
  809. {
  810. // 使用withTrashed方法包含软删除的记录,并根据主键ID查询产品
  811. return model::withTrashed()->where($this->getPk(), $id);
  812. }
  813. /**
  814. * 获取各种有效时间内的活动
  815. * @param int $productType
  816. * @return BaseQuery
  817. *
  818. * @date 2023/09/22
  819. * @author yyw
  820. */
  821. public function activitSearch(int $productType)
  822. {
  823. $query = model::getDB()->alias('P')
  824. ->where('P.is_del', 0)
  825. ->where('P.mer_status', 1)
  826. ->where('P.product_type', $productType);
  827. switch ($productType) {
  828. case 0:
  829. // $query->where('P.is_show',1)
  830. // ->where('P.is_used',1)
  831. // ->field('product_id,product_type,mer_id,store_name,keyword,price,rank,sort,image,status,temp_id');
  832. break;
  833. case 1:
  834. $query->join('StoreSeckillActive S', 'S.seckill_active_id = P.active_id')
  835. ->field('P.*,S.status,S.seckill_active_id,S.end_time');
  836. break;
  837. case 2:
  838. $query->join('StoreProductPresell R', 'R.product_id = P.product_id')
  839. ->where('R.is_del', 0)
  840. ->field('P.*,R.product_presell_id,R.store_name,R.price,R.status,R.is_show,R.product_status,R.action_status');
  841. break;
  842. case 3:
  843. $query->join('StoreProductAssist A', 'A.product_id = P.product_id')
  844. ->where('A.is_del', 0)
  845. ->field('P.*,A.product_assist_id,A.store_name,A.status,A.is_show,A.product_status,A.action_status');
  846. break;
  847. case 4:
  848. $query->join('StoreProductGroup G', 'G.product_id = P.product_id')
  849. ->where('G.is_del', 0)
  850. ->field('P.*,G.product_group_id,G.price,G.status,G.is_show,G.product_status,G.action_status');
  851. break;
  852. default:
  853. break;
  854. }
  855. return $query;
  856. }
  857. public function commandChangeProductStatus($data)
  858. {
  859. $ret = [];
  860. foreach ($data as $item) {
  861. $status = 0;
  862. switch ($item['product_type']) {
  863. case 0:
  864. if ($item['is_show'] && $item['is_used']) $status = 1;
  865. $ret[] = [
  866. 'activity_id' => 0,
  867. 'product_id' => $item['product_id'],
  868. 'mer_id' => $item['mer_id'],
  869. 'keyword' => $item['keyword'],
  870. 'price' => $item['price'],
  871. 'rank' => $item['rank'],
  872. 'sort' => $item['sort'],
  873. 'image' => $item['image'],
  874. 'status' => $status,
  875. 'temp_id' => $item['temp_id'],
  876. 'store_name' => $item['store_name'],
  877. 'product_type' => $item['product_type'],
  878. ];
  879. break;
  880. case 1:
  881. if ($item['is_show'] && $item['is_used'] && $item['status'] && ($item['end_time'] > time())) $status = 1;
  882. $ret[] = [
  883. 'activity_id' => $item['seckill_active_id'],
  884. 'product_id' => $item['product_id'],
  885. 'mer_id' => $item['mer_id'],
  886. 'keyword' => $item['keyword'],
  887. 'price' => $item['price'],
  888. 'rank' => $item['rank'],
  889. 'sort' => $item['sort'],
  890. 'image' => $item['image'],
  891. 'status' => $status,
  892. 'temp_id' => $item['temp_id'],
  893. 'store_name' => $item['store_name'],
  894. 'product_type' => $item['product_type'],
  895. ];
  896. break;
  897. case 2:
  898. if ($item['is_show'] && $item['action_status'] && $item['status'] && $item['product_status']) $status = 1;
  899. $ret[] = [
  900. 'activity_id' => $item['product_presell_id'],
  901. 'product_id' => $item['product_id'],
  902. 'mer_id' => $item['mer_id'],
  903. 'keyword' => $item['keyword'],
  904. 'price' => $item['price'],
  905. 'rank' => $item['rank'],
  906. 'sort' => $item['sort'],
  907. 'image' => $item['image'],
  908. 'status' => $status,
  909. 'temp_id' => $item['temp_id'],
  910. 'store_name' => $item['store_name'],
  911. 'product_type' => $item['product_type'],
  912. ];
  913. break;
  914. case 3:
  915. if ($item['is_show'] && $item['action_status'] && $item['status'] && $item['product_status']) $status = 1;
  916. $ret[] = [
  917. 'activity_id' => $item['product_assist_id'],
  918. 'product_id' => $item['product_id'],
  919. 'mer_id' => $item['mer_id'],
  920. 'keyword' => $item['keyword'],
  921. 'price' => $item['price'],
  922. 'rank' => $item['rank'],
  923. 'sort' => $item['sort'],
  924. 'image' => $item['image'],
  925. 'status' => $status,
  926. 'temp_id' => $item['temp_id'],
  927. 'store_name' => $item['store_name'],
  928. 'product_type' => $item['product_type'],
  929. ];
  930. break;
  931. case 4:
  932. if ($item['is_show'] && $item['action_status'] && $item['status'] && $item['product_status']) $status = 1;
  933. $ret[] = [
  934. 'activity_id' => $item['product_group_id'],
  935. 'product_id' => $item['product_id'],
  936. 'mer_id' => $item['mer_id'],
  937. 'keyword' => $item['keyword'],
  938. 'price' => $item['price'],
  939. 'rank' => $item['rank'],
  940. 'sort' => $item['sort'],
  941. 'image' => $item['image'],
  942. 'status' => $status,
  943. 'temp_id' => $item['temp_id'],
  944. 'store_name' => $item['store_name'],
  945. 'product_type' => $item['product_type'],
  946. ];
  947. break;
  948. default:
  949. if ($item['is_show'] && $item['is_used']) $status = 1;
  950. $ret[] = [
  951. 'activity_id' => 0,
  952. 'product_id' => $item['product_id'],
  953. 'mer_id' => $item['mer_id'],
  954. 'keyword' => $item['keyword'],
  955. 'price' => $item['price'],
  956. 'rank' => $item['rank'],
  957. 'sort' => $item['sort'],
  958. 'image' => $item['image'],
  959. 'status' => $status,
  960. 'temp_id' => $item['temp_id'],
  961. 'store_name' => $item['store_name'],
  962. 'product_type' => $item['product_type'],
  963. ];
  964. break;
  965. }
  966. }
  967. return $ret;
  968. }
  969. /**
  970. * 软删除商户的所有商品
  971. * @param $merId
  972. * @author Qinii
  973. * @day 5/15/21
  974. */
  975. public function clearProduct($merId)
  976. {
  977. $this->getModel()::withTrashed()->where('mer_id', $merId)->delete();
  978. }
  979. /**
  980. * 获取好物推荐列表
  981. * @param array|null $good_ids
  982. * @param $is_mer
  983. * @return array|mixed
  984. *
  985. * @date 2023/10/30
  986. * @author yyw
  987. */
  988. public function getGoodList(array $good_ids, int $merId, $is_show = true)
  989. {
  990. if (empty($good_ids) && !$is_show) return [];
  991. $filed = 'product_id,image,store_name,price,create_time,is_gift_bag,is_good,is_show,mer_id,sales,status';
  992. $where = [];
  993. $limit = 30;
  994. if ($is_show) {
  995. $where = $this->productShow();
  996. $limit = 18;
  997. }
  998. $query = $this->getModel()::getDB()->where('mer_id', $merId)->where($where)
  999. ->when(!empty($good_ids), function ($query) use ($good_ids) {
  1000. $query->whereIn($this->getPk(), $good_ids);
  1001. })->field($filed);
  1002. $list = $query->limit($limit)->select()->toArray();
  1003. return $list;
  1004. }
  1005. public function deleteProductFormByFormId(int $form_id, int $mer_id)
  1006. {
  1007. return $this->getModel()::getDB()->where('mer_form_id', $form_id)->where('mer_id', $mer_id)->update(['mer_form_id' => 0]);
  1008. }
  1009. /**
  1010. * 商品详情推荐商品列表
  1011. *
  1012. * @param array $goodIds
  1013. * @param integer $merId
  1014. * @param integer $recommendNum
  1015. * @return void
  1016. */
  1017. public function recommendProduct(array $goodIds, int $merId, int $recommendNum)
  1018. {
  1019. $where = $this->productShow();
  1020. $where['mer_id'] = $merId;
  1021. $filed = 'product_id, image, store_name, price, create_time, is_gift_bag, is_good, is_show, mer_id, sales, status';
  1022. $query = $this->getModel()::getDB()->field($filed)->where($where);
  1023. if (!empty($goodIds)) {
  1024. $query->whereIn($this->getPk(), $goodIds);
  1025. $diff = bcsub($recommendNum, count($goodIds));
  1026. }
  1027. $list = $query->limit($recommendNum)->select()->toArray();
  1028. if (isset($diff) && $diff > 0) {
  1029. $diffList = $query->removeWhereField($this->getPk())->whereNotIn($this->getPk(), $goodIds)->limit($diff)->select()->toArray();
  1030. }
  1031. return array_merge($list, $diffList ?? []);
  1032. }
  1033. }