123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- /**
- * 代理商数据模型
- * Created by PhpStorm.
- * User: user
- * Date: 2021/6/24
- * Time: 12:00
- */
- namespace JinDouYun\Model\Agent;
- use JinDouYun\Dao\Agent\DOemDetails;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- use JinDouYun\Dao\Oem\DOem;
- use JinDouYun\Dao\Agent\DMeal;
- use JinDouYun\Dao\Agent\DAgents;
- class MAgents
- {
- private $objDAgents;
- private $objDOem;
- private $objDOemDetails;
- public function __construct()
- {
- $this->objDAgents = new DAgents('default');
- $this->objDOem = new DOem();
- $this->objDOem->setTable('qianniao_oem_1');
- $this->objDOemDetails = new DOemDetails();
- $this->objDOemDetails->setTable('qianniao_oem_details');
- }
- /**
- * 增加代理商
- * @param $params
- * @return ResultWrapper
- */
- public function addAgent($params)
- {
- $addAgent = $this->objDAgents->insert($params);
- if ($addAgent === false) {
- return ResultWrapper::fail($this->objAgents->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($addAgent);
- }
- /**
- * 编辑代理商信息
- *
- * @param int|array $params 修改代理的信息
- *
- * @return ResultWrapper
- */
- public function editAgent($params)
- {
- if (empty($params['id'])) {
- return ResultWrapper::fail('id为空', ErrorCode::$paramError);
- }
- $id = $params['id'];
- unset($params['id']);
- $dbResult = $this->objDAgents->update($params, ['id' => $id]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 删除代理信息
- * @param $id
- * @return ResultWrapper
- */
- public function deleteAgent($id)
- {
- $params = [
- 'deleteStatus' => StatusCode::$delete,
- 'updateTime' => time(),
- ];
- //本条id的数据
- $agentData = $this->objDAgents->get(['id'=>$id]);
- if ($agentData === false){
- return ResultWrapper::fail($this->objDAgents->error(),ErrorCode::$dberror);
- }
- $dbResult = $this->objDAgents->update($params, $id);//软删除
- if($dbResult === false){
- return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取代理信息
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllAgent($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $dbResult = $this->objDAgents->select($selectParams, '*', 'createTime DESC', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
- }
- $total = $this->objDAgents->count($selectParams);
- $return = [
- 'data' => self::format($dbResult),//格式化json数据
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * oem信息设置
- * @param int|array $params 需设置的key
- * @param int|number $enterpriseId 企业id
- * @return ResultWrapper
- */
- public function oemSystemSet($params,$enterpriseId)
- {
- $objDOem = new DOem();
- $is_null = $objDOem->get(['enterpriseId' => $enterpriseId]);
- //企业是否存在
- if (empty($is_null)) {
- return ResultWrapper::fail("暂无该企业信息", ErrorCode::$dberror);
- }
- //需要修改的字段 信息
- switch ($params['type']) {
- case 1://微信开放平台配置
- $updateDate = ['weixinOpen'=>json_encode($params['setData'],JSON_UNESCAPED_UNICODE)];
- break;
- case 2://短息配置
- $updateDate = ['sms'=>json_encode($params['setData'],JSON_UNESCAPED_UNICODE)];
- break;
- case 3://高德地图地址
- $updateDate = ['amapKey'=>isset($params['setData']['amapKey'])?$params['setData']['amapKey']:''];
- break;
- default:
- return ResultWrapper::fail("参数值错误", ErrorCode::$dberror);
- }
- $dbResult = $objDOem->update($updateDate, ['enterpriseId' => $enterpriseId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->$objDOem->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 根据企业id获取oem企业
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getOemInfo($selectParams)
- {
- $objDOem = new DOem();
- $dbResult = $objDOem->select($selectParams);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
- }
- $dataInfo = self::formatOem($dbResult);
- return ResultWrapper::success($dataInfo);
- }
- /**
- * 格式化联系人json数据
- */
- private function format($data)
- {
- foreach ($data as &$v) {
- if (isset($v['contact'])) {
- $v['contact'] = json_decode($v['contact'], true);
- }
- }
- return $data;
- }
- /**
- * 格式化oem json数据
- */
- private function formatOem($data)
- {
- $data[0]['tencentCloud'] = isset($data[0]['tencentCloud']) ? json_decode($data[0]['tencentCloud']):"";
- $data[0]['weixinOpen'] = isset($data[0]['weixinOpen']) ? json_decode($data[0]['weixinOpen']):"";
- $data[0]['sms'] = isset($data[0]['sms']) ? json_decode($data[0]['sms']):"";
- return $data;
- }
- public function getAllMeal()
- {
- $objDMeal = new DMeal();
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $objDMeal->select($selectParams);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDAgents->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * oem续费
- */
- public function renew($params)
- {
- // 查询当前企业的数据
- $dbResult = $this->objDOem->get(['enterpriseId'=>$params['enterpriseId']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDOem->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::fail('当前企业不存在',ErrorCode::$notAllowAccess);
- }
- $afterMoney = bcsub($dbResult['balance'],$params['money'],2);
- //组装数据
- $detailsDate = [
- 'enterpriseId' => getArrayItem($params,'enterpriseId',0),
- 'enterpriseName' => getArrayItem($params,'enterpriseName',''),
- 'mealId' => getArrayItem($params,'mealId',0),
- 'meal' => getArrayItem($params,'meal',''),
- 'mobile' => getArrayItem($params,'mobile',0),
- 'type' => '续费',
- 'beforeMoney' => $dbResult['balance'],
- 'money' => getArrayItem($params,'money',0),
- 'afterMoney' => $afterMoney,
- 'explain' => '客户续费'.$params['years'].'年的'.$params['meal'].'共消费'.$params['money'].'元',
- 'createTime' => time(),
- ];
- //减完的钱不能小于零
- if($detailsDate['afterMoney']<0){
- return ResultWrapper::fail('余额不足',ErrorCode::$notAllowAccess);
- }
- unset($dbResult);
- $beginTransactionStatus = $this->objDOemDetails->beginTransaction();
- $dbResult = $this->objDOemDetails->insert($detailsDate);
- if ($dbResult === false) {
- $this->objDOemDetails->rollBack();
- return ResultWrapper::fail($this->objDOemDetails->error(), ErrorCode::$dberror);
- }
- // 扣代理商余额
- $update = $this->objDOem->update(['balance'=>$afterMoney],['enterpriseId'=>$params['enterpriseId']]);
- if ($update === false) {
- $this->objDOemDetails->rollBack();
- return ResultWrapper::fail($this->objDOem->error(), ErrorCode::$dberror);
- }
- if($beginTransactionStatus){
- $this->objDOemDetails->commit();
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 消费记录列表
- */
- public function getAllRenew($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $returnData = [
- 'data' => [],
- 'total' => 0,
- ];
- $whereSql = '';
- if (isset($selectParams['enterpriseId']) && !empty($selectParams['enterpriseId'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' enterpriseId = ' . $selectParams['enterpriseId'];
- }
- if (isset($selectParams['mobile']) && !empty($selectParams['mobile'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' mobile like "%' . $selectParams['mobile'].'%"';
- }
- if (isset($selectParams['mealId']) && !empty($selectParams['mealId'])) {
- $where = empty($whereSql) ? ' WHERE ' : ' AND ';
- $whereSql .= $where . ' mealId = ' . $selectParams['mealId'];
- }
- $sql = 'SELECT * FROM ' .$this->objDOemDetails->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
- $dbResult = $this->objDOemDetails->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDOemDetails->error(), ErrorCode::$dberror);
- }
- if(empty($dbResult)){
- return ResultWrapper::success($returnData);
- }
- $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDOemDetails->get_Table() . $whereSql;
- $dbTotalResult = $this->objDOemDetails->query($totalSql);
- if ($dbTotalResult === false) {
- return ResultWrapper::fail($this->objDOemDetails->error(), ErrorCode::$dberror);
- }
- if(empty($dbTotalResult)){
- return ResultWrapper::success([]);
- }
- $return = [
- 'data' => $dbResult,
- 'total' => $dbTotalResult[0]['count']
- ];
- return ResultWrapper::success($return);
- }
- }
|