180 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			180 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
| <?php
 | |
| 
 | |
| namespace app\model;
 | |
| 
 | |
| use think\Model;
 | |
| 
 | |
| class Order extends BaseModel
 | |
| {
 | |
|     use SearcherTrait;
 | |
| 
 | |
|     protected $name = 'orders';
 | |
|     const PRODUCT_TYPE_BAO_YUE = 2; // 连续包月类型
 | |
|     const PRODUCT_TYPE_NORMAL = 1; // 普通商品
 | |
|     const STATUS_WAIT_SIGN = 0; // 待签约
 | |
|     const STATUS_SIGNED = 1; // 已签约
 | |
|     const STATUS_WAIT_RECHARGE = 2; // 待充值
 | |
|     const STATUS_RECHARGE_ING = 3; // 充值中
 | |
|     const STATUS_RECHARGE_SUCCESS = 4; // 充值完成
 | |
|     const STATUS_RECHARGE_FAIL = 5; // 充值失败
 | |
|     const STATUS_RECHARGE_CLOSE = 6; // 已取消
 | |
|     // 支付状态
 | |
|     const PAY_STATUS_WAIT = 1; // 待支付
 | |
|     const PAY_STATUS_PAID = 2; // 已支付
 | |
|     const PAY_STATUS_FAIL = 3; // 付款失败
 | |
|     // 退款状态
 | |
|     const REFUND_STATUS_WAIT = 1;
 | |
|     const REFUND_STATUS_SUCCESS = 2;
 | |
|     const REFUND_STATUS_FAIL = 3;
 | |
|     //  重试状态
 | |
|     const RETRY_STATUS_NO = 0;  //  未重试
 | |
|     const RETRY_STATUS_YES = 1; //  已重试
 | |
| 
 | |
|     const STATUS_TEXT = [
 | |
|         self::STATUS_WAIT_SIGN => '待签约',
 | |
|         self::STATUS_SIGNED => '已签约',
 | |
|         self::STATUS_WAIT_RECHARGE => '待充值',
 | |
|         self::STATUS_RECHARGE_ING => '充值中',
 | |
|         self::STATUS_RECHARGE_SUCCESS => '已完成',
 | |
|         self::STATUS_RECHARGE_FAIL => '充值失败',
 | |
|         self::STATUS_RECHARGE_CLOSE => '已取消'
 | |
|     ];
 | |
| 
 | |
|     /**
 | |
|      * 根据userId获取连续包月的订单数
 | |
|      * @param int $userId
 | |
|      * @return int
 | |
|      * @throws \think\db\exception\DbException
 | |
|      */
 | |
|     public static function getMonthOrderCountByUserId(int $userId): int
 | |
|     {
 | |
|         return self::where(['user_id' => $userId,
 | |
|             'type' => self::PRODUCT_TYPE_BAO_YUE,
 | |
|             'pay_status' => self::PAY_STATUS_PAID])->count();
 | |
|     }
 | |
| 
 | |
|     public static function updateChangeData($data, $orderNumber)
 | |
|     {
 | |
|         return self::where('order_number', $orderNumber)->update($data);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 根据用户id查询已支付连续扣款订单
 | |
|      * @param int $userId
 | |
|      * @return Order|array|mixed|Model
 | |
|      * @throws \think\db\exception\DataNotFoundException
 | |
|      * @throws \think\db\exception\ModelNotFoundException
 | |
|      */
 | |
|     public static function getOneMonthByUserId(int $userId)
 | |
|     {
 | |
|         return self::where(['user_id' => $userId,
 | |
|             'type' => self::PRODUCT_TYPE_BAO_YUE,
 | |
|             'pay_status' => self::PAY_STATUS_PAID
 | |
|         ])->findOrFail();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 根据订单号获取订单信息
 | |
|      * @param string $orderNumber
 | |
|      * @param string $field
 | |
|      * @return Order|array|mixed|Model
 | |
|      */
 | |
|     public static function getByOrderNumber(string $orderNumber, string $field = '*')
 | |
|     {
 | |
|         return self::where('order_number', $orderNumber)->field($field)->findOrEmpty();
 | |
|     }
 | |
| 
 | |
|     public function searchOrderNumberAttr($query, $value, $data)
 | |
|     {
 | |
|         if (!empty($value)) {
 | |
|             $query->where('order_number', $value);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function searchTypeAttr($query, $value, $data)
 | |
|     {
 | |
|         if (!empty($value)) {
 | |
|             $query->where('type', $value);
 | |
|         }
 | |
|     }
 | |
|     public function getWaitSignOrderByUserId(int $userId)
 | |
|     {
 | |
|         return self::where(['user_id' => $userId, 'order_status' => Order::STATUS_WAIT_SIGN])->findOrEmpty();
 | |
| 
 | |
|     }
 | |
| 
 | |
|     public function searchUserIdAttr($query, $value, $data)
 | |
|     {
 | |
|         if (!empty($value)) {
 | |
|             $query->where('user_id', $value);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 根据账号获取该账号近一年是否已经签约扣过款
 | |
|      * @param string $account
 | |
|      * @return int
 | |
|      * @throws \think\db\exception\DbException
 | |
|      */
 | |
|     public static function getAgreeOrderLastYearByAccount(string $account)
 | |
|     {
 | |
|         return self::where('account', $account)->whereIn('order_status', [2,3,4])->whereTime('create_time', '-1 year')->count();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 根据userId 获取该账号近一年是否已经扣过款
 | |
|      * @param int $userId
 | |
|      * @return int
 | |
|      * @throws \think\db\exception\DbException
 | |
|      */
 | |
|     public static function getAgreeOrderLastYearByUserId(int $userId)
 | |
|     {
 | |
|         return self::where('user_id', $userId)->whereIn('order_status', [2,3,4])->whereTime('create_time', '-1 year')->count();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 根据协议号获取待签约订单
 | |
|      * @param string $agreementId
 | |
|      * @return Order|array|mixed|Model
 | |
|      */
 | |
|     public static function getWaitSignByAgreementId(string $agreementId)
 | |
|     {
 | |
|         return self::where(['agreement_id' => $agreementId, 'order_status' => Order::STATUS_WAIT_SIGN])->findOrEmpty();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 根据协议号获取最近一次扣款订单
 | |
|      * @param string $agreementId
 | |
|      * @return Order|array|mixed|Mode
 | |
|      */
 | |
|     public static  function getPaidOrderByAgreementId(string $agreementId)
 | |
|     {
 | |
|         return self::where(['agreement_id' => $agreementId])->order('id', 'desc')->findOrEmpty();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取该签约生成扣款订单次数
 | |
|      * @param string $agreementId
 | |
|      * @return int
 | |
|      * @throws \think\db\exception\DbException
 | |
|      */
 | |
|     public static function getCountByAgreementId(string $agreementId)
 | |
|     {
 | |
|         return self::where(['agreement_id' => $agreementId])->count();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 最近一个月订单数量
 | |
|      * @return int
 | |
|      * @throws \think\db\exception\DbException
 | |
|      */
 | |
|     public  function getCountLastMonth()
 | |
|     {
 | |
|         return self::where('order_status', Order::STATUS_RECHARGE_SUCCESS)->whereTime('create_time', '-1 month')->count();
 | |
|     }
 | |
|     public static function getByAgreementId(string $agreementId)
 | |
|     {
 | |
|         return self::where(['agreement_id' => $agreementId])->findOrEmpty();
 | |
|     }
 | |
| 
 | |
| } |