enterpriseId = $enterpriseId; $this->userCenterId = $userCenterId; parent::__construct($this->enterpriseId, $this->userCenterId); $this->obj = new DShopSubscribe('default'); $this->obj->setTable('qianniao_shop_subscribe_'.$enterpriseId); $this->Dproject = new DShopProject('default'); $this->objStaff = new DStaff('default'); $this->objCustomer = new DCustomer('default'); $this->objShop = new DShop('default'); $this->objStaff->setTable('qianniao_staff_'.$enterpriseId); $this->objShop->setTable('qianniao_shop_1'); $this->objCustomer->setTable('qianniao_customer_'.$enterpriseId); } /** * 列表 * @param $where * @return mixed */ public function list($where) { $limit = $where['limit']; unset($where['limit']); $offset = $where['offset']; unset($where['offset']); $wherein = []; if (isset($where['uid'])){ $wherein[] = ['uid', '=', $where['uid']]; } if (isset($where['shop_id'])){ $wherein[] = ['shop_id', '=', $where['shop_id']]; } if (isset($where['status'])){ $wherein[] = ['status', '>=', 0]; } if (isset($where['customer_id'])){ $wherein[] = ['customer_id', '=', $where['customer_id']]; } if (isset($where['time'])){ $wherein[] = ['time', '>=', strtotime($where['time'][0])]; $wherein[] = ['time', '<=', strtotime($where['time'][1])]; unset($where['time']); } if (isset($where['type'])){ if ($where['type'] == 1){ //待支付 $wherein[] = ['status', '=', 0]; $wherein[] = ['paid', '=', 0]; } if ($where['type'] == 2){ //已支付 $wherein[] = ['status', '=', 0]; $wherein[] = ['paid', '=', 1]; } if ($where['type'] == 3){ //已完成 $wherein[] = ['status', '=', 1]; $wherein[] = ['paid', '=', 1]; $wherein[] = ['refund', '=', 0]; } if ($where['type'] == 4){ //退款订单 $wherein[] = ['status', '=', -1]; } unset($where['type']); } $dbResult = $this->obj->select($wherein, '*', 'id DESC', $limit, $offset); $count = $this->obj->count($wherein); if ($dbResult === false) { return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); } foreach ($dbResult as &$item) { $customer = $this->objCustomer->get(['id' => $item['customer_id']]); $staff = $this->objStaff->get(['id' => $item['uid']]); $item['staff'] = $staff['staffName']; $item['avatar'] = $staff['avatar']; $item['customer'] = $customer['name']; $item['customer_avatar'] = $customer['avatar']; $item['project'] = $this->Dproject->select([['id', 'in', $item['project']]], 'id,name,price,image'); $shop = $this->objShop->get(['id' => $item['shop_id']]); $item['shop_name'] = $shop['name']; $Duser = new DUserCenter('default'); $user = $Duser->get(['id' => $customer['userCenterId']]); $item['customer_mobile'] = $user['mobile']; } $return = [ 'data' => $dbResult, 'total' => $count, ]; if($return === false){ return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($return); } } /** * 详情 * @param $where * @return mixed */ public function details($where) { if(empty($where)){ return ResultWrapper::success($where); } $dbResult = $this->obj->get($where); if(empty($dbResult)){ return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); } $dbResult['staff'] = $this->objStaff->get(['id' => $dbResult['uid'], 'shopId' => $dbResult['shop_id']], 'staffName'); $dbResult['customer'] = $this->objCustomer->get(['id' => $dbResult['customer_id']], 'name,avatar,userCenterId'); $Duser = new DUserCenter('default'); $user = $Duser->get(['id' => $dbResult['customer']['userCenterId']]); $dbResult['customer']['mobile'] = $user['mobile']; return ResultWrapper::success(self::formatInfo($dbResult)); } /** * Doc: (des="添加") * User: XMing * Date: 2020/7/15 * Time: 10:42 上午 * @throws Exception * @param array $params * @return ResultWrapper */ public function insert(array $params) { $params['time'] = strtotime($params['time']); if (isset($params['card_id'])){ $card_id = $params['card_id']; unset($params['card_id']); } $res = $this->obj->insert($params); if ($res){ if (isset($params['paid'])){ if (isset($card_id)){ $this->reduceCard($card_id); } } return ResultWrapper::success('添加成功'); }else{ return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); } } public function reduceCard($id) { $cardNum = new DCustomerCardNum('default'); $card = $cardNum->get(['id' => $id]); if ($card['number'] <= 0) return false; $cardNum->update(['number' => $card['number'] -1], $card['id']); return true; } public function update($params, $id) { $data = $this->obj->get($id); if (!$data){ return ResultWrapper::fail('数据不存在', ErrorCode::$dberror); } $dbResult = $this->obj->update($params,$id); if($dbResult === false){ return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); } return ResultWrapper::success('修改成功'); } /** * 格式数据详情 * @param $data * @return mixed */ public function formatInfo($data) { return $data; } public function delete($id) { $res = $this->obj->delete(['id' => $id]); if ($res){ return ResultWrapper::success('删除成功'); } return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); } public function select($where) { $dbResult = $this->obj->select($where, '*', 'id DESC'); if ($dbResult === false) { return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); } $return = [ 'data' => $dbResult, ]; if($return === false){ return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($return); } } /** * 微信支付 * @param $params * @return mixed * @throws Exception */ public function wxPay($params, $ip, $source) { $objSystemCache = new SystemCache(); $objMPaymentSetting = new MPaymentSetting($this->userCenterId, $this->enterpriseId); $result = $objMPaymentSetting->getPayData(1); if (!$result->isSuccess() || empty($result->getData())) { return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor); } $payment = $result->getData(); //获取客户信息 $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId); $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId); if (!$customerData->isSuccess()) { return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode()); } $customerData = $customerData->getData(); //wx /********************生成小程序唤起支付用到的参数(原配置)*****/ /**$weixinConfigData = Factory::config()->get('weixin'); * if (empty($weixinConfigData)) { * return ResultWrapper::fail('微信配置错误', ErrorCode::$configEroor); * }*/ // 获取当前登录企业的小程序配置 $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->enterpriseId); if (empty($miniprogramSetting)) { return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor); } $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid // 获取配置文件微信公众号 /**$weixinConfigData = Factory::config()->get('weixin'); * if (empty($weixinConfigData)) { * return ResultWrapper::fail('微信配置错误', ErrorCode::$configEroor); * }*/ switch ($source) { case StatusCode::$source['ByteProgram']; //$appid = $weixinConfigData['offiaccount']['appid']; $appid = $miniprogramSetting['appid']; $paySource = 'byteDanceH5'; break; case StatusCode::$source['miniProgram']; $appid = $miniprogramSetting['appid']; $paySource = 'miniProgram'; break; default : $appid = $payment['mobileAppid']; $paySource = 'APP'; break; } $weixinConfigData = [ 'mch_id' => $payment['mchId'], 'apiPartnerKey' => $payment['apiPartnerKey'], 'appid' => $appid ]; $attach = $this->enterpriseId . '|' . $this->userCenterId . '|' . implode('|', [$params['order_id']]); //$objPay = new Pay($appid, $weixinConfigData['pay']['mch_id'], $weixinConfigData['pay']['apiPartnerKey']);//old $objPay = new Pay($appid, $weixinConfigData['mch_id'], $weixinConfigData['apiPartnerKey']); $payParams = $objPay->unifiedorder($params['order_id'],$params['pay_price'], $ip, $paySource, '千鸟云商', $customerData['openId'], $attach); return ResultWrapper::success($payParams->getData()); } public function yuePay($parma) { //获取客户信息 $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId); $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId); if (!$customerData->isSuccess()) { return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode()); } $customerData = $customerData->getData(); $price = $parma['pay_price'];// 实际支付 if ($customerData['memberBalance'] < $price){ return ResultWrapper::fail('余额不足', 1005); } $before = $customerData['memberBalance']; $after = $customerData['memberBalance'] - $price; $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId); $Mbalance->create($customerData['id'], $customerData['userCenterId'], 4, $before, $price, $after,'支付服务订单', '余额支付服务订单'); $res = $this->paySuccess($parma['order_id']); if ($res){ return ResultWrapper::success('支付成功'); }else{ return ResultWrapper::fail('支付失败', 1005); } } /** * 退款 * @param $data * @return void */ public function refund($data) { if ($data['pay_price'] > 0){ if ($data['pay_type'] == 1){ $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId); $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId); if (!$customerData->isSuccess()) { return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode()); } $customerData = $customerData->getData(); $price = $data['pay_price'];// 实际支付 $before = $customerData['memberBalance']; $after = $customerData['memberBalance'] + $price; $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId); $Mbalance->create($customerData['id'], $customerData['userCenterId'], 5, $before, $price, $after,'取消服务订单', '取消服务订单退余额'); }elseif ($data['pay_type'] == 2){ $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId); $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId); if (!$customerData->isSuccess()) { return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode()); } $customerData = $customerData->getData(); $price = $data['pay_price'];// 实际支付 $before = $customerData['memberBalance']; $after = $customerData['memberBalance'] + $price; $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId); $Mbalance->create($customerData['id'], $customerData['userCenterId'], 5, $before, $price, $after,'取消服务订单', '取消服务订单退余额'); } } return true; } /** * 取出员工 * @param $param * @return int|string|null * @throws Exception */ public function getStaff($param, $shop_id) { $time = explode(' ', $param['time']); $Mrostering = new MShopRostering($this->enterpriseId); $Msubscribe = new DShopSubscribe('default'); $Msubscribe->setTable('qianniao_shop_subscribe_'.$this->enterpriseId); $join = 'Left Join qianniao_staff_'.$this->enterpriseId.' as b on a.uid = b.id'; // $rostering = $this->objRostering->select([['a.time', 'like', '%'.$time.'%'], ['a.template_id', '>', 1], ['a.shop_id','=',$this->shopId]], 'a.*', 'id DESC', '', '', [], false, true, $join)->getData(); $db = new DShopRostering('default'); $db->setTable('qianniao_shop_rostering_'.$this->enterpriseId); $rostering = $db->select([['a.time', 'like', '%'.$time[0].'%'], ['a.template_id', '>', 1], ['a.shop_id','=',$shop_id], ['b.is_technician' ,'=', 1]],'*' , 'a.id DESC', '', '', [], false, true, $join); // $rostering = $Mrostering->select([['time', 'like', '%'.$time[0].'%'], ['template_id', '>', 1], ['shop_id','=', $shop_id]])->getData()['data']; if ($rostering){ $staff_id = []; foreach ($rostering as $item){ $work = json_decode($item['time_slot']); $work_count = count($work); for ($i = 0; $i < $work_count;$i++){ if (strtotime($time[1]) >= strtotime($time[0].' '.$work[$i][0]) and strtotime($time[1]) <= strtotime($time[0].' '.$work[$i][1])){ $staff_id[] = $item['uid']; } } } if (!empty($staff_id)){ $staff = []; if ($staff_id){ foreach ($staff_id as $k => $vo) { $orderCount = $Msubscribe->count([['uid', '=', $vo], ['paid', '=', 1],['time', '>=', strtotime($time[0])], ['time', '<=', strtotime($time[0])+86400]]); $staff[$vo] = $orderCount; } } asort($staff); return key($staff);//取出当前订单数最小的员工 }else{ return false; } } } public function count($where) { $count = $this->obj->count($where); $count = empty($count) ? 0 : $count; return $count; } /** * 支付成功后 * @return void */ public function paySuccess($order_id) { $order = $this->obj->update(['paid' => 1], ['order_id' => $order_id]); if ($order){ return true; } return false; } }