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