| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace app\common\model;
- use think\Model;
- use Exception;
- class ProductTransfer extends Model
- {
- protected $name = "product_transfer";
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'create_time_text',
- 'update_time_text'
- ];
- const Stop = 0;
- const Normal = 1;
- //状态
- public $status_list = [
- '-1' => '全部',
- self::Stop => '停用',
- self::Normal => '正常'
- ];
- //添加/更新转让订单
- public static function setTransferOrder($uid, $product_id, $area_id, $feeAmount, $orderNo, $params)
- {
- $rows = self::where('order_id', $params['order_id'])->find();
- if(empty($rows)){
- // 生成订单
- $order_data['user_id'] = $uid;
- $order_data['price'] = $params['price'];
- $order_data['product_id'] = $product_id;
- $order_data['fees'] = $feeAmount;
- $order_data['area_id'] = $area_id;
- $order_data['order_id'] = $params['order_id']; //订单ID
- $order_data['order_no'] = $orderNo;
- return self::create($order_data);
- }else{
- $rows->price = $params['price'];
- $rows->fees = $feeAmount;
- $rows->status = self::Normal;
- return $rows->save();
- }
- }
- //获取转让订单
- public static function getTransferMinPrice(int $product_id)
- {
- return self::where('product_id', $product_id)->where('status', self::Normal)->min('price');
- }
- //获取最小价格
- public static function getTransferMinPriceByProduct(int $product_id, float $price)
- {
- return self::where('product_id', $product_id)->where('status', self::Normal)->where('price', '>', $price)->min('price');
- }
-
- //产品
- public function products()
- {
- return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
- }
- //用户 user_id
- public function users()
- {
- return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
- }
- 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);
- }
- }
|