| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace app\common\model;
- use think\Model;
- class ProductBuying extends Model
- {
- protected $name = "product_buying";
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'create_time_text',
- 'update_time_text'
- ];
- //1 正常 2 已出售 0 关闭
- const Normal = 1;
- const SaleOut = 2;
- const Close = 0;
- /*
- * 订单状态
- */
- public $aciton_name = [
- '-1' => '全部',
- self::Normal => '待成交',
- self::SaleOut => '已成交',
- self::Close => '已取消',
- ];
- public static function getProductBuyingMaxPrice(int $productId)
- {
- return self::where('product_id', $productId)->max('min_price');
- }
- //用户购买套数
- public static function getProductBuyingCount(int $userId, int $productId)
- {
- return self::where('product_id', $productId)->where('user_id', $userId)->where('status', self::Normal)->count();
- }
- //创建订单
- public static function setCreateBuying(int $userId, int $productId, int $num, float $minPrice, float $totalPrice)
- {
- return self::create([
- 'user_id' => $userId,
- 'product_id' => $productId,
- 'num' => $num,
- 'min_price' => $minPrice,
- 'total_price' => $totalPrice
- ]);
- }
- //获取订单列表
- public function getBuyingList(int $userId, int $page, int $size, int $status)
- {
- $where = [
- 'user_id' => $userId,
- 'status' => $status,
- ];
- $list = $this->where($where)->page($page, $size)->order('id desc')->select();
- $count = $this->where($where)->count();
- return compact('list', 'count');
- }
- public function getCreateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- public function getUpdateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setCreateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- protected function setUpdateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- }
|