MShopSubscribe.Class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /**
  3. * 商铺管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/31
  7. * Time: 15:02
  8. */
  9. namespace JinDouYun\Model\Shop;
  10. use AlibabaCloud\SDK\Dingtalk\Vbizfinance_1_0\Models\QueryReceiptsBaseInfoResponseBody\list_\project;
  11. use Exception;
  12. use JinDouYun\Cache\SystemCache;
  13. use JinDouYun\Dao\Customer\DCustomerCardNum;
  14. use JinDouYun\Dao\Department\DStaff;
  15. use JinDouYun\Dao\Shop\DShopProject;
  16. use JinDouYun\Dao\Shop\DShop;
  17. use JinDouYun\Dao\Shop\DShopRostering;
  18. use JinDouYun\Dao\Shop\DShopSubscribe;
  19. use JinDouYun\Dao\Customer\DCustomer;
  20. use JinDouYun\Dao\UserCenter\DUserCenter;
  21. use JinDouYun\Model\Customer\MCustomer;
  22. use JinDouYun\Model\Customer\MMemberBalanceDetail;
  23. use JinDouYun\Model\System\MPaymentSetting;
  24. use Mall\Framework\Core\ErrorCode;
  25. use Mall\Framework\Core\StatusCode;
  26. use Mall\Framework\Core\ResultWrapper;
  27. use JinDouYun\Model\MBaseModel;
  28. use Monolog\Handler\IFTTTHandler;
  29. use Util\WeiXin\Pay;
  30. class MShopSubscribe extends MBaseModel
  31. {
  32. private $obj;
  33. private $cutTable = 250000;
  34. private $enterpriseId;
  35. private $userCenterId;
  36. private $objMShopPartner;
  37. private $objStaff;
  38. private $Dproject;
  39. private $objCustomer;
  40. private $objShop;
  41. public function __construct($enterpriseId, $userCenterId = '')
  42. {
  43. $this->enterpriseId = $enterpriseId;
  44. $this->userCenterId = $userCenterId;
  45. parent::__construct($this->enterpriseId, $this->userCenterId);
  46. $this->obj = new DShopSubscribe('default');
  47. $this->obj->setTable('qianniao_shop_subscribe_'.$enterpriseId);
  48. $this->Dproject = new DShopProject('default');
  49. $this->objStaff = new DStaff('default');
  50. $this->objCustomer = new DCustomer('default');
  51. $this->objShop = new DShop('default');
  52. $this->objStaff->setTable('qianniao_staff_'.$enterpriseId);
  53. $this->objShop->setTable('qianniao_shop_1');
  54. $this->objCustomer->setTable('qianniao_customer_'.$enterpriseId);
  55. }
  56. /**
  57. * 列表
  58. * @param $where
  59. * @return mixed
  60. */
  61. public function list($where)
  62. {
  63. $limit = $where['limit'];
  64. unset($where['limit']);
  65. $offset = $where['offset'];
  66. unset($where['offset']);
  67. $wherein = [];
  68. if (isset($where['uid'])){
  69. $wherein[] = ['uid', '=', $where['uid']];
  70. }
  71. if (isset($where['shop_id'])){
  72. $wherein[] = ['shop_id', '=', $where['shop_id']];
  73. }
  74. if (isset($where['status'])){
  75. $wherein[] = ['status', '>=', 0];
  76. }
  77. if (isset($where['customer_id'])){
  78. $wherein[] = ['customer_id', '=', $where['customer_id']];
  79. }
  80. if (isset($where['time'])){
  81. $wherein[] = ['time', '>=', strtotime($where['time'][0])];
  82. $wherein[] = ['time', '<=', strtotime($where['time'][1])];
  83. unset($where['time']);
  84. }
  85. if (isset($where['type'])){
  86. if ($where['type'] == 1){
  87. //待支付
  88. $wherein[] = ['status', '=', 0];
  89. $wherein[] = ['paid', '=', 0];
  90. }
  91. if ($where['type'] == 2){
  92. //已支付
  93. $wherein[] = ['status', '=', 0];
  94. $wherein[] = ['paid', '=', 1];
  95. }
  96. if ($where['type'] == 3){
  97. //已完成
  98. $wherein[] = ['status', '=', 1];
  99. $wherein[] = ['paid', '=', 1];
  100. $wherein[] = ['refund', '=', 0];
  101. }
  102. if ($where['type'] == 4){
  103. //退款订单
  104. $wherein[] = ['status', '=', -1];
  105. }
  106. unset($where['type']);
  107. }
  108. $dbResult = $this->obj->select($wherein, '*', 'id DESC', $limit, $offset);
  109. $count = $this->obj->count($wherein);
  110. if ($dbResult === false) {
  111. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  112. }
  113. foreach ($dbResult as &$item)
  114. {
  115. $customer = $this->objCustomer->get(['id' => $item['customer_id']]);
  116. $staff = $this->objStaff->get(['id' => $item['uid']]);
  117. $item['staff'] = $staff['staffName'];
  118. $item['avatar'] = $staff['avatar'];
  119. $item['customer'] = $customer['name'];
  120. $item['customer_avatar'] = $customer['avatar'];
  121. $item['project'] = $this->Dproject->select([['id', 'in', $item['project']]], 'id,name,price,image');
  122. $shop = $this->objShop->get(['id' => $item['shop_id']]);
  123. $item['shop_name'] = $shop['name'];
  124. $Duser = new DUserCenter('default');
  125. $user = $Duser->get(['id' => $customer['userCenterId']]);
  126. $item['customer_mobile'] = $user['mobile'];
  127. }
  128. $return = [
  129. 'data' => $dbResult,
  130. 'total' => $count,
  131. ];
  132. if($return === false){
  133. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  134. }else{
  135. return ResultWrapper::success($return);
  136. }
  137. }
  138. /**
  139. * 详情
  140. * @param $where
  141. * @return mixed
  142. */
  143. public function details($where)
  144. {
  145. if(empty($where)){
  146. return ResultWrapper::success($where);
  147. }
  148. $dbResult = $this->obj->get($where);
  149. if(empty($dbResult)){
  150. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  151. }
  152. $dbResult['staff'] = $this->objStaff->get(['id' => $dbResult['uid'], 'shopId' => $dbResult['shop_id']], 'staffName');
  153. $dbResult['customer'] = $this->objCustomer->get(['id' => $dbResult['customer_id']], 'name,avatar,userCenterId');
  154. $Duser = new DUserCenter('default');
  155. $user = $Duser->get(['id' => $dbResult['customer']['userCenterId']]);
  156. $dbResult['customer']['mobile'] = $user['mobile'];
  157. return ResultWrapper::success(self::formatInfo($dbResult));
  158. }
  159. /**
  160. * Doc: (des="添加")
  161. * User: XMing
  162. * Date: 2020/7/15
  163. * Time: 10:42 上午
  164. * @throws Exception
  165. * @param array $params
  166. * @return ResultWrapper
  167. */
  168. public function insert(array $params)
  169. {
  170. $params['time'] = strtotime($params['time']);
  171. if (isset($params['card_id'])){
  172. $card_id = $params['card_id'];
  173. unset($params['card_id']);
  174. }
  175. $res = $this->obj->insert($params);
  176. if ($res){
  177. if (isset($params['paid'])){
  178. if (isset($card_id)){
  179. $this->reduceCard($card_id);
  180. }
  181. }
  182. return ResultWrapper::success('添加成功');
  183. }else{
  184. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  185. }
  186. }
  187. public function reduceCard($id)
  188. {
  189. $cardNum = new DCustomerCardNum('default');
  190. $card = $cardNum->get(['id' => $id]);
  191. if ($card['number'] <= 0) return false;
  192. $cardNum->update(['number' => $card['number'] -1], $card['id']);
  193. return true;
  194. }
  195. public function update($params, $id)
  196. {
  197. $data = $this->obj->get($id);
  198. if (!$data){
  199. return ResultWrapper::fail('数据不存在', ErrorCode::$dberror);
  200. }
  201. $dbResult = $this->obj->update($params,$id);
  202. if($dbResult === false){
  203. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  204. }
  205. return ResultWrapper::success('修改成功');
  206. }
  207. /**
  208. * 格式数据详情
  209. * @param $data
  210. * @return mixed
  211. */
  212. public function formatInfo($data)
  213. {
  214. return $data;
  215. }
  216. public function delete($id)
  217. {
  218. $res = $this->obj->delete(['id' => $id]);
  219. if ($res){
  220. return ResultWrapper::success('删除成功');
  221. }
  222. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  223. }
  224. public function select($where)
  225. {
  226. $dbResult = $this->obj->select($where, '*', 'id DESC');
  227. if ($dbResult === false) {
  228. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  229. }
  230. $return = [
  231. 'data' => $dbResult,
  232. ];
  233. if($return === false){
  234. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  235. }else{
  236. return ResultWrapper::success($return);
  237. }
  238. }
  239. /**
  240. * 微信支付
  241. * @param $params
  242. * @return mixed
  243. * @throws Exception
  244. */
  245. public function wxPay($params, $ip, $source)
  246. {
  247. $objSystemCache = new SystemCache();
  248. $objMPaymentSetting = new MPaymentSetting($this->userCenterId, $this->enterpriseId);
  249. $result = $objMPaymentSetting->getPayData(1);
  250. if (!$result->isSuccess() || empty($result->getData())) {
  251. return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor);
  252. }
  253. $payment = $result->getData();
  254. //获取客户信息
  255. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  256. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  257. if (!$customerData->isSuccess()) {
  258. return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
  259. }
  260. $customerData = $customerData->getData();
  261. //wx
  262. /********************生成小程序唤起支付用到的参数(原配置)*****/
  263. /**$weixinConfigData = Factory::config()->get('weixin');
  264. * if (empty($weixinConfigData)) {
  265. * return ResultWrapper::fail('微信配置错误', ErrorCode::$configEroor);
  266. * }*/
  267. // 获取当前登录企业的小程序配置
  268. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->enterpriseId);
  269. if (empty($miniprogramSetting)) {
  270. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  271. }
  272. $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid
  273. // 获取配置文件微信公众号
  274. /**$weixinConfigData = Factory::config()->get('weixin');
  275. * if (empty($weixinConfigData)) {
  276. * return ResultWrapper::fail('微信配置错误', ErrorCode::$configEroor);
  277. * }*/
  278. switch ($source) {
  279. case StatusCode::$source['ByteProgram'];
  280. //$appid = $weixinConfigData['offiaccount']['appid'];
  281. $appid = $miniprogramSetting['appid'];
  282. $paySource = 'byteDanceH5';
  283. break;
  284. case StatusCode::$source['miniProgram'];
  285. $appid = $miniprogramSetting['appid'];
  286. $paySource = 'miniProgram';
  287. break;
  288. default :
  289. $appid = $payment['mobileAppid'];
  290. $paySource = 'APP';
  291. break;
  292. }
  293. $weixinConfigData = [
  294. 'mch_id' => $payment['mchId'],
  295. 'apiPartnerKey' => $payment['apiPartnerKey'],
  296. 'appid' => $appid
  297. ];
  298. $attach = $this->enterpriseId . '|' . $this->userCenterId . '|' . implode('|', [$params['order_id']]);
  299. //$objPay = new Pay($appid, $weixinConfigData['pay']['mch_id'], $weixinConfigData['pay']['apiPartnerKey']);//old
  300. $objPay = new Pay($appid, $weixinConfigData['mch_id'], $weixinConfigData['apiPartnerKey']);
  301. $payParams = $objPay->unifiedorder($params['order_id'],$params['pay_price'], $ip, $paySource, '千鸟云商', $customerData['openId'], $attach);
  302. return ResultWrapper::success($payParams->getData());
  303. }
  304. public function yuePay($parma)
  305. {
  306. //获取客户信息
  307. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  308. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  309. if (!$customerData->isSuccess()) {
  310. return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
  311. }
  312. $customerData = $customerData->getData();
  313. $price = $parma['pay_price'];// 实际支付
  314. if ($customerData['memberBalance'] < $price){
  315. return ResultWrapper::fail('余额不足', 1005);
  316. }
  317. $before = $customerData['memberBalance'];
  318. $after = $customerData['memberBalance'] - $price;
  319. $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId);
  320. $Mbalance->create($customerData['id'], $customerData['userCenterId'], 4, $before, $price, $after,'支付服务订单', '余额支付服务订单');
  321. $res = $this->paySuccess($parma['order_id']);
  322. if ($res){
  323. return ResultWrapper::success('支付成功');
  324. }else{
  325. return ResultWrapper::fail('支付失败', 1005);
  326. }
  327. }
  328. /**
  329. * 退款
  330. * @param $data
  331. * @return void
  332. */
  333. public function refund($data)
  334. {
  335. if ($data['pay_price'] > 0){
  336. if ($data['pay_type'] == 1){
  337. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  338. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  339. if (!$customerData->isSuccess()) {
  340. return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
  341. }
  342. $customerData = $customerData->getData();
  343. $price = $data['pay_price'];// 实际支付
  344. $before = $customerData['memberBalance'];
  345. $after = $customerData['memberBalance'] + $price;
  346. $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId);
  347. $Mbalance->create($customerData['id'], $customerData['userCenterId'], 5, $before, $price, $after,'取消服务订单', '取消服务订单退余额');
  348. }elseif ($data['pay_type'] == 2){
  349. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  350. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  351. if (!$customerData->isSuccess()) {
  352. return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
  353. }
  354. $customerData = $customerData->getData();
  355. $price = $data['pay_price'];// 实际支付
  356. $before = $customerData['memberBalance'];
  357. $after = $customerData['memberBalance'] + $price;
  358. $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId);
  359. $Mbalance->create($customerData['id'], $customerData['userCenterId'], 5, $before, $price, $after,'取消服务订单', '取消服务订单退余额');
  360. }
  361. }
  362. return true;
  363. }
  364. /**
  365. * 取出员工
  366. * @param $param
  367. * @return int|string|null
  368. * @throws Exception
  369. */
  370. public function getStaff($param, $shop_id)
  371. {
  372. $time = explode(' ', $param['time']);
  373. $Mrostering = new MShopRostering($this->enterpriseId);
  374. $Msubscribe = new DShopSubscribe('default');
  375. $Msubscribe->setTable('qianniao_shop_subscribe_'.$this->enterpriseId);
  376. $join = 'Left Join qianniao_staff_'.$this->enterpriseId.' as b on a.uid = b.id';
  377. // $rostering = $this->objRostering->select([['a.time', 'like', '%'.$time.'%'], ['a.template_id', '>', 1], ['a.shop_id','=',$this->shopId]], 'a.*', 'id DESC', '', '', [], false, true, $join)->getData();
  378. $db = new DShopRostering('default');
  379. $db->setTable('qianniao_shop_rostering_'.$this->enterpriseId);
  380. $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);
  381. // $rostering = $Mrostering->select([['time', 'like', '%'.$time[0].'%'], ['template_id', '>', 1], ['shop_id','=', $shop_id]])->getData()['data'];
  382. if ($rostering){
  383. $staff_id = [];
  384. foreach ($rostering as $item){
  385. $work = json_decode($item['time_slot']);
  386. $work_count = count($work);
  387. for ($i = 0; $i < $work_count;$i++){
  388. if (strtotime($time[1]) >= strtotime($time[0].' '.$work[$i][0]) and strtotime($time[1]) <= strtotime($time[0].' '.$work[$i][1])){
  389. $staff_id[] = $item['uid'];
  390. }
  391. }
  392. }
  393. if (!empty($staff_id)){
  394. $staff = [];
  395. if ($staff_id){
  396. foreach ($staff_id as $k => $vo)
  397. {
  398. $orderCount = $Msubscribe->count([['uid', '=', $vo], ['paid', '=', 1],['time', '>=', strtotime($time[0])], ['time', '<=', strtotime($time[0])+86400]]);
  399. $staff[$vo] = $orderCount;
  400. }
  401. }
  402. asort($staff);
  403. return key($staff);//取出当前订单数最小的员工
  404. }else{
  405. return false;
  406. }
  407. }
  408. }
  409. public function count($where)
  410. {
  411. $count = $this->obj->count($where);
  412. $count = empty($count) ? 0 : $count;
  413. return $count;
  414. }
  415. /**
  416. * 支付成功后
  417. * @return void
  418. */
  419. public function paySuccess($order_id)
  420. {
  421. $order = $this->obj->update(['paid' => 1], ['order_id' => $order_id]);
  422. if ($order){
  423. return true;
  424. }
  425. return false;
  426. }
  427. }