where('name', 'like', '%' . $params['search'] . '%'); } if(isset($params['branch_ids'])&&$params['branch_ids']){ $model = $model->where('branch_id', 'in', $params['branch_ids']); } if(isset($params['is_takeout']) && $params['is_takeout'] > -1){ $model = $model->where('is_takeout', '=', $params['is_takeout']); } // 查询列表数据 return $model->with(['superUser']) ->where('is_delete', '=', '0') ->order(['sort' => 'desc', 'create_time' => 'desc']) ->paginate($params); } /** * 获取列表数据 */ public static function getAll($params = []) { $model = new static(); if (!empty($params['branch_type'])) { $model = $model->where('branch_type', '=', $params['branch_type']); } // 查询列表数据 return $model->field(['branch_id,name'])->where('is_delete', '=', '0') ->order(['create_time' => 'desc']) ->select(); } /** * 添加 */ public function add($data) { // 开启事务 $this->startTrans(); try { $branch = $data['branch']; if (BranchModel::checkExist($branch['name'])) { $this->error = '分会名称已存在,请更换'; return false; } if (BranchUserModel::checkExist($branch['user_name'])) { $this->error = '登录账号已存在,请更换'; return false; } // $branch = $this->createData($branch); // 添加分会 $branch['app_id'] = self::$app_id; $this->save($branch); // 添加登录用户 $user_model = new BranchUserModel(); $user_model->save([ 'user_id' => $branch['user_id'], 'user_name' => $branch['user_name'], 'password' => salt_hash($branch['password']), 'real_name' => $branch['link_name'], 'branch_id' => $this['branch_id'], 'is_super' => 1, 'app_id' => self::$app_id, ]); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 创建数据 */ private function createData($data) { $data['app_id'] = self::$app_id; return $data; } /** * 修改 */ public function edit($data) { // 开启事务 $this->startTrans(); try { $branch = $data['branch']; if ($this['superUser'] && $branch['user_name'] != $this['superUser']['user_name'] && BranchUserModel::checkExist($branch['user_name'])) { $this->error = '登录账号已存在'; return false; } // 修改分会 $this->save($branch); // 修改登录用户 $user_model = $this['superUser']; $user_data = [ 'user_id' => $branch['user_id'], 'user_name' => $branch['user_name'] ]; if (isset($branch['password']) && !empty($branch['password'])) { $user_data['password'] = salt_hash($branch['password']); } $user_model->save($user_data); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 软删除 */ public function setDelete() { return $this->save(['is_delete' => 1]); } /** * 开启禁止 */ public function setRecycle($is_recycle) { // 开启事务 $this->startTrans(); try { //更改分会状态 $this->save(['is_recycle' => $is_recycle]); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 提现驳回:解冻资金 */ public static function backFreezeMoney($branch_id, $money) { $model = self::detail($branch_id); return $model->save([ 'money' => $model['money'] + $money, 'freeze_money' => $model['freeze_money'] - $money, ]); } /** * 提现打款成功:累积提现金额 */ public static function totalMoney($branch_id, $money) { $model = self::detail($branch_id); return $model->save([ 'freeze_money' => $model['freeze_money'] - $money, 'cash_money' => $model['cash_money'] + $money, ]); } /** * 获取分会数量 */ public static function getTotal($where) { $model = new static; return $model->where($where)->count(); } /** * 获取分会总数量 */ public static function getBranchTotalByDay($day) { $startTime = strtotime($day); return (new static())->where('create_time', '>=', $startTime) ->where('create_time', '<', $startTime + 86400) ->count(); } /** * 获取分会统计数量 */ public function getBranchData($startDate = null, $endDate = null, $type) { $model = $this; if(!is_null($startDate)){ $model = $model->where('create_time', '>=', strtotime($startDate)); } if(is_null($endDate)){ $model = $model->where('create_time', '<', strtotime($startDate) + 86400); }else{ $model = $model->where('create_time', '<', strtotime($endDate) + 86400); } if($type == 'branch_total' || $type == 'branch_add'){ return $model->count(); } return 0; } /** * 获取平台的总销售额 */ public function getTotalMoney($type = 'total_money',$postData=[]) { $model = $this; if(!empty($postData["branch_name"])){ //获取商户id $branch_id = BranchModel::getBranchIdByName($postData['branch_name']); $model = $model->where('branch_id', '=', $branch_id); } if($type == 'total'){ return $model->sum('total_money'); } else if($type == 'money'){ return $model->sum('money'); } else if($type == 'freeze_money'){ return $model->sum('freeze_money'); } else if($type == 'cash_money'){ return $model->sum('cash_money'); } else if($type == 'deposit_money'){ return $model->sum('deposit_money'); } return 0; } //判断用户是否申请 public function isApply($user_id) { return $this->where('user_id', '=', $user_id)->where('status', '=', 0)->count(); } /** * 获取列表数据 */ public function getStatisticsList($params,$is_page = true) { $model = $this; if (isset($params['search']) && $params['search']) { $model = $model->where('name', 'like', '%' . $params['search'] . '%'); } //搜索时间段 $create_time = empty($params['create_time']) ? 0 : $params['create_time']; $model = $model->field("branch_id,name") ->where('is_delete', '=', '0') ->order(['create_time' => 'desc']); // 查询列表数据 if($is_page){ $list = $model->paginate($params); foreach ($list->toArray()["data"] as $key => $value) { $list[$key]["coupon_money"] = orderModel::getCouponMoneyByBranch($value["branch_id"],$create_time); $list[$key]["coupon_num"] = orderModel::getCouponNumByBranch($value["branch_id"],$create_time); $list[$key]["store_coupon_money"] = StoreCouponModel::getCouponMoneyByBranch($value["branch_id"],$create_time); $list[$key]["store_coupon_num"] = StoreCouponModel::getCouponNumByBranch($value["branch_id"],$create_time); $list[$key]["total_coupon_money"] = $list[$key]["coupon_money"] + $list[$key]["store_coupon_money"]; } }else{ $list = $model->select(); foreach ($list as $key => $value) { $list[$key]["coupon_money"] = orderModel::getCouponMoneyByBranch($value["branch_id"],$create_time); $list[$key]["coupon_num"] = orderModel::getCouponNumByBranch($value["branch_id"],$create_time); $list[$key]["store_coupon_money"] = StoreCouponModel::getCouponMoneyByBranch($value["branch_id"],$create_time); $list[$key]["store_coupon_num"] = StoreCouponModel::getCouponNumByBranch($value["branch_id"],$create_time); $list[$key]["total_coupon_money"] = $list[$key]["coupon_money"] + $list[$key]["store_coupon_money"]; } } return $list; } /** * 订单导出 */ public function exportList($params) { // 获取订单列表 $list = $this->getStatisticsList($params,false); // 导出excel文件 return (new Exportservice)->branchStatisticsList($list); } }