123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <?php
- /**
- * 商铺管理模块
- * Created by PhpStorm.
- * User: wxj
- * Date: 2019/10/31
- * Time: 15:02
- */
- namespace JinDouYun\Model\Shop;
- use AlibabaCloud\SDK\Dingtalk\Vbizfinance_1_0\Models\QueryReceiptsBaseInfoResponseBody\list_\project;
- use Exception;
- use JinDouYun\Cache\SystemCache;
- use JinDouYun\Dao\Customer\DCustomerCardNum;
- use JinDouYun\Dao\Department\DStaff;
- use JinDouYun\Dao\Shop\DShopProject;
- use JinDouYun\Dao\Shop\DShop;
- use JinDouYun\Dao\Shop\DShopRostering;
- use JinDouYun\Dao\Shop\DShopSubscribe;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\UserCenter\DUserCenter;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\Customer\MMemberBalanceDetail;
- use JinDouYun\Model\System\MPaymentSetting;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Model\MBaseModel;
- use Monolog\Handler\IFTTTHandler;
- use Util\WeiXin\Pay;
- class MShopSubscribe extends MBaseModel
- {
- private $obj;
- private $cutTable = 250000;
- private $enterpriseId;
- private $userCenterId;
- private $objMShopPartner;
- private $objStaff;
- private $Dproject;
- private $objCustomer;
- private $objShop;
- public function __construct($enterpriseId, $userCenterId = '')
- {
- $this->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;
- }
- }
|