| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace app\common\model;
- use think\Db;
- class OrderProductModel extends BaseModel
- {
- protected $table = 'erp_order_product';
- protected function genSchema(array $schema)
- {
- // TODO: Implement genSchema() method.
- }
- public function product()
- {
- return self::hasOne(ProductModel::class, 'id', 'product_id')->where('is_delete', 0);
- }
- public function payments()
- {
- return self::hasMany(OrderPaymentModel::class, 'order_id', 'order_id')->where('is_delete', 0);
- }
- /**
- * @param $o_id
- * @param int $is_pay
- * @return OrderProductModel[]|array|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function findByOrderId($o_id, $is_pay = 1)
- {
- return $this->where('is_delete', 0)
- ->where('order_id', $o_id)
- ->where('is_pay', $is_pay)
- ->select();
- }
- /**
- * @param $o_id
- * @param int $page
- * @param int $size
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function fetchByOrderId($o_id, $page = 1, $size = 10)
- {
- return $this->where('is_delete', 0)
- ->where('order_id', $o_id)
- ->page($page)
- ->paginate($size);
- }
- /**
- * @param $c_id
- * @param int $is_upload_numerology
- * @param int $is_upload
- * @return int
- * @throws \think\db\exception\DbException
- */
- public function countByCustomerId($c_id, $is_upload_numerology = 1, $is_upload = null)
- {
- $where = [
- ['is_delete', '=', 0],
- ['customer_id', '=', $c_id],
- ['is_upload_numerology', '=', $is_upload_numerology]
- ];
- if ($is_upload) {
- array_push($where, ['is_upload', '=', $is_upload]);
- }
- return $this->where($where)
- ->count();
- }
- public function countByAdviser($adviser_1_id, $start_time = null, $end_time = null)
- {
- $where = [
- ['is_delete', '=', 0],
- ['adviser_1_id', '=', $adviser_1_id],
- ['is_pay', '=', 1]
- ];
- if ($start_time) array_push($where, ['create_time', '>=', $start_time]);
- if ($end_time) array_push($where, ['create_time', '<=', $end_time]);
- return [
- $this->where($where)->sum('real_price'),
- $this->where(array_merge($where, [
- ['is_upload_numerology', '=', 1],
- ['is_upload', '=', 1]
- ]))->count(),
- $this->where(array_merge($where, [['is_upload_numerology', '=', 1]]))->count(),
- $this->where(array_merge($where, [['is_serve', '=', 0]]))->sum('real_price'),
- $this->where(array_merge($where, [['teacher_1_id', '>', 0]]))->sum('real_price'),
- $this->where(array_merge($where, [['is_serve', '=', 1]]))->sum('real_price'),
- ];
- }
- public function countByAdviserProductCategoryIds($adviser_1_id, $category_ids = [], $start_time = null, $end_time = null)
- {
- if (count($category_ids) == 0) return [0, 0];
- $where = [
- ['is_delete', '=', 0],
- ['adviser_1_id', '=', $adviser_1_id],
- ['is_pay', '=', 1],
- ['product_category_id', 'in', $category_ids]
- ];
- if ($start_time) array_push($where, ['create_time', '>=', $start_time]);
- if ($end_time) array_push($where, ['create_time', '<=', $end_time]);
- return [$this->where($where)->sum('real_price'), $this->where($where)->count()];
- }
- public function findByPaginate(array $params)
- {
- return $this->genWhere($params)
- ->paginate([
- 'list_rows' => 10,
- "query" => $params
- ]);
- }
- public function report($params)
- {
- return $this->genWhere($params)
- ->select();
- }
- public function genWhere($params)
- {
- $self_where = [
- ['erp_order_product.is_delete', '=', 0],
- ['erp_order_product.is_pay', '=', 1],
- ];
- $product_where = [];
- if ($params['order_no']) {
- array_push($self_where, ["{$this->table}.order_no", "like", "%{$params['order_no']}%"]);
- }
- if ($params['product_name']) {
- array_push($self_where, ["{$this->table}.product_name", "like", "%{$params['product_name']}%"]);
- }
- if ($params['adviser_id'] > 0) {
- array_push($self_where, ["{$this->table}.adviser_1_id|{$this->table}.adviser_2_id", "=", $params['adviser_id']]);
- }
- if ($params['teacher_id'] > 0) {
- array_push($self_where, ["{$this->table}.teacher_1_id|{$this->table}.teacher_2_id", "=", $params['teacher_id']]);
- }
- if ($params['date_range']) {
- $date_range = explode('/', preg_replace("/\s| /", "", $params['date_range']));
- $start_time = strtotime($date_range[0]);
- $end_time = strtotime($date_range[1]);
- if ($start_time > 0) {
- array_push($self_where, ["{$this->table}.create_time", ">=", $start_time]);
- }
- if ($end_time > 0) {
- array_push($self_where, ["{$this->table}.create_time", "<", $end_time]);
- }
- }
- if ($params['bar_code']) {
- array_push($product_where, ['bar_code', "like", "%{$params['bar_code']}%"]);
- }
- if ($params['company_id'] && $params['company_id'] > 0) {
- array_push($product_where, ['company_id', "=", $params['company_id']]);
- }
- $query = $this->where($self_where)
- ->with(['product', 'product.company', 'payments'])
- ->hasWhere('product', $product_where);
- if ($params['channel_id'] > 0) {
- $query->where("FIND_IN_SET('{$params['channel_id']}', OrderProductModel.channel_ids)");
- }
- return $query;
- }
- }
|