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; } }