CateMapService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace addons\TinyShop\services\product;
  3. use Yii;
  4. use common\components\Service;
  5. use addons\TinyShop\common\models\product\CateMap;
  6. /**
  7. * Class CateMapService
  8. * @package addons\TinyShop\services\product
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class CateMapService extends Service
  12. {
  13. /**
  14. * 创建关联
  15. *
  16. * @param $product_id
  17. * @param array $cate_ids
  18. */
  19. public function create($product_id, array $cate_ids, $merchant_id = 0)
  20. {
  21. CateMap::deleteAll(['product_id' => $product_id, 'merchant_id' => $merchant_id]);
  22. $rows = [];
  23. $cate = Yii::$app->tinyShopService->productCate->findAllByIds($cate_ids);
  24. foreach ($cate as $item) {
  25. $rows[] = [
  26. 'cate_id' => $item['id'],
  27. 'product_id' => $product_id,
  28. 'merchant_id' => $item['merchant_id'],
  29. ];
  30. }
  31. // 判断插入
  32. $field = ['cate_id', 'product_id', 'merchant_id'];
  33. !empty($rows) && Yii::$app->db->createCommand()->batchInsert(CateMap::tableName(), $field, $rows)->execute();
  34. }
  35. /**
  36. * 获取所有的分类id
  37. *
  38. * @param $product_id
  39. * @return array
  40. */
  41. public function findByProductId($product_id, $merchant_id = '')
  42. {
  43. return CateMap::find()
  44. ->select(['cate_id'])
  45. ->where(['product_id' => $product_id])
  46. ->andFilterWhere(['merchant_id' => $merchant_id])
  47. ->column();
  48. }
  49. /**
  50. * 获取所有的分类id
  51. *
  52. * @param $product_id
  53. * @return array|CateMap
  54. */
  55. public function findOneByProductId($product_id)
  56. {
  57. return CateMap::find()
  58. ->with(['cate'])
  59. ->where(['product_id' => $product_id])
  60. ->andWhere(['merchant_id' => 0])
  61. ->one();
  62. }
  63. /**
  64. * 获取所有的商品id
  65. *
  66. * @param $product_id
  67. * @return array
  68. */
  69. public function findByCateIds($cate_ids)
  70. {
  71. return CateMap::find()
  72. ->select(['product_id'])
  73. ->where(['in', 'cate_id', $cate_ids])
  74. ->column();
  75. }
  76. /**
  77. * @param $cate_id
  78. * @param $product_id
  79. */
  80. public function findSaveById($cate_id, $product_id)
  81. {
  82. CateMap::deleteAll(['product_id' => $product_id, 'merchant_id' => 0]);
  83. $model = new CateMap();
  84. $model->merchant_id = 0;
  85. $model->cate_id = $cate_id;
  86. $model->product_id = $product_id;
  87. $model->save();
  88. }
  89. }
粤ICP备19079148号