123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <?php
- /*
- * @Author: wang jun
- * @Date: 2021-10-29 16:16:29
- * @Last Modified by: wang jun
- * @Last Modified time: 2021-12-31 17:52:41
- */
- namespace app\index\server;
- use app\index\model\scorebusinessmodel;
- use app\index\model\scorebusinessrecordmodel;
- use app\index\model\useraccountaddrecordmodel;
- use app\index\model\useraccountmodel;
- use app\index\model\useraccountuserrecordmodel;
- use app\index\model\userinfomodel;
- use app\index\server\businessserver as BusinessBase;
- use think\Db;
- use think\Log;
- class scorebusinessserver extends BusinessBase
- {
- protected $m_sb;
- public function __construct()
- {
- $this->m_sb = new scorebusinessmodel;
- }
- public function getinfo($arr)
- {
- $where = ['isactive' => 1, 'typeid' => $arr['typeid'], 'type' => $arr['type']];
- if (isset($arr['code']) && !empty($arr['code']) && is_string($arr['code'])) {
- $where['code'] = $arr['code'];
- $info = $this->m_sb->getInfo($where);
- $result = $this->checkactive($info);
- if (!$result) {
- if ($info['isautooff']) {
- $this->closebusiness($info['id']);
- }
- throw new \Exception("业务不可用");
- }
- $result = $this->checkstart($info);
- if (!$result) {
- throw new \Exception("业务未开始");
- }
- } else {
- $where['time'] = date('Y-m-d H:i:s');
- $info = $this->m_sb->getinfobyweight($where);
- }
- if (empty($info)) {
- throw new \Exception("无可用业务");
- }
- $time = date('Y-m-d H:i:s');
- $oldwhere = [
- 'typeid' => $info['typeid'],
- 'weight' => ['<', $info['weight']],
- 'starttime' => ['<=', $time],
- 'endtime' => ['>=', $time],
- 'isactive' => 1,
- 'isautooff' => 1,
- ];
- $list = $this->m_sb->getList($oldwhere, ['id'], 1, 0);
- if (count($list) > 0) {
- foreach ($list as $key => $value) {
- $this->closebusiness($value['id']);
- }
- }
- return $info;
- }
- /**
- * 关闭业务
- * 20220104
- * wj
- */
- protected function closebusiness($id)
- {
- $where = ['id' => $id];
- $update = ['isactive' => 0];
- $row = $this->m_sb->updateinfo($where, $update);
- if (empty($row)) {
- log::info($where);
- throw new \Exception("业务关闭失败");
- }
- }
- /**
- * 创建业务
- * 20211231
- * wj
- */
- private function newifno()
- {
- }
- /**
- * 创建业务码
- * 20211231
- * wj
- */
- private function createCode()
- {
- $code = 'bs' . date('YmdHis');
- return $code;
- }
- /**
- * 加积分
- * 20211231
- * wj
- */
- protected function adduserscore($arr)
- {
- $fillFields = ['userid', 'scoretype', 'addscore', 'sourcetype'];
- foreach ($fillFields as $key => $value) {
- if (!isset($arr[$value]) || empty($arr[$value])) {
- return backarr(0, "积分增加请求错误");
- }
- }
- $m_u = new userinfomodel();
- $userid = $arr['userid'];
- $scoretype = $arr['scoretype'];
- $addscore = $arr['addscore'];
- $sourcetype = $arr['sourcetype'];
- $userwhere = ['id' => $userid];
- $userinfo = $m_u->getInfo($userwhere);
- if (empty($userinfo)) {
- return backarr(0, "无用户信息");
- }
- Db::startTrans();
- try {
- $result = $this->adduseraccount($userid, $addscore, $scoretype);
- if (1 != $result['status']) {
- throw new \Exception($result['msg']);
- }
- $insertData = [
- 'activescore' => $addscore,
- 'createdate' => date('Y-m-d H:i:s'),
- ];
- $insertData = array_merge($insertData, $arr);
- $m_uaar = new useraccountaddrecordmodel();
- $uaarid = $m_uaar->insertData($insertData);
- if (empty($uaarid)) {
- throw new \Exception("积分记录添加失败");
- }
- Db::commit();
- return backarr(1, "积分增加成功", ['uaarid' => $uaarid, 'uid' => $userid, 'addscore' => $addscore]);
- } catch (\Exception $e) {
- Db::rollback();
- return backarr(0, $e->getMessage());
- }
- }
- /**
- * 减积分
- * 20211231
- * wj
- */
- protected function reduceuserscore($arr, $type = 0)
- {
- $fillFields = ['userid', 'usescore', 'usetype'];
- foreach ($fillFields as $key => $value) {
- if (!isset($arr[$value]) || empty($arr[$value])) {
- return backarr(0, "积分使用请求错误");
- }
- }
- $m_u = new userinfomodel();
- $userid = $arr['userid'];
- $usescore = $arr['usescore'];
- $usetype = $arr['usetype'];
- $userwhere = ['id' => $userid];
- $userinfo = $m_u->getInfo($userwhere);
- if (empty($userinfo)) {
- return backarr(0, "无用户信息");
- }
- Db::startTrans();
- try {
- //操作总表
- $result = $this->reduceuseraccount($userid, $usescore, $type);
- if (1 != $result['status']) {
- throw new \Exception($result['msg']);
- }
- $m_uaur = new useraccountuserrecordmodel();
- $m_uaar = new useraccountaddrecordmodel();
- $list = $this->getaddrecordlistforreduce($userid);
- if (!$list) {
- if (empty($userinfo)) {
- return backarr(0, "无可扣除积分");
- }
- }
- $residueScore = $usescore; //剩余抵扣积分
- foreach ($list as $key => $value) {
- if ($residueScore <= 0) {
- break;
- }
- $diffScore = $value['activescore'] - $residueScore;
- if ($diffScore > 0) {
- $recordScore = $residueScore;
- $activescore = $diffScore;
- $residueScore = 0;
- } else {
- $recordScore = $value['activescore'];
- $activescore = 0;
- $residueScore = abs($diffScore);
- }
- $uaurinsertdata = [
- 'sourcerecordid' => $value['id'],
- 'userid' => $userid,
- 'usescore' => $recordScore,
- 'usetype' => $usetype,
- 'usetime' => date("Y-m-d H:i:s"),
- ];
- if (isset($arr['usedemo']) && !empty($arr['usedemo'])) {
- $uaurinsertdata['usedemo'] = $arr['usedemo'];
- }
- $uaurid = $m_uaur->insertData($uaurinsertdata);
- if (empty($uaurid)) {
- throw new \Exception("用户积分记录添加失败");
- }
- $uaarwhere = ['id' => $value['id']];
- $uaarupdatedata = [
- 'activescore' => $activescore,
- ];
- $row = $m_uaar->updateinfo($uaarwhere, $uaarupdatedata);
- if (empty($row)) {
- throw new \Exception("用户积分增加记录修改失败");
- }
- }
- Db::commit();
- return backarr(1, '操作成功');
- } catch (\Exception $e) {
- Db::rollback();
- return backarr(0, $e->getMessage());
- }
- }
- /***
- * 加用户积分 总表修改
- * $scoretype 1金,2银
- * 20211105
- * wj
- */
- private function adduseraccount($uid, $addscore, $scoretype)
- {
- $result = $this->getinfobyuid(['userid' => $uid]);
- if (1 != $result['status']) {
- return $result;
- }
- $info = $result['data'];
- $updateData = [];
- switch ($scoretype) {
- case 1:
- $updateData['goldscore'] = bcadd($info['goldscore'], $addscore);
- break;
- case 2:
- $updateData['siliverscore'] = bcadd($info['siliverscore'], $addscore);
- break;
- }
- $m_ua = new useraccountmodel();
- $where = ['id' => $info['id']];
- $row = $m_ua->updateinfo($where, $updateData);
- if (empty($row)) {
- return backarr(0, "用户积分修改失败");
- }
- return backarr(1, "用户积分修改成功", ['id' => $info['id']]);
- }
- /***
- * 查询用户积分
- * 20211105
- * wj
- */
- private function getinfobyuid($arr)
- {
- if (!isset($arr['userid']) || empty($arr['userid'])) {
- return backarr(0, "请求错误");
- }
- $uid = $arr['userid'];
- $m_u = new userinfomodel();
- $m_ua = new useraccountmodel();
- $userwhere = ['id' => $uid];
- $userinfo = $m_u->getInfo($userwhere);
- if (empty($userinfo)) {
- return backarr(0, "无用户信息");
- }
- $where = ['userid' => $uid];
- $info = $m_ua->getInfo($where);
- if (empty($info)) {
- $defaultData = [
- 'userid' => $uid,
- 'goldscore' => 0,
- 'siliverscore' => 0,
- ];
- $id = $m_ua->insertData($defaultData);
- $info = $defaultData;
- $info['id'] = $id;
- }
- return backarr(1, "查询成功", $info);
- }
- /**
- * 扣除用户积分 总表修改
- * type 0 先扣银币再扣 1金分 2银分
- * 20211105
- * wj
- */
- private function reduceuseraccount($uid, $userscore, $type = 0)
- {
- $result = $this->checkreducedata($uid, $userscore, $type);
- if (1 != $result['status']) {
- return $result;
- }
- $info = $result['data'];
- $updateData = [];
- switch ($type) {
- case 0:
- $subsiliverscore = $info['siliverscore'] - $userscore;
- if ($subsiliverscore >= 0) {
- $updateData['siliverscore'] = $subsiliverscore;
- } else {
- $updateData['siliverscore'] = 0;
- $updateData['goldscore'] = bcsub($info['goldscore'], abs($subsiliverscore));
- }
- break;
- case 2:
- $updateData['siliverscore'] = $info['siliverscore'] - $userscore;
- break;
- case 1:
- $updateData['goldscore'] = $info['goldscore'] - $userscore;
- break;
- }
- $m_ua = new useraccountmodel();
- $where = ['id' => $info['id']];
- $row = $m_ua->updateinfo($where, $updateData);
- if (empty($row)) {
- return backarr(0, "用户积分修改失败");
- }
- return backarr(1, "用户积分修改成功", ['id' => $info['id']]);
- }
- /***
- * 验证用户积分
- * 积分不够返回错误
- * type 0 总积分 1金分 2银分
- * 20211105
- * wj
- */
- private function checkreducedata($uid, $userscore, $type = 0)
- {
- $result = $this->getinfobyuid(['userid' => $uid]);
- if (1 != $result['status']) {
- return $result;
- }
- $info = $result['data'];
- switch ($type) {
- case 0:
- $count = bcadd($info['goldscore'], $info['siliverscore']);
- break;
- case 2:
- $count = $info['siliverscore'];
- break;
- case 1:
- $count = $info['goldscore'];
- break;
- }
- if ($count < $userscore) {
- return backarr(0, "用户积分不足");
- }
- return backarr(1, '', $info);
- }
- /***
- * 加业务记录表
- * 20220104
- * wj
- */
- protected function newrecord($arr)
- {
- if (!isset($arr['createtime'])) {
- $arr['createtime'] = date('Y-m-d H:i:s');
- }
- $m_sbr = new scorebusinessrecordmodel();
- $id = $m_sbr->insertData($arr);
- return empty($id) ? false : $id;
- }
- /**
- * 获取用户可扣除积分
- * type 0 先银再扣金 1金分 2银分
- * 20211105
- * wj
- */
- protected function getaddrecordlistforreduce($userid, $scoretype = 0)
- {
- $m_u = new userinfomodel();
- $m_uaar = new useraccountaddrecordmodel();
- $userwhere = ['id' => $userid];
- $userinfo = $m_u->getInfo($userwhere);
- //$startDate = empty($userinfo['createtime']) ? '2021-10-01 00:00:00' : $userinfo['createtime'];
- $endData = date('Y-m-d H:i:s');
- $list = $m_uaar->getaddrecordlistforreduce($userid, $endData, $scoretype);
- if (empty($list)) {
- return false;
- }
- return $list;
- }
- }
|