MShopCardOrder.Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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\Vcrm_1_0\Models\DescribeRelationMetaResponseBody\relationMetaDTOList\items\props\fields\relateProps;
  11. use Exception;
  12. use JinDouYun\Cache\SystemCache;
  13. use JinDouYun\Dao\BaseDao;
  14. use JinDouYun\Dao\Customer\DCustomer;
  15. use JinDouYun\Dao\Customer\DCustomerCard;
  16. use JinDouYun\Dao\Customer\DCustomerCardNum;
  17. use JinDouYun\Dao\Shop\DShopCard;
  18. use JinDouYun\Dao\Shop\DShopCardBind;
  19. use JinDouYun\Dao\Shop\DShopCardOrder;
  20. use JinDouYun\Model\Customer\MCustomer;
  21. use JinDouYun\Model\Customer\MMemberBalanceDetail;
  22. use JinDouYun\Model\System\MPaymentSetting;
  23. use Mall\Framework\Core\ErrorCode;
  24. use Mall\Framework\Core\StatusCode;
  25. use Mall\Framework\Core\ResultWrapper;
  26. use JinDouYun\Model\MBaseModel;
  27. use Monolog\Handler\IFTTTHandler;
  28. use Util\WeiXin\Pay;
  29. //use Util\Common\PartnerTools;
  30. class MShopCardOrder extends MBaseModel
  31. {
  32. private $obj;
  33. private $cutTable = 250000;
  34. private $enterpriseId;
  35. private $userCenterId;
  36. private $DCardBind;
  37. private $DCard;
  38. private $objDCustomer;
  39. public function __construct($enterpriseId, $userCenterId = '')
  40. {
  41. $this->enterpriseId = $enterpriseId;
  42. $this->userCenterId = $userCenterId;
  43. parent::__construct($this->enterpriseId, $this->userCenterId);
  44. $this->obj = new DShopCardOrder('default');
  45. $this->DCardBind = new DShopCardBind('default');
  46. $this->DCard = new DShopCard('default');
  47. $this->DCustomerCard = new DCustomerCard('default');
  48. $this->DCustomerCardNum = new DCustomerCardNum('default');
  49. $this->objDCustomer = new DCustomer();
  50. $this->objDCustomer->setTable('qianniao_customer_' . $this->enterpriseId);
  51. }
  52. /**
  53. * 列表
  54. * @param $where
  55. * @return mixed
  56. */
  57. public function list($where)
  58. {
  59. $limit = $where['limit'];
  60. unset($where['limit']);
  61. $offset = $where['offset'];
  62. unset($where['offset']);
  63. $join = 'Left Join qianniao_customer_'.$this->enterpriseId.' as b on a.customer_id = b.id';
  64. $join .= ' Left Join qianniao_shop_card as c on a.card_id = c.id';
  65. $wherein[] = ['a.enterprise_id' ,'=' ,$this->enterpriseId];
  66. if (isset($where['name']) and !empty($where['name'])){
  67. $wherein[] = ['b.id|b.name', '=', $where['name']];
  68. }
  69. if (isset($where['card_name']) and !empty($where['card_name'])){
  70. $wherein[] = ['c.id|c.name', '=', $where['card_name']];
  71. }
  72. if (isset($where['paid'])){
  73. $wherein[] = ['paid' , '=', 1];
  74. }
  75. if (isset($where['time'])){
  76. $wherein[] = ['a.createTime', '>=', strtotime($where['time'][0])];
  77. $wherein[] = ['a.createTime', '<=', strtotime($where['time'][1])];
  78. }
  79. if(isset($where["customer_id"])){
  80. $wherein[] = ['customer_id' , '=', $where["customer_id"]];
  81. }
  82. $dbResult = $this->obj->select($wherein, 'a.*,b.name as user_name,c.name as card_name,c.image', 'id DESC', $limit, $offset, array(), true, false,$join);
  83. $count = $this->obj->count($wherein, [], $join);
  84. if ($dbResult === false) {
  85. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  86. }
  87. $return = [
  88. 'data' => $dbResult,
  89. 'total' => $count,
  90. ];
  91. if($return === false){
  92. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  93. }else{
  94. return ResultWrapper::success($return);
  95. }
  96. }
  97. /**
  98. * 详情
  99. * @param $where
  100. * @return mixed
  101. */
  102. public function details($where)
  103. {
  104. if(empty($where)){
  105. return ResultWrapper::success($where);
  106. }
  107. $dbResult = $this->obj->get($where);
  108. if($dbResult === false){
  109. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  110. }
  111. return ResultWrapper::success(self::formatInfo($dbResult));
  112. }
  113. /**
  114. * Doc: (des="添加")
  115. * User: XMing
  116. * Date: 2020/7/15
  117. * Time: 10:42 上午
  118. * @throws Exception
  119. * @param array $params
  120. * @return ResultWrapper
  121. */
  122. public function insert(array $params)
  123. {
  124. $res = $this->obj->insert($params);
  125. if ($res){
  126. return ResultWrapper::success('添加成功');
  127. }else{
  128. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  129. }
  130. }
  131. public function update($params, $id)
  132. {
  133. $data = $this->obj->get($id);
  134. if (!$data){
  135. return ResultWrapper::fail('数据不存在', ErrorCode::$dberror);
  136. }
  137. $dbResult = $this->obj->update($params,$id);
  138. if($dbResult === false){
  139. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  140. }
  141. return ResultWrapper::success('修改成功');
  142. }
  143. /**
  144. * 完成订单发放优惠卡
  145. * @param $data
  146. * @return void
  147. */
  148. public function complete($data)
  149. {
  150. $this->obj->beginTransaction();
  151. $card = $this->DCardBind->select(['card_id' => $data['card_id']]);
  152. $res = true;
  153. if ($card) {
  154. $cardRes = $this->DCustomerCard->insert(['customer_id' => $data['customer_id'], 'enterprise_id' => $data['enterprise_id'], 'card_id' => $data['card_id']]);
  155. foreach ($card as $item)
  156. {
  157. $user_card = [
  158. 'customer_card' => $cardRes,
  159. 'enterprise_id' => $data['enterprise_id'],
  160. 'customer_id' => $data['customer_id'],
  161. 'project_id' => $item['project_id'],
  162. 'number' => $item['number']
  163. ];
  164. $res = $this->DCustomerCardNum->insert($user_card);
  165. }
  166. $res = $this->obj->update(['paid' => 1], $data['id']);
  167. }
  168. if ($res){
  169. $this->obj->commit();
  170. return true;
  171. }else{
  172. $this->obj->rollBack();
  173. return false;
  174. }
  175. }
  176. /**
  177. * 格式数据详情
  178. * @param $data
  179. * @return mixed
  180. */
  181. public function formatInfo($data)
  182. {
  183. return $data;
  184. }
  185. public function delete($where)
  186. {
  187. $res = $this->obj->delete($where);
  188. if ($res){
  189. return ResultWrapper::success('删除成功');
  190. }
  191. return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
  192. }
  193. /**
  194. * 微信支付
  195. * @param $params
  196. * @return mixed
  197. * @throws Exception
  198. */
  199. public function wxPay($params, $ip, $source)
  200. {
  201. $objSystemCache = new SystemCache();
  202. $objMPaymentSetting = new MPaymentSetting($this->userCenterId, $this->enterpriseId);
  203. $result = $objMPaymentSetting->getPayData(1);
  204. if (!$result->isSuccess() || empty($result->getData())) {
  205. return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor);
  206. }
  207. $payment = $result->getData();
  208. //获取客户信息
  209. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  210. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  211. if (!$customerData->isSuccess()) {
  212. return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
  213. }
  214. $customerData = $customerData->getData();
  215. //wx
  216. /********************生成小程序唤起支付用到的参数(原配置)*****/
  217. /**$weixinConfigData = Factory::config()->get('weixin');
  218. * if (empty($weixinConfigData)) {
  219. * return ResultWrapper::fail('微信配置错误', ErrorCode::$configEroor);
  220. * }*/
  221. // 获取当前登录企业的小程序配置
  222. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->enterpriseId);
  223. if (empty($miniprogramSetting)) {
  224. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  225. }
  226. $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid
  227. // 获取配置文件微信公众号
  228. /**$weixinConfigData = Factory::config()->get('weixin');
  229. * if (empty($weixinConfigData)) {
  230. * return ResultWrapper::fail('微信配置错误', ErrorCode::$configEroor);
  231. * }*/
  232. switch ($source) {
  233. case StatusCode::$source['ByteProgram'];
  234. //$appid = $weixinConfigData['offiaccount']['appid'];
  235. $appid = $miniprogramSetting['appid'];
  236. $paySource = 'byteDanceH5';
  237. break;
  238. case StatusCode::$source['miniProgram'];
  239. $appid = $miniprogramSetting['appid'];
  240. $paySource = 'miniProgram';
  241. break;
  242. default :
  243. $appid = $payment['mobileAppid'];
  244. $paySource = 'APP';
  245. break;
  246. }
  247. $weixinConfigData = [
  248. 'mch_id' => $payment['mchId'],
  249. 'apiPartnerKey' => $payment['apiPartnerKey'],
  250. 'appid' => $appid
  251. ];
  252. $attach = $this->enterpriseId . '|' . $this->userCenterId . '|' . implode('|', [$params['order_id']]);
  253. //$objPay = new Pay($appid, $weixinConfigData['pay']['mch_id'], $weixinConfigData['pay']['apiPartnerKey']);//old
  254. $objPay = new Pay($appid, $weixinConfigData['mch_id'], $weixinConfigData['apiPartnerKey']);
  255. $payParams = $objPay->unifiedorder($params['order_id'],$params['pay_price'], $ip, $paySource, '千鸟云商', $customerData['openId'], $attach);
  256. return ResultWrapper::success($payParams->getData());
  257. }
  258. public function yuePay($param)
  259. {
  260. //获取客户信息
  261. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  262. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  263. if (!$customerData->isSuccess()) {
  264. return ResultWrapper::fail($customerData->getData(), $customerData->getErrorCode());
  265. }
  266. $customerData = $customerData->getData();
  267. $price = $param['pay_price'];// 实际支付
  268. if ($customerData['memberBalance'] < $price){
  269. return ResultWrapper::fail('余额不足', 1005);
  270. }
  271. $before = $customerData['memberBalance'];
  272. $after = $customerData['memberBalance'] - $price;
  273. $Mbalance = new MMemberBalanceDetail($this->enterpriseId, $this->userCenterId);
  274. $Mbalance->create($customerData['id'], $customerData['userCenterId'], 4, $before, $price, $after,'支付优惠卡订单', '余额支付优惠卡订单');
  275. $res = $this->paySuccess($param['order_id']);
  276. if ($res){
  277. return ResultWrapper::success('支付成功');
  278. }else{
  279. return ResultWrapper::fail('支付失败', 1005);
  280. }
  281. }
  282. /**
  283. * 支付成功后
  284. * @return void
  285. */
  286. public function paySuccess($order_id)
  287. {
  288. $data = $this->obj->get(['order_id' => $order_id]);
  289. $this->obj->beginTransaction();
  290. $card = $this->DCardBind->select(['card_id' => $data['card_id']]);
  291. $res = true;
  292. if ($card) {
  293. $cardRes = $this->DCustomerCard->insert(['customer_id' => $data['customer_id'], 'enterprise_id' => $data['enterprise_id'], 'card_id' => $data['card_id']]);
  294. foreach ($card as $item)
  295. {
  296. $user_card = [
  297. 'customer_card' => $cardRes,
  298. 'enterprise_id' => $data['enterprise_id'],
  299. 'customer_id' => $data['customer_id'],
  300. 'project_id' => $item['project_id'],
  301. 'number' => $item['number']
  302. ];
  303. $this->DCustomerCardNum->insert($user_card);
  304. }
  305. $res = $this->obj->update(['paid' => 1], $data['id']);
  306. }
  307. if ($res){
  308. $this->obj->commit();
  309. //服务优惠卡分销插入任务
  310. //只有小程序微信支付订单才可以计算分佣
  311. $customerData = $this->objDCustomer->get($data["customer_id"]);
  312. if(!empty($customerData) && !empty($data) && $data["pay_price"]>0 && $data["payType"]==2){
  313. $checkData = [
  314. "parentPath"=>$customerData["parentPath"],
  315. "payAmount"=>$data["pay_price"],
  316. "type"=>"shopCardOrder",
  317. ];
  318. // PartnerTools::addTask($data["id"], $data["enterprise_id"],$checkData);
  319. }
  320. return true;
  321. }else{
  322. $this->obj->rollBack();
  323. return false;
  324. }
  325. }
  326. }