scorebusinessserver.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2021-10-29 16:16:29
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2021-12-31 17:52:41
  7. */
  8. namespace app\index\server;
  9. use app\index\model\scorebusinessmodel;
  10. use app\index\model\scorebusinessrecordmodel;
  11. use app\index\model\useraccountaddrecordmodel;
  12. use app\index\model\useraccountmodel;
  13. use app\index\model\useraccountuserrecordmodel;
  14. use app\index\model\userinfomodel;
  15. use app\index\server\businessserver as BusinessBase;
  16. use think\Db;
  17. use think\Log;
  18. class scorebusinessserver extends BusinessBase
  19. {
  20. protected $m_sb;
  21. public function __construct()
  22. {
  23. $this->m_sb = new scorebusinessmodel;
  24. }
  25. public function getinfo($arr)
  26. {
  27. $where = ['isactive' => 1, 'typeid' => $arr['typeid'], 'type' => $arr['type']];
  28. if (isset($arr['code']) && !empty($arr['code']) && is_string($arr['code'])) {
  29. $where['code'] = $arr['code'];
  30. $info = $this->m_sb->getInfo($where);
  31. $result = $this->checkactive($info);
  32. if (!$result) {
  33. if ($info['isautooff']) {
  34. $this->closebusiness($info['id']);
  35. }
  36. throw new \Exception("业务不可用");
  37. }
  38. $result = $this->checkstart($info);
  39. if (!$result) {
  40. throw new \Exception("业务未开始");
  41. }
  42. } else {
  43. $where['time'] = date('Y-m-d H:i:s');
  44. $info = $this->m_sb->getinfobyweight($where);
  45. }
  46. if (empty($info)) {
  47. throw new \Exception("无可用业务");
  48. }
  49. $time = date('Y-m-d H:i:s');
  50. $oldwhere = [
  51. 'typeid' => $info['typeid'],
  52. 'weight' => ['<', $info['weight']],
  53. 'starttime' => ['<=', $time],
  54. 'endtime' => ['>=', $time],
  55. 'isactive' => 1,
  56. 'isautooff' => 1,
  57. ];
  58. $list = $this->m_sb->getList($oldwhere, ['id'], 1, 0);
  59. if (count($list) > 0) {
  60. foreach ($list as $key => $value) {
  61. $this->closebusiness($value['id']);
  62. }
  63. }
  64. return $info;
  65. }
  66. /**
  67. * 关闭业务
  68. * 20220104
  69. * wj
  70. */
  71. protected function closebusiness($id)
  72. {
  73. $where = ['id' => $id];
  74. $update = ['isactive' => 0];
  75. $row = $this->m_sb->updateinfo($where, $update);
  76. if (empty($row)) {
  77. log::info($where);
  78. throw new \Exception("业务关闭失败");
  79. }
  80. }
  81. /**
  82. * 创建业务
  83. * 20211231
  84. * wj
  85. */
  86. private function newifno()
  87. {
  88. }
  89. /**
  90. * 创建业务码
  91. * 20211231
  92. * wj
  93. */
  94. private function createCode()
  95. {
  96. $code = 'bs' . date('YmdHis');
  97. return $code;
  98. }
  99. /**
  100. * 加积分
  101. * 20211231
  102. * wj
  103. */
  104. protected function adduserscore($arr)
  105. {
  106. $fillFields = ['userid', 'scoretype', 'addscore', 'sourcetype'];
  107. foreach ($fillFields as $key => $value) {
  108. if (!isset($arr[$value]) || empty($arr[$value])) {
  109. return backarr(0, "积分增加请求错误");
  110. }
  111. }
  112. $m_u = new userinfomodel();
  113. $userid = $arr['userid'];
  114. $scoretype = $arr['scoretype'];
  115. $addscore = $arr['addscore'];
  116. $sourcetype = $arr['sourcetype'];
  117. $userwhere = ['id' => $userid];
  118. $userinfo = $m_u->getInfo($userwhere);
  119. if (empty($userinfo)) {
  120. return backarr(0, "无用户信息");
  121. }
  122. Db::startTrans();
  123. try {
  124. $result = $this->adduseraccount($userid, $addscore, $scoretype);
  125. if (1 != $result['status']) {
  126. throw new \Exception($result['msg']);
  127. }
  128. $insertData = [
  129. 'activescore' => $addscore,
  130. 'createdate' => date('Y-m-d H:i:s'),
  131. ];
  132. $insertData = array_merge($insertData, $arr);
  133. $m_uaar = new useraccountaddrecordmodel();
  134. $uaarid = $m_uaar->insertData($insertData);
  135. if (empty($uaarid)) {
  136. throw new \Exception("积分记录添加失败");
  137. }
  138. Db::commit();
  139. return backarr(1, "积分增加成功", ['uaarid' => $uaarid, 'uid' => $userid, 'addscore' => $addscore]);
  140. } catch (\Exception $e) {
  141. Db::rollback();
  142. return backarr(0, $e->getMessage());
  143. }
  144. }
  145. /**
  146. * 减积分
  147. * 20211231
  148. * wj
  149. */
  150. protected function reduceuserscore($arr, $type = 0)
  151. {
  152. $fillFields = ['userid', 'usescore', 'usetype'];
  153. foreach ($fillFields as $key => $value) {
  154. if (!isset($arr[$value]) || empty($arr[$value])) {
  155. return backarr(0, "积分使用请求错误");
  156. }
  157. }
  158. $m_u = new userinfomodel();
  159. $userid = $arr['userid'];
  160. $usescore = $arr['usescore'];
  161. $usetype = $arr['usetype'];
  162. $userwhere = ['id' => $userid];
  163. $userinfo = $m_u->getInfo($userwhere);
  164. if (empty($userinfo)) {
  165. return backarr(0, "无用户信息");
  166. }
  167. Db::startTrans();
  168. try {
  169. //操作总表
  170. $result = $this->reduceuseraccount($userid, $usescore, $type);
  171. if (1 != $result['status']) {
  172. throw new \Exception($result['msg']);
  173. }
  174. $m_uaur = new useraccountuserrecordmodel();
  175. $m_uaar = new useraccountaddrecordmodel();
  176. $list = $this->getaddrecordlistforreduce($userid);
  177. if (!$list) {
  178. if (empty($userinfo)) {
  179. return backarr(0, "无可扣除积分");
  180. }
  181. }
  182. $residueScore = $usescore; //剩余抵扣积分
  183. foreach ($list as $key => $value) {
  184. if ($residueScore <= 0) {
  185. break;
  186. }
  187. $diffScore = $value['activescore'] - $residueScore;
  188. if ($diffScore > 0) {
  189. $recordScore = $residueScore;
  190. $activescore = $diffScore;
  191. $residueScore = 0;
  192. } else {
  193. $recordScore = $value['activescore'];
  194. $activescore = 0;
  195. $residueScore = abs($diffScore);
  196. }
  197. $uaurinsertdata = [
  198. 'sourcerecordid' => $value['id'],
  199. 'userid' => $userid,
  200. 'usescore' => $recordScore,
  201. 'usetype' => $usetype,
  202. 'usetime' => date("Y-m-d H:i:s"),
  203. ];
  204. if (isset($arr['usedemo']) && !empty($arr['usedemo'])) {
  205. $uaurinsertdata['usedemo'] = $arr['usedemo'];
  206. }
  207. $uaurid = $m_uaur->insertData($uaurinsertdata);
  208. if (empty($uaurid)) {
  209. throw new \Exception("用户积分记录添加失败");
  210. }
  211. $uaarwhere = ['id' => $value['id']];
  212. $uaarupdatedata = [
  213. 'activescore' => $activescore,
  214. ];
  215. $row = $m_uaar->updateinfo($uaarwhere, $uaarupdatedata);
  216. if (empty($row)) {
  217. throw new \Exception("用户积分增加记录修改失败");
  218. }
  219. }
  220. Db::commit();
  221. return backarr(1, '操作成功');
  222. } catch (\Exception $e) {
  223. Db::rollback();
  224. return backarr(0, $e->getMessage());
  225. }
  226. }
  227. /***
  228. * 加用户积分 总表修改
  229. * $scoretype 1金,2银
  230. * 20211105
  231. * wj
  232. */
  233. private function adduseraccount($uid, $addscore, $scoretype)
  234. {
  235. $result = $this->getinfobyuid(['userid' => $uid]);
  236. if (1 != $result['status']) {
  237. return $result;
  238. }
  239. $info = $result['data'];
  240. $updateData = [];
  241. switch ($scoretype) {
  242. case 1:
  243. $updateData['goldscore'] = bcadd($info['goldscore'], $addscore);
  244. break;
  245. case 2:
  246. $updateData['siliverscore'] = bcadd($info['siliverscore'], $addscore);
  247. break;
  248. }
  249. $m_ua = new useraccountmodel();
  250. $where = ['id' => $info['id']];
  251. $row = $m_ua->updateinfo($where, $updateData);
  252. if (empty($row)) {
  253. return backarr(0, "用户积分修改失败");
  254. }
  255. return backarr(1, "用户积分修改成功", ['id' => $info['id']]);
  256. }
  257. /***
  258. * 查询用户积分
  259. * 20211105
  260. * wj
  261. */
  262. private function getinfobyuid($arr)
  263. {
  264. if (!isset($arr['userid']) || empty($arr['userid'])) {
  265. return backarr(0, "请求错误");
  266. }
  267. $uid = $arr['userid'];
  268. $m_u = new userinfomodel();
  269. $m_ua = new useraccountmodel();
  270. $userwhere = ['id' => $uid];
  271. $userinfo = $m_u->getInfo($userwhere);
  272. if (empty($userinfo)) {
  273. return backarr(0, "无用户信息");
  274. }
  275. $where = ['userid' => $uid];
  276. $info = $m_ua->getInfo($where);
  277. if (empty($info)) {
  278. $defaultData = [
  279. 'userid' => $uid,
  280. 'goldscore' => 0,
  281. 'siliverscore' => 0,
  282. ];
  283. $id = $m_ua->insertData($defaultData);
  284. $info = $defaultData;
  285. $info['id'] = $id;
  286. }
  287. return backarr(1, "查询成功", $info);
  288. }
  289. /**
  290. * 扣除用户积分 总表修改
  291. * type 0 先扣银币再扣 1金分 2银分
  292. * 20211105
  293. * wj
  294. */
  295. private function reduceuseraccount($uid, $userscore, $type = 0)
  296. {
  297. $result = $this->checkreducedata($uid, $userscore, $type);
  298. if (1 != $result['status']) {
  299. return $result;
  300. }
  301. $info = $result['data'];
  302. $updateData = [];
  303. switch ($type) {
  304. case 0:
  305. $subsiliverscore = $info['siliverscore'] - $userscore;
  306. if ($subsiliverscore >= 0) {
  307. $updateData['siliverscore'] = $subsiliverscore;
  308. } else {
  309. $updateData['siliverscore'] = 0;
  310. $updateData['goldscore'] = bcsub($info['goldscore'], abs($subsiliverscore));
  311. }
  312. break;
  313. case 2:
  314. $updateData['siliverscore'] = $info['siliverscore'] - $userscore;
  315. break;
  316. case 1:
  317. $updateData['goldscore'] = $info['goldscore'] - $userscore;
  318. break;
  319. }
  320. $m_ua = new useraccountmodel();
  321. $where = ['id' => $info['id']];
  322. $row = $m_ua->updateinfo($where, $updateData);
  323. if (empty($row)) {
  324. return backarr(0, "用户积分修改失败");
  325. }
  326. return backarr(1, "用户积分修改成功", ['id' => $info['id']]);
  327. }
  328. /***
  329. * 验证用户积分
  330. * 积分不够返回错误
  331. * type 0 总积分 1金分 2银分
  332. * 20211105
  333. * wj
  334. */
  335. private function checkreducedata($uid, $userscore, $type = 0)
  336. {
  337. $result = $this->getinfobyuid(['userid' => $uid]);
  338. if (1 != $result['status']) {
  339. return $result;
  340. }
  341. $info = $result['data'];
  342. switch ($type) {
  343. case 0:
  344. $count = bcadd($info['goldscore'], $info['siliverscore']);
  345. break;
  346. case 2:
  347. $count = $info['siliverscore'];
  348. break;
  349. case 1:
  350. $count = $info['goldscore'];
  351. break;
  352. }
  353. if ($count < $userscore) {
  354. return backarr(0, "用户积分不足");
  355. }
  356. return backarr(1, '', $info);
  357. }
  358. /***
  359. * 加业务记录表
  360. * 20220104
  361. * wj
  362. */
  363. protected function newrecord($arr)
  364. {
  365. if (!isset($arr['createtime'])) {
  366. $arr['createtime'] = date('Y-m-d H:i:s');
  367. }
  368. $m_sbr = new scorebusinessrecordmodel();
  369. $id = $m_sbr->insertData($arr);
  370. return empty($id) ? false : $id;
  371. }
  372. /**
  373. * 获取用户可扣除积分
  374. * type 0 先银再扣金 1金分 2银分
  375. * 20211105
  376. * wj
  377. */
  378. protected function getaddrecordlistforreduce($userid, $scoretype = 0)
  379. {
  380. $m_u = new userinfomodel();
  381. $m_uaar = new useraccountaddrecordmodel();
  382. $userwhere = ['id' => $userid];
  383. $userinfo = $m_u->getInfo($userwhere);
  384. //$startDate = empty($userinfo['createtime']) ? '2021-10-01 00:00:00' : $userinfo['createtime'];
  385. $endData = date('Y-m-d H:i:s');
  386. $list = $m_uaar->getaddrecordlistforreduce($userid, $endData, $scoretype);
  387. if (empty($list)) {
  388. return false;
  389. }
  390. return $list;
  391. }
  392. }