model = new \app\admin\model\massager\Closing; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ private function fetchWhere() { $admin = $this->auth->getUserInfo(); if (!$admin) $this->error("error"); $p_where = []; if (\E_ADMIN_TYPE::Store === $admin["type"]) { array_push($p_where, ["closing.city_code", "=", $admin["store_id"]]); // wuyong } else if (\E_ADMIN_TYPE::Agency === $admin["type"]) { array_push($p_where, ["closing.city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]); } return $p_where; } /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); $c_where = $this->fetchWhere(); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->c_selectpage($c_where); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $query = $this->model ->with(['massager']) ->where($where); foreach ($c_where as $item) { $query->where($item[0], $item[1], $item[2]); } $list = $query ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->visible(['id', 'year', 'month', 'status', 'closing_amount', 'updatetime']); $row->visible(['massager']); $row->getRelation('massager')->visible(['id', 'name']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } public function check($id = null, $check = null) { if (!$id || !$check) $this->error("参数错误!"); $record = $this->model->where([ "id" => $id, "status" => \E_MASSAGER_STATUS::Checking, ])->find(); if (!$record) $this->error("申请记录不存在!"); $massager = (new \app\api\model\massager\Massager())->findById($record["massager_id"]); if (!$massager) $this->error("助教不存在!"); $is_pass = $check === "pass"; $redLock = RedLock::of(); $massagerLock = $redLock->lock(\app\api\model\massager\Wallet::MWKey($massager["id"])); if (!is_array($massagerLock)) $this->error("请稍后再试!"); $agency = (new Admin())->findAgency($record["city_code"]); $agencyLock = false; $pLock = false; if ($agency) { $agencyLock = $redLock->lock(Admin::AgencyKey($agency->id)); if (!is_array($agencyLock)) $this->error("请稍后再试!"); } else { $pLock = $redLock->lock(Admin::PlatformKey()); if (!is_array($pLock)) $this->error("请稍后再试!"); } $locks = [$massagerLock, $agencyLock, $pLock]; $mWallet = (new \app\api\model\massager\Wallet())->getWallet($massager["id"]); Db::startTrans(); try { if ($is_pass) { $s_result = (new MassagerActionService())->fetchDiffAmountDetailsByYm($massager["id"], $record["city_code"], $record["year"], $record["month"]); if (0 === $s_result->code()) $this->error("获取数据异常"); $details = $s_result->data(); $m_profit_amount = $mWallet["profit_amount"]; $p_bills = []; $m_bills = []; if ($agency) { // 代理商存在 支付的钱由代理商支付 if ($agency["profit_amount"] < $details["diff_total_amount"]) $this->error("代理商钱包余额不足!"); $agency_profit_amount = $agency["profit_amount"]; foreach ($details["month_orders"] as $order) { array_push($p_bills, [ "identity_type" => \E_IDENTITY_TYPE::Agency, "target_id" => $agency["id"], "target_name" => $agency["nickname"], "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingExpend, "order_no" => $order["no"], "total_amount" => $order["total_real_amount"] - $order["trip_amount"], "rate" => $order["bill"]["diff_rate"], "change" => -$order["bill"]["diff_amount"], "before" => $agency_profit_amount, "after" => fixed2Float($agency_profit_amount - $order["bill"]["diff_amount"]), "createtime" => time(), "city_code" => $order["city_code"] ], [ "identity_type" => \E_IDENTITY_TYPE::Massager, "target_id" => $massager["id"], "target_name" => $massager["name"], "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingIncome, "order_no" => $order["no"], "total_amount" => $order["total_real_amount"] - $order["trip_amount"], "rate" => $order["bill"]["diff_rate"], "change" => $order["bill"]["diff_amount"], "before" => $m_profit_amount, "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]), "createtime" => time(), "city_code" => $order["city_code"] ]); $m_bills[] = [ "massager_id" => $massager["id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::ClosingIncome, "change" => $order["bill"]["diff_amount"], "before" => $m_profit_amount, "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]), "reason" => "业绩结算", "relation_no" => $order["no"], "createtime" => time() ]; $agency_profit_amount -= $order["bill"]["diff_amount"]; $m_profit_amount += $order["bill"]["diff_amount"]; } (new Admin())->where("id", $agency["id"])->setDec("profit_amount", $details["diff_total_amount"]); } else { // 不存在则平台发放 $platform = (new Admin())->where("id", 1)->find(); if (!$platform) $this->error("平台账号异常!"); $platform_profit_amount = $platform["profit_amount"]; if ($platform_profit_amount < $details["diff_total_amount"]) $this->error("平台钱包余额不足!"); foreach ($details["month_orders"] as $order) { array_push($p_bills, [ "identity_type" => \E_IDENTITY_TYPE::Platform, "target_id" => 1, "target_name" => "平台", "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingExpend, "order_no" => $order["no"], "total_amount" => $order["total_real_amount"] - $order["trip_amount"], "rate" => $order["bill"]["diff_rate"], "change" => -$order["bill"]["diff_amount"], "before" => $platform_profit_amount, "after" => fixed2Float($platform_profit_amount - $order["bill"]["diff_amount"]), "createtime" => time(), "city_code" => $order["city_code"] ], [ "identity_type" => \E_IDENTITY_TYPE::Massager, "target_id" => $massager["id"], "target_name" => $massager["name"], "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingIncome, "order_no" => $order["no"], "total_amount" => $order["total_real_amount"] - $order["trip_amount"], "rate" => $order["bill"]["diff_rate"], "change" => $order["bill"]["diff_amount"], "before" => $m_profit_amount, "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]), "createtime" => time(), "city_code" => $order["city_code"] ]); $m_bills[] = [ "massager_id" => $massager["id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::ClosingIncome, "change" => $order["bill"]["diff_amount"], "before" => $m_profit_amount, "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]), "reason" => "业绩结算", "relation_no" => $order["no"], "createtime" => time() ]; $platform_profit_amount -= $order["bill"]["diff_amount"]; $m_profit_amount += $order["bill"]["diff_amount"]; } (new Admin())->where("id", 1)->setDec("profit_amount", $details["diff_total_amount"]); } (new \app\api\model\massager\Wallet())->where("id", $mWallet["id"])->setInc("profit_amount", $details["diff_total_amount"]); $this->model->update([ "status" => "allow", "closing_rate" => $details["current_rate"], "closing_amount" => $details["diff_total_amount"], "updatetime" => time() ], ["id" => $record["id"]]); (new \app\api\model\profit\Bill())->saveAll($p_bills); (new \app\api\model\massager\Bill())->saveAll($m_bills); } else { // 拒绝 $this->model->update([ "status" => "reject" ], ["id" => $record["id"]]); } \app\api\model\system\Message::sendSystemMessage( \E_IDENTITY_TYPE::Massager, ["to_massager_id" => $record["massager_id"]], "业绩结算", "您的业绩结算申请已被管理员" . ($is_pass ? "通过!" : "拒绝!") ); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } finally { foreach ($locks as $lock) { if (is_array($lock)) $redLock->unlock($lock); } } $this->success("审核成功!"); } public function details($ids = null) { $record = $this->model->where([ "id" => $ids, ])->find(); $s_result = (new MassagerActionService())->fetchDiffAmountDetailsByYm($record["massager_id"], $record["city_code"], $record["year"], $record["month"]); if (0 === $s_result->code()) $this->error("获取数据异常"); $details = $s_result->data(); $this->view->assign([ "current_rate" => $details["current_rate"], "diff_total_amount" => $details["diff_total_amount"], "month_orders" => $details["month_orders"], ]); return $this->view->fetch(); } }