| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace app\admin\controller\massager;
- use app\admin\model\Admin;
- use app\api\service\MassagerActionService;
- use app\common\controller\Backend;
- use redis\RedLock;
- use think\Db;
- use think\Exception;
- /**
- * 业绩结算
- *
- * @icon fa fa-circle-o
- */
- class Closing extends Backend
- {
- /**
- * Closing模型对象
- * @var \app\admin\model\massager\Closing
- */
- protected $model = null;
- protected $noNeedRight = ['check', "details"];
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|