StoreProductVirtualServices.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\services\product\sku;
  3. use app\dao\product\sku\StoreProductVirtualDao;
  4. use app\services\BaseServices;
  5. /**
  6. * 规格卡密信息
  7. * Class StoreProductVirtualServices
  8. * @package app\services\product\sku
  9. * @mixin StoreProductVirtualDao
  10. */
  11. class StoreProductVirtualServices extends BaseServices
  12. {
  13. public function __construct(StoreProductVirtualDao $dao)
  14. {
  15. $this->dao = $dao;
  16. }
  17. /**
  18. * 规格中获取卡密列表
  19. * @param $unique
  20. * @param $product_id
  21. * @return array
  22. */
  23. public function getArr($unique, $product_id)
  24. {
  25. $res = $this->dao->getColumn(['attr_unique' => $unique, 'product_id' => $product_id], 'card_no,card_pwd');
  26. $data = [];
  27. foreach ($res as $item) {
  28. $data[] = ['key' => $item['card_no'], 'value' => $item['card_pwd']];
  29. }
  30. return $data;
  31. }
  32. /**
  33. * 获取订单发送卡密列表
  34. * @param array $where
  35. * @param int $limit
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getOrderCardList(array $where, int $limit = 1)
  42. {
  43. return $this->dao->getList($where, '*', 0, $limit);
  44. }
  45. /**
  46. * 保存商品规格(虚拟卡密信息)
  47. * @param int $id
  48. * @param array $valueGroup
  49. * @param int $store_id
  50. * @return bool
  51. */
  52. public function saveProductVirtual(int $id, array $valueGroup, int $store_id = 0)
  53. {
  54. foreach ($valueGroup as &$item) {
  55. if (isset($item['product_type']) && $item['product_type'] == 1 && isset($item['virtual_list']) && count($item['virtual_list'])) {
  56. $this->dao->delete(['store_id' => $store_id, 'product_id' => $id, 'attr_unique' => $item['unique'], 'uid' => 0]);
  57. $data = [];
  58. foreach ($item['virtual_list'] as &$items) {
  59. if (!$this->dao->count(['product_id' => $id, 'store_id' => $store_id, 'card_no' => $items['key'], 'card_pwd' => $items['value']])) {
  60. $data = [
  61. 'product_id' => $id,
  62. 'attr_unique' => $item['unique'],
  63. 'card_no' => $items['key'],
  64. 'card_pwd' => $items['value'],
  65. 'card_unique' => md5($item['unique'] . ',' . $items['key'] . ',' . $items['value'])
  66. ];
  67. $this->dao->save($data);
  68. }
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74. }