FlashSale.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\model\unishop;
  3. use addons\unishop\model\Product;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. use traits\model\SoftDelete;
  8. class FlashSale extends Model
  9. {
  10. use SoftDelete;
  11. //数据库
  12. protected $connection = 'database';
  13. // 表名
  14. protected $name = 'unishop_flash_sale';
  15. // 自动写入时间戳字段
  16. protected $autoWriteTimestamp = 'int';
  17. // 定义时间戳字段名
  18. protected $createTime = 'createtime';
  19. protected $updateTime = 'updatetime';
  20. protected $deleteTime = 'deletetime';
  21. // 追加属性
  22. protected $append = [
  23. 'status_text',
  24. 'starttime_text',
  25. 'endtime_text',
  26. 'current_state'
  27. ];
  28. // 已归档
  29. const STATUS_YES = 1; // 是
  30. const STATUS_NO = 0; // 否
  31. // 已上架
  32. const SWITCH_YES = 1; // 是
  33. const SWITCH_NO = 0; // 否
  34. /**
  35. * 获取当前状态
  36. */
  37. public function getCurrentStateAttr($value, $data)
  38. {
  39. $time = time();
  40. switch (true) {
  41. case $data['starttime'] > $time:
  42. $result = __('Not started');
  43. break;
  44. case $data['starttime'] <= $time && $time < $data['endtime']:
  45. $result = __('On going');
  46. break;
  47. case $time >= $data['endtime']:
  48. $result = __('Has ended');
  49. break;
  50. default:
  51. $result = __('Nothing');
  52. }
  53. return $result;
  54. }
  55. public function getStatusList()
  56. {
  57. return ['0' => __('Status 0'), '1' => __('Status 1')];
  58. }
  59. public function getStatusTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  62. $list = $this->getStatusList();
  63. return isset($list[$value]) ? $list[$value] : '';
  64. }
  65. public function getStarttimeTextAttr($value, $data)
  66. {
  67. $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
  68. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  69. }
  70. public function getEndtimeTextAttr($value, $data)
  71. {
  72. $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
  73. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  74. }
  75. protected function setStarttimeAttr($value)
  76. {
  77. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  78. }
  79. protected function setEndtimeAttr($value)
  80. {
  81. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  82. }
  83. /**
  84. * 关联产品
  85. * @return \think\model\relation\HasMany
  86. */
  87. public function product()
  88. {
  89. return $this->hasMany('flashProduct', 'flash_id', 'id');
  90. }
  91. /**
  92. * 判断能不能修改
  93. * 已归档、已开始、上架状态的秒杀不能够修改。
  94. */
  95. public function checkItCanEdit()
  96. {
  97. if ($this['switch'] == self::SWITCH_YES || $this['status'] == self::STATUS_YES || $this['starttime'] < time()) {
  98. throw new Exception('已归档、已开始、上架状态的秒杀信息不能够修改。');
  99. }
  100. return true;
  101. }
  102. /**
  103. * 归档减库存
  104. */
  105. public function activityFiled($params, $specNumber)
  106. {
  107. $productExtend = new \addons\unishop\extend\Product;
  108. $key = 0;
  109. $prefix = \think\Config::get('database.prefix');
  110. foreach ($specNumber as $spec => $number) {
  111. $result = 0;
  112. if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
  113. $result = Db::execute("UPDATE fa_unishop_product SET stock = stock-{$number}, real_sales = real_sales+{$number} WHERE id = {$params[$key]['id']}");
  114. } else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
  115. $info = $productExtend->getBaseData($params[$key], $spec);
  116. // mysql<5.7.13时用
  117. //if (mysql < 5.7.13) {
  118. $spec = str_replace(',', '","', $spec);
  119. $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
  120. $stock = $info['stock'] - $number;
  121. $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
  122. $sql = 'UPDATE ' . $prefix . "unishop_product SET stock = stock-{$number}, real_sales = real_sales+{$number}, `specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']}";
  123. $result = Db::execute($sql);
  124. //}
  125. //下面语句直接操作JSON
  126. //if (mysql >= 5.7.13) {
  127. //$info['stock'] -= $number;
  128. //$result = Db::execute("UPDATE fa_unishop_product SET stock = stock-{$number}, real_sales = real_sales+{$number}, specTableList = JSON_REPLACE(specTableList, '$[{$info['key']}].stock', {$info['stock']}) WHERE id = {$params[$key]['id']}");
  129. //}
  130. }
  131. if ($result == 0) { // 锁生效
  132. throw new Exception('失败');
  133. }
  134. $key++;
  135. }
  136. }
  137. }