card_key.model.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=0);
  3. defined('InShopNC') or exit('Access Invalid!');
  4. use const mtopcard\UnusedCard;
  5. use const mtopcard\ReserveCard;
  6. use const mtopcard\AssignedCard;
  7. use const mtopcard\FreezedCard;
  8. class card_keyModel extends Model
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct('card_key');
  13. }
  14. public function getCardByOrderId($order_id)
  15. {
  16. return $this->field('*')->where(['order_id' => $order_id])->select();
  17. }
  18. public function getCards($cond)
  19. {
  20. return $this->field('*')->where($cond)->select();
  21. }
  22. public function addCard($params)
  23. {
  24. return $this->insert($params);
  25. }
  26. public function getCardsList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '')
  27. {
  28. $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->select();
  29. if (empty($list)) return [];
  30. return $list;
  31. }
  32. public function addCards($params)
  33. {
  34. return $this->insertAll($params);
  35. }
  36. public function getUsable($card_type, $amount,$store_ids)
  37. {
  38. if(empty($store_ids)) {
  39. Log::record("获取卡密需要指定获取的店铺",Log::ERR);
  40. return [];
  41. }
  42. return $this->where(['card_state' => UnusedCard, 'card_type' => $card_type, 'amount' => $amount, 'store_id' => ['in',$store_ids]])
  43. ->lock(true)
  44. ->master(true)
  45. ->find();
  46. }
  47. public function reserve($card_id)
  48. {
  49. return $this->where(['card_id' => $card_id, 'card_state' => UnusedCard])
  50. ->update(['card_state' => ReserveCard]);
  51. }
  52. public function assign($order_id, $oper = "")
  53. {
  54. if(empty($oper)) {
  55. return $this->where(['order_id' => $order_id, 'card_state' => ReserveCard])
  56. ->update(['card_state' => AssignedCard, 'assigned_time' => time()]);
  57. }
  58. else {
  59. return $this->where(['order_id' => $order_id, 'card_state' => ReserveCard])
  60. ->update(['card_state' => AssignedCard, 'allocator' => $oper,'assigned_time' => time()]);
  61. }
  62. }
  63. public function reuse_card($card_id)
  64. {
  65. //只有已经预留了的卡才可以重新回收使用。
  66. return $this->where(['card_id' => $card_id, 'card_state' => ReserveCard])
  67. ->update(['card_state' => UnusedCard, 'order_id' => 0,
  68. 'member_id' => 0,
  69. 'out_store_id' => 0,
  70. 'receive_card_no' => '',
  71. 'receive_card_type' => 0,
  72. 'reserved_time' => 0,
  73. 'assigned_time' => 0]);
  74. }
  75. public function reuse($order_id)
  76. {
  77. //只有已经预留了的卡才可以重新回收使用。
  78. return $this->where(['order_id' => $order_id, 'card_state' => ReserveCard])
  79. ->update(['card_state' => UnusedCard, 'order_id' => 0,
  80. 'member_id' => 0,
  81. 'out_store_id' => 0,
  82. 'receive_card_no' => '',
  83. 'receive_card_type' => 0,
  84. 'reserved_time' => 0,
  85. 'assigned_time' => 0]);
  86. }
  87. public function freeze($order_id,$errmsg = '')
  88. {
  89. //只有已经预留了的卡才可以重新回收使用。
  90. return $this->where(['order_id' => $order_id, 'card_state' => ReserveCard])
  91. ->update(['card_state' => FreezedCard, 'errmsg' => $errmsg, 'order_id' => 0]);
  92. }
  93. public function manual_freeze($card_id,$errmsg = '')
  94. {
  95. //只有已经预留了的卡才可以重新回收使用。
  96. return $this->where(['card_id' => $card_id, 'card_state' => UnusedCard])
  97. ->lock(true)
  98. ->update(['card_state' => FreezedCard, 'errmsg' => $errmsg, 'order_id' => 0]);
  99. }
  100. public function unfreeze($card_id)
  101. {
  102. //只有已经预留了的卡才可以重新回收使用。
  103. return $this->where(['card_id' => $card_id, 'card_state' => FreezedCard])
  104. ->update(['card_state' => UnusedCard,
  105. 'order_id' => 0,
  106. 'member_id' => 0,
  107. 'out_store_id' => 0,
  108. 'receive_card_no' => '',
  109. 'receive_card_type' => 0,
  110. 'reserved_time' => 0,
  111. 'assigned_time' => 0,
  112. 'errmsg' => '']);
  113. }
  114. }