Closing.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\admin\controller\massager;
  3. use app\admin\model\Admin;
  4. use app\api\service\MassagerActionService;
  5. use app\common\controller\Backend;
  6. use redis\RedLock;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * 业绩结算
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Closing extends Backend
  15. {
  16. /**
  17. * Closing模型对象
  18. * @var \app\admin\model\massager\Closing
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['check', "details"];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\massager\Closing;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. private function fetchWhere()
  34. {
  35. $admin = $this->auth->getUserInfo();
  36. if (!$admin)
  37. $this->error("error");
  38. $p_where = [];
  39. if (\E_ADMIN_TYPE::Store === $admin["type"]) {
  40. array_push($p_where, ["closing.city_code", "=", $admin["store_id"]]); // wuyong
  41. } else if (\E_ADMIN_TYPE::Agency === $admin["type"]) {
  42. array_push($p_where, ["closing.city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]);
  43. }
  44. return $p_where;
  45. }
  46. /**
  47. * 查看
  48. */
  49. public function index()
  50. {
  51. //当前是否为关联查询
  52. $this->relationSearch = true;
  53. //设置过滤方法
  54. $this->request->filter(['strip_tags', 'trim']);
  55. $c_where = $this->fetchWhere();
  56. if ($this->request->isAjax()) {
  57. //如果发送的来源是Selectpage,则转发到Selectpage
  58. if ($this->request->request('keyField')) {
  59. return $this->c_selectpage($c_where);
  60. }
  61. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  62. $query = $this->model
  63. ->with(['massager'])
  64. ->where($where);
  65. foreach ($c_where as $item) {
  66. $query->where($item[0], $item[1], $item[2]);
  67. }
  68. $list = $query
  69. ->order($sort, $order)
  70. ->paginate($limit);
  71. foreach ($list as $row) {
  72. $row->visible(['id', 'year', 'month', 'status', 'closing_amount', 'updatetime']);
  73. $row->visible(['massager']);
  74. $row->getRelation('massager')->visible(['id', 'name']);
  75. }
  76. $result = array("total" => $list->total(), "rows" => $list->items());
  77. return json($result);
  78. }
  79. return $this->view->fetch();
  80. }
  81. public function check($id = null, $check = null)
  82. {
  83. if (!$id || !$check)
  84. $this->error("参数错误!");
  85. $record = $this->model->where([
  86. "id" => $id,
  87. "status" => \E_MASSAGER_STATUS::Checking,
  88. ])->find();
  89. if (!$record)
  90. $this->error("申请记录不存在!");
  91. $massager = (new \app\api\model\massager\Massager())->findById($record["massager_id"]);
  92. if (!$massager)
  93. $this->error("助教不存在!");
  94. $is_pass = $check === "pass";
  95. $redLock = RedLock::of();
  96. $massagerLock = $redLock->lock(\app\api\model\massager\Wallet::MWKey($massager["id"]));
  97. if (!is_array($massagerLock))
  98. $this->error("请稍后再试!");
  99. $agency = (new Admin())->findAgency($record["city_code"]);
  100. $agencyLock = false;
  101. $pLock = false;
  102. if ($agency) {
  103. $agencyLock = $redLock->lock(Admin::AgencyKey($agency->id));
  104. if (!is_array($agencyLock))
  105. $this->error("请稍后再试!");
  106. } else {
  107. $pLock = $redLock->lock(Admin::PlatformKey());
  108. if (!is_array($pLock))
  109. $this->error("请稍后再试!");
  110. }
  111. $locks = [$massagerLock, $agencyLock, $pLock];
  112. $mWallet = (new \app\api\model\massager\Wallet())->getWallet($massager["id"]);
  113. Db::startTrans();
  114. try {
  115. if ($is_pass) {
  116. $s_result = (new MassagerActionService())->fetchDiffAmountDetailsByYm($massager["id"], $record["city_code"], $record["year"], $record["month"]);
  117. if (0 === $s_result->code())
  118. $this->error("获取数据异常");
  119. $details = $s_result->data();
  120. $m_profit_amount = $mWallet["profit_amount"];
  121. $p_bills = [];
  122. $m_bills = [];
  123. if ($agency) { // 代理商存在 支付的钱由代理商支付
  124. if ($agency["profit_amount"] < $details["diff_total_amount"])
  125. $this->error("代理商钱包余额不足!");
  126. $agency_profit_amount = $agency["profit_amount"];
  127. foreach ($details["month_orders"] as $order) {
  128. array_push($p_bills, [
  129. "identity_type" => \E_IDENTITY_TYPE::Agency,
  130. "target_id" => $agency["id"],
  131. "target_name" => $agency["nickname"],
  132. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingExpend,
  133. "order_no" => $order["no"],
  134. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  135. "rate" => $order["bill"]["diff_rate"],
  136. "change" => -$order["bill"]["diff_amount"],
  137. "before" => $agency_profit_amount,
  138. "after" => fixed2Float($agency_profit_amount - $order["bill"]["diff_amount"]),
  139. "createtime" => time(),
  140. "city_code" => $order["city_code"]
  141. ], [
  142. "identity_type" => \E_IDENTITY_TYPE::Massager,
  143. "target_id" => $massager["id"],
  144. "target_name" => $massager["name"],
  145. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingIncome,
  146. "order_no" => $order["no"],
  147. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  148. "rate" => $order["bill"]["diff_rate"],
  149. "change" => $order["bill"]["diff_amount"],
  150. "before" => $m_profit_amount,
  151. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  152. "createtime" => time(),
  153. "city_code" => $order["city_code"]
  154. ]);
  155. array_push($m_bills, [
  156. "massager_id" => $massager["id"],
  157. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  158. "change_type" => \E_M_BILL_CHANGE_TYPE::ClosingIncome,
  159. "change" => $order["bill"]["diff_amount"],
  160. "before" => $m_profit_amount,
  161. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  162. "reason" => "业绩结算",
  163. "relation_no" => $order["no"],
  164. "createtime" => time()
  165. ]);
  166. $agency_profit_amount -= $order["bill"]["diff_amount"];
  167. $m_profit_amount += $order["bill"]["diff_amount"];
  168. }
  169. (new Admin())->where("id", $agency["id"])->setDec("profit_amount", $details["diff_total_amount"]);
  170. } else { // 不存在则平台发放
  171. $platform = (new Admin())->where("id", 1)->find();
  172. if (!$platform)
  173. $this->error("平台账号异常!");
  174. $platform_profit_amount = $platform["profit_amount"];
  175. if ($platform_profit_amount < $details["diff_total_amount"])
  176. $this->error("平台钱包余额不足!");
  177. foreach ($details["month_orders"] as $order) {
  178. array_push($p_bills, [
  179. "identity_type" => \E_IDENTITY_TYPE::Platform,
  180. "target_id" => 1,
  181. "target_name" => "平台",
  182. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingExpend,
  183. "order_no" => $order["no"],
  184. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  185. "rate" => $order["bill"]["diff_rate"],
  186. "change" => -$order["bill"]["diff_amount"],
  187. "before" => $platform_profit_amount,
  188. "after" => fixed2Float($platform_profit_amount - $order["bill"]["diff_amount"]),
  189. "createtime" => time(),
  190. "city_code" => $order["city_code"]
  191. ], [
  192. "identity_type" => \E_IDENTITY_TYPE::Massager,
  193. "target_id" => $massager["id"],
  194. "target_name" => $massager["name"],
  195. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::ClosingIncome,
  196. "order_no" => $order["no"],
  197. "total_amount" => $order["total_real_amount"] - $order["trip_amount"],
  198. "rate" => $order["bill"]["diff_rate"],
  199. "change" => $order["bill"]["diff_amount"],
  200. "before" => $m_profit_amount,
  201. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  202. "createtime" => time(),
  203. "city_code" => $order["city_code"]
  204. ]);
  205. array_push($m_bills, [
  206. "massager_id" => $massager["id"],
  207. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  208. "change_type" => \E_M_BILL_CHANGE_TYPE::ClosingIncome,
  209. "change" => $order["bill"]["diff_amount"],
  210. "before" => $m_profit_amount,
  211. "after" => fixed2Float($m_profit_amount + $order["bill"]["diff_amount"]),
  212. "reason" => "业绩结算",
  213. "relation_no" => $order["no"],
  214. "createtime" => time()
  215. ]);
  216. $platform_profit_amount -= $order["bill"]["diff_amount"];
  217. $m_profit_amount += $order["bill"]["diff_amount"];
  218. }
  219. (new Admin())->where("id", 1)->setDec("profit_amount", $details["diff_total_amount"]);
  220. }
  221. (new \app\api\model\massager\Wallet())->where("id", $mWallet["id"])->setInc("profit_amount", $details["diff_total_amount"]);
  222. $this->model->update([
  223. "status" => "allow",
  224. "closing_rate" => $details["current_rate"],
  225. "closing_amount" => $details["diff_total_amount"],
  226. "updatetime" => time()
  227. ], ["id" => $record["id"]]);
  228. (new \app\api\model\profit\Bill())->saveAll($p_bills);
  229. (new \app\api\model\massager\Bill())->saveAll($m_bills);
  230. } else {
  231. // 拒绝
  232. $this->model->update([
  233. "status" => "reject"
  234. ], ["id" => $record["id"]]);
  235. }
  236. \app\api\model\system\Message::sendSystemMessage(
  237. \E_IDENTITY_TYPE::Massager,
  238. ["to_massager_id" => $record["massager_id"]],
  239. "业绩结算",
  240. "您的业绩结算申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  241. );
  242. Db::commit();
  243. } catch (Exception $e) {
  244. Db::rollback();
  245. $this->error($e->getMessage());
  246. } finally {
  247. foreach ($locks as $lock) {
  248. if (is_array($lock))
  249. $redLock->unlock($lock);
  250. }
  251. }
  252. $this->success("审核成功!");
  253. }
  254. public function details($ids = null)
  255. {
  256. $record = $this->model->where([
  257. "id" => $ids,
  258. ])->find();
  259. $s_result = (new MassagerActionService())->fetchDiffAmountDetailsByYm($record["massager_id"], $record["city_code"], $record["year"], $record["month"]);
  260. if (0 === $s_result->code())
  261. $this->error("获取数据异常");
  262. $details = $s_result->data();
  263. $this->view->assign([
  264. "current_rate" => $details["current_rate"],
  265. "diff_total_amount" => $details["diff_total_amount"],
  266. "month_orders" => $details["month_orders"],
  267. ]);
  268. return $this->view->fetch();
  269. }
  270. }