ApiShopProject.Class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace JinDouYun\Controller\Shop;
  3. use JinDouYun\Controller\BaseController;
  4. use JinDouYun\Dao\BaseDao;
  5. use JinDouYun\Dao\Customer\DCustomerCardNum;
  6. use JinDouYun\Dao\Department\DStaff;
  7. use JinDouYun\Dao\Shop\DShopRostering;
  8. use JinDouYun\Model\Customer\MCustomer;
  9. use JinDouYun\Model\Customer\MCustomerCard;
  10. use JinDouYun\Model\Shop\MShopProject;
  11. use JinDouYun\Model\Shop\MShopRostering;
  12. use JinDouYun\Model\Shop\MShopSubscribe;
  13. use Mall\Framework\Core\ErrorCode;
  14. use Mall\Framework\Core\Ding;
  15. use Mall\Framework\Factory;
  16. class ApiShopProject extends BaseController
  17. {
  18. private $obj;
  19. private $objCustomer;
  20. private $objCard;
  21. private $objRostering;
  22. private $objSub;
  23. private $objStaff;
  24. public function __construct($isCheckAcl = true, $isMustLogin = false, $checkToken = true, $getAreaCode = false, $checkShopToken = true, $checkSupplierToken = false)
  25. {
  26. parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode, $checkShopToken, $checkSupplierToken);
  27. $authorization = $this->request->getServerParam('HTTP_AUTHORIZATION');
  28. if (!empty($authorization)) {
  29. self::getUserIdByAuthorization();
  30. }
  31. $shopToken = $this->request->param('SHOP-TOKEN');
  32. if (!empty($shopToken)){
  33. self::getShopIdByShopToken($shopToken);
  34. }
  35. $this->obj = new MShopProject($this->onlineEnterpriseId, $this->onlineUserId);
  36. $this->objCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  37. $this->objCard = new MCustomerCard($this->onlineEnterpriseId);
  38. $this->objCardNum = new DCustomerCardNum('default');
  39. $this->objRostering = new MShopRostering($this->onlineEnterpriseId, $this->onlineUserId);
  40. $this->objSub = new MShopSubscribe($this->onlineEnterpriseId, $this->onlineUserId);
  41. $this->objStaff = new DStaff('default');
  42. $this->objStaff->setTable('qianniao_staff_'.$this->onlineEnterpriseId);
  43. $this->cache = Factory::cache('user');
  44. }
  45. /**
  46. * 列表
  47. */
  48. public function list()
  49. {
  50. $params = $this->request->getRawJson();
  51. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  52. $selectParams['limit'] = $pageParams['limit'];
  53. $selectParams['offset'] = $pageParams['offset'];
  54. $selectParams['enterprise_id'] = $this->onlineEnterpriseId;
  55. // $selectParams['shop_id'] = $this->shopId;
  56. //项目昵称
  57. if (isset($params['name']) && !empty($params['name'])) {
  58. $selectParams['name'] = $params['name'];
  59. }
  60. if (isset($params['type']) && !empty($params['type'])){
  61. $selectParams['type'] = $params['type'];
  62. }
  63. $selectParams['is_show'] = 1 ;
  64. $result = $this->obj->list($selectParams);
  65. if ($result->isSuccess()) {
  66. $returnData = $result->getData();
  67. foreach ($returnData['data'] as &$item)
  68. {
  69. $item['sold'] = $this->objSub->count([['project', 'like', '%'.$item['id'].'%']]);
  70. }
  71. $pageData = [
  72. 'pageIndex' => $params['page'],
  73. 'pageSize' => $params['pageSize'],
  74. 'pageTotal' => $returnData['total'],
  75. ];
  76. parent::sendOutput($returnData['data'], 0, $pageData);
  77. } else {
  78. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  79. }
  80. }
  81. /**
  82. * 详情
  83. * @return void
  84. */
  85. public function details()
  86. {
  87. $where = [];
  88. $id = $this->request->param('id');
  89. if (!empty($id)) {
  90. $where['id'] = $id;
  91. }
  92. $result = $this->obj->details($where);
  93. if ($result->isSuccess()) {
  94. $result = $result->getData();
  95. $result['sold'] = $this->objSub->count([['project', 'like', '%'.$result['id'].'%']]);
  96. $result['result'] = html_entity_decode($result['result']);
  97. parent::sendOutput($result);
  98. } else {
  99. parent::sendOutput($result->getData(), $result->getErrorCode());
  100. }
  101. }
  102. /**
  103. * 计算价格
  104. * @return void
  105. */
  106. public function calculation()
  107. {
  108. $id = $this->request->param('id');
  109. if (empty($this->onlineUserId)) parent::sendOutput('未登录', 1002);
  110. $project = $this->obj->get(['id' => $id]);
  111. $pay_price = 0;// 实际支付价格
  112. $discount_price = 0;// 优惠金额
  113. $customer = $this->objCustomer->get(['userCenterId' => $this->onlineUserId]);
  114. $numWhere = [
  115. ['customer_id', '=', $customer['id']],
  116. ['project_id', '=', $id],
  117. ['number', '>', 0],
  118. ];
  119. $card = $this->objCardNum->get($numWhere);
  120. $to_price = $project['price'];
  121. $cost_price = $project['cost_price'];
  122. $card_id = 0;
  123. if ($card) {
  124. $card_id = $card['id'];
  125. $discount_price = $project['price'];
  126. } else {
  127. $pay_price = $project['price'];
  128. }
  129. $user = $this->objCustomer->get(['userCenterId' => $this->onlineUserId]);
  130. $data = [
  131. 'project' => $id,
  132. 'customer_id' => $customer['id'],
  133. 'pay_price' => $pay_price,
  134. 'discount_price' => $discount_price,
  135. 'to_price' => $to_price,
  136. 'cost_price' => $cost_price,
  137. 'order_id' => 'wx'.$customer['id'].time().$this->getRandPass(),
  138. 'card_id' => $card_id,
  139. 'memberBalance' => $user['memberBalance']
  140. ];
  141. $this->cache->hset($data['order_id'], $this->onlineUserId, json_encode($data));
  142. parent::sendOutput($data);
  143. }
  144. /**
  145. * 创建订单
  146. * @return void
  147. * @throws \Exception
  148. */
  149. public function create()
  150. {
  151. $params = $this->request->getRawJson();
  152. $order = $this->cache->hget($params['order_id'], $this->onlineUserId);
  153. if (empty($order)) parent::sendOutput('未查到订单',ErrorCode::$dberror);
  154. $order = json_decode($order);
  155. if ($params['staff_id']){
  156. $order->uid = $params['staff_id'];
  157. }else{
  158. $staff = $this->objSub->getStaff($params, $this->shopId);
  159. if ($staff == false) parent::sendOutput('当前时间段未匹配到员工',ErrorCode::$dberror);
  160. $order->uid = $staff;
  161. }
  162. $base = new BaseDao();
  163. $base->beginTransaction();
  164. $orderInset = [
  165. 'shop_id' => $this->shopId,
  166. 'project' => $order->project,
  167. 'customer_id' => $order->customer_id,
  168. 'pay_price' => $order->pay_price,
  169. 'discount_price' => $order->discount_price,
  170. 'to_price' => $order->to_price,
  171. 'cost_price' => $order->cost_price,
  172. 'order_id' => $order->order_id,
  173. 'card_id' => $order->card_id,
  174. 'uid' => $order->uid,
  175. 'pay_type' => $params['pay_type'],
  176. 'time' => $params['time'],
  177. 'remarks' => $params['remarks'],
  178. ];
  179. if ($order->pay_price == 0){
  180. $orderInset['paid'] = 1;
  181. }
  182. $orderRes = $this->objSub->insert($orderInset);
  183. $this->cache->hdel($params['order_id'], $this->onlineUserId);
  184. if ($orderRes->isSuccess()){
  185. if ($order->pay_price > 0){
  186. if ($params['pay_type'] == 1){
  187. //余额支付
  188. $res = $this->objSub->yuePay($orderInset);
  189. if (!$res->isSuccess()){
  190. parent::sendOutput($res->getData(), ErrorCode::$dberror);
  191. }
  192. $base->commit();
  193. parent::sendOutput(['status' => 'complete', 'data' => '支付成功']);
  194. }elseif ($params['pay_type'] == 2){
  195. //微信支付
  196. $res = $this->objSub->wxPay($orderInset, $this->request->get_onlineip(), $params['source']);
  197. $base->commit();
  198. parent::sendOutput(['status' => 'weixinPay', 'data' => $res->getData()]);
  199. }elseif ($params['pay_type'] == 3){
  200. //支付宝支付
  201. }
  202. }
  203. $base->commit();
  204. parent::sendOutput(['status' => 'complete', 'data' => '支付成功']);
  205. }else{
  206. $base->rollBack();
  207. parent::sendOutput($orderRes->getData(),ErrorCode::$dberror);
  208. }
  209. }
  210. /**
  211. * 待支付支付
  212. * @return void
  213. * @throws \Exception
  214. */
  215. public function payment()
  216. {
  217. $id = $this->request->param('id');
  218. $pay_type = $this->request->param('pay_type');
  219. $source = $this->request->param('source');
  220. $order = $this->objSub->details(['id' => $id]);
  221. if (!$order->isSuccess()) parent::sendOutput('订单不存在',ErrorCode::$dberror);
  222. $base = new BaseDao();
  223. $base->beginTransaction();
  224. $order = $order->getData();
  225. if ($pay_type == 1){
  226. //余额支付
  227. $res = $this->objSub->yuePay($order);
  228. if (!$res->isSuccess()){
  229. parent::sendOutput($res->getData(), ErrorCode::$dberror);
  230. }
  231. $base->commit();
  232. parent::sendOutput(['status' => 'complete', 'data' => '支付成功']);
  233. }elseif ($pay_type == 2){
  234. //微信支付
  235. $res = $this->objSub->wxPay($order, $this->request->get_onlineip(), $source);
  236. $base->commit();
  237. parent::sendOutput(['status' => 'weixinPay', 'data' => $res->getData()]);
  238. }elseif ($pay_type == 3){
  239. //支付宝支付
  240. }
  241. $base->rollBack();
  242. parent::sendOutput('支付方式不存在',ErrorCode::$dberror);
  243. }
  244. /**
  245. * 日期查找员工
  246. * @return void
  247. *
  248. */
  249. public function choice()
  250. {
  251. $time = $this->request->param('time');
  252. if (empty($time)) parent::sendOutput('选择预约时间', ErrorCode::$dberror);
  253. $join = 'Left Join qianniao_staff_'.$this->onlineEnterpriseId.' as b on a.uid = b.id';
  254. // $rostering = $this->objRostering->select([['a.time', 'like', '%'.$time.'%'], ['a.template_id', '>', 1], ['a.shop_id','=',$this->shopId]], 'a.*', 'id DESC', '', '', [], false, true, $join)->getData();
  255. $db = new DShopRostering('default');
  256. $db->setTable('qianniao_shop_rostering_'.$this->onlineEnterpriseId);
  257. $filed = 'a.*,b.staffName,b.avatar,b.evaluate,b.info,b.is_technician';
  258. $rostering = $db->select([['a.time', 'like', '%'.$time.'%'], ['a.template_id', '>', 1], ['a.shop_id','=',$this->shopId], ['b.is_technician' ,'=', 1]],$filed , 'a.id DESC', '', '', [], false, true, $join);
  259. foreach ($rostering as &$item)
  260. {
  261. $item['time_slot'] = json_decode($item['time_slot']);
  262. $item['clock'] = json_decode($item['clock']);
  263. }
  264. if (!empty($rostering)){
  265. parent::sendOutput($rostering);
  266. }
  267. parent::sendOutput('当天未找到员工', ErrorCode::$dberror);
  268. }
  269. /**
  270. * 员工可预约时间段
  271. * @return void
  272. */
  273. public function choice_time()
  274. {
  275. $time = $this->request->param('time');
  276. $uid = $this->request->param('uid');
  277. $rostering = $this->objRostering->details([['time', 'like', '%'.$time.'%'], ['uid', '=', $uid]])->getData();
  278. if(isset($rostering['time_slot']) && isset($rostering['clock'])) {
  279. $rostering['time_slot'] = json_decode($rostering['time_slot'], true);
  280. $rostering['clock'] = json_decode($rostering['clock'], true);
  281. $Stime = strtotime($time);
  282. $Etime = strtotime($time) + 86400;
  283. $where[] = ['uid', '=', $uid];
  284. $where[] = ['time', '>=', $Stime];
  285. $where[] = ['time', '<=', $Etime];
  286. $order = $this->objSub->select($where)->getData();
  287. $rostering['reserved'] = [];
  288. if ($order) {
  289. foreach ($order['data'] as $item) {
  290. $rostering['reserved'][] = date('H:i:s', $item['time']);
  291. }
  292. }
  293. }
  294. parent::sendOutput($rostering);
  295. }
  296. }