Storejoinin.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * 店铺入住模型
  4. */
  5. namespace app\common\model;
  6. use think\facade\Db;
  7. /**
  8. *
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * 数据层模型
  13. */
  14. class Storejoinin extends BaseModel
  15. {
  16. public $page_info;
  17. /**
  18. * 读取列表
  19. * @access public
  20. * @author csdeshang
  21. * @param array $condition 条件
  22. * @param int $pagesize 分页
  23. * @param string $order 排序
  24. * @param string $field 字段
  25. * @return array
  26. */
  27. public function getStorejoininList($condition, $pagesize = '', $order = '', $field = '*')
  28. {
  29. if ($pagesize) {
  30. $result = Db::name('storejoinin')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  31. $this->page_info = $result;
  32. return $result->items();
  33. } else {
  34. $result = Db::name('storejoinin')->field($field)->where($condition)->order($order)->select()->toArray();
  35. return $result;
  36. }
  37. }
  38. /**
  39. * 店铺入住数量
  40. * @access public
  41. * @author csdeshang
  42. * @param array $condition 条件
  43. * @return int
  44. */
  45. public function getStorejoininCount($condition)
  46. {
  47. return Db::name('storejoinin')->where($condition)->count();
  48. }
  49. /**
  50. * 读取单条记录
  51. * @access public
  52. * @author csdeshang
  53. * @param array $condition 条件
  54. * @return array
  55. */
  56. public function getOneStorejoinin($condition)
  57. {
  58. $result = Db::name('storejoinin')->where($condition)->find();
  59. return $result;
  60. }
  61. /**
  62. * 判断是否存在
  63. * @access public
  64. * @author csdeshang
  65. * @param type $condition 条件
  66. * @return boolean
  67. */
  68. public function isStorejoininExist($condition)
  69. {
  70. $result = $this->getOneStorejoinin($condition);
  71. if (empty($result)) {
  72. return FALSE;
  73. } else {
  74. return TRUE;
  75. }
  76. }
  77. /**
  78. * 增加
  79. * @access public
  80. * @author csdeshang
  81. * @param type $data 数据
  82. * @return type
  83. */
  84. public function addStorejoinin($data)
  85. {
  86. return Db::name('storejoinin')->insert($data);
  87. }
  88. /**
  89. * 更新
  90. * @access public
  91. * @author csdeshang
  92. * @param type $update 数据
  93. * @param type $condition 条件
  94. * @return type
  95. */
  96. public function editStorejoinin($update, $condition)
  97. {
  98. return Db::name('storejoinin')->where($condition)->update($update);
  99. }
  100. /**
  101. * 删除
  102. * @access public
  103. * @author csdeshang
  104. * @param type $condition 条件
  105. * @return type
  106. */
  107. public function delStorejoinin($condition)
  108. {
  109. return Db::name('storejoinin')->where($condition)->delete();
  110. }
  111. /**
  112. * 充值卡支付
  113. * 如果充值卡足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  114. */
  115. public function rcbPay($order_info, $input, $buyer_info)
  116. {
  117. $available_rcb_amount = floatval($buyer_info['available_rc_balance']);
  118. if ($available_rcb_amount <= 0)
  119. return $order_info;
  120. $predeposit_model = model('predeposit');
  121. $order_amount = round($order_info['paying_amount'] - $order_info['rcb_amount'] - $order_info['pd_amount'], 2);
  122. $data_pd = array();
  123. $data_pd['member_id'] = $buyer_info['member_id'];
  124. $data_pd['member_name'] = $buyer_info['member_name'];
  125. $data_pd['amount'] = $order_amount;
  126. $data_pd['order_sn'] = $order_info['pay_sn'];
  127. if ($available_rcb_amount >= $order_amount) {
  128. // 预存款立即支付,订单支付完成
  129. $predeposit_model->changeRcb('storejoinin_pay', $data_pd);
  130. $available_rcb_amount -= $order_amount;
  131. //支付被冻结的充值卡
  132. $rcb_amount = isset($order_info['rcb_amount']) ? floatval($order_info['rcb_amount']) : 0;
  133. if ($rcb_amount > 0) {
  134. $data_pd = array();
  135. $data_pd['member_id'] = $buyer_info['member_id'];
  136. $data_pd['member_name'] = $buyer_info['member_name'];
  137. $data_pd['amount'] = $rcb_amount;
  138. $data_pd['order_sn'] = $order_info['pay_sn'];
  139. $predeposit_model->changeRcb('storejoinin_comb_pay', $data_pd);
  140. }
  141. $store_model = model('store');
  142. $store_model->setStoreOpen($order_info, array('joinin_state' => STORE_JOIN_STATE_FINAL, 'payment_code' => 'predeposit'));
  143. // 订单状态 置为已支付
  144. $order_info['rcb_amount'] = round($order_info['rcb_amount'] + $order_amount, 2);
  145. } else {
  146. //暂冻结预存款,后面还需要 API彻底完成支付
  147. $data_pd['amount'] = $available_rcb_amount;
  148. $predeposit_model->changeRcb('storejoinin_freeze', $data_pd);
  149. //预存款支付金额保存到订单
  150. $data_order = array();
  151. $order_info['rcb_amount'] = $data_order['rcb_amount'] = round($order_info['rcb_amount'] + $available_rcb_amount, 2);
  152. $result = $this->editStorejoinin($data_order, array('member_id' => $order_info['member_id']));
  153. if (!$result) {
  154. throw new \think\Exception('订单更新失败', 10006);
  155. }
  156. }
  157. return $order_info;
  158. }
  159. /**
  160. * 预存款支付 主要处理
  161. * 如果预存款足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  162. */
  163. public function pdPay($order_info, $input, $buyer_info)
  164. {
  165. if ($order_info['joinin_state'] == STORE_JOIN_STATE_FINAL)
  166. return $order_info;
  167. $available_pd_amount = floatval($buyer_info['available_predeposit']);
  168. if ($available_pd_amount <= 0)
  169. return $order_info;
  170. $predeposit_model = model('predeposit');
  171. $order_amount = round($order_info['paying_amount'] - $order_info['rcb_amount'] - $order_info['pd_amount'], 2);
  172. $data_pd = array();
  173. $data_pd['member_id'] = $buyer_info['member_id'];
  174. $data_pd['member_name'] = $buyer_info['member_name'];
  175. $data_pd['amount'] = $order_amount;
  176. $data_pd['order_sn'] = $order_info['pay_sn'];
  177. if ($available_pd_amount >= $order_amount) {
  178. //预存款立即支付,订单支付完成
  179. $predeposit_model->changePd('storejoinin_pay', $data_pd);
  180. $available_pd_amount -= $order_amount;
  181. //下单,支付被冻结的充值卡
  182. $pd_amount = floatval($order_info['rcb_amount']);
  183. if ($pd_amount > 0) {
  184. $data_pd = array();
  185. $data_pd['member_id'] = $buyer_info['member_id'];
  186. $data_pd['member_name'] = $buyer_info['member_name'];
  187. $data_pd['amount'] = $pd_amount;
  188. $data_pd['order_sn'] = $order_info['pay_sn'];
  189. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  190. }
  191. //支付被冻结的预存款
  192. $pd_amount = isset($order_info['pd_amount']) ? floatval($order_info['pd_amount']) : 0;
  193. if ($pd_amount > 0) {
  194. $data_pd = array();
  195. $data_pd['member_id'] = $buyer_info['member_id'];
  196. $data_pd['member_name'] = $buyer_info['member_name'];
  197. $data_pd['amount'] = $pd_amount;
  198. $data_pd['order_sn'] = $order_info['pay_sn'];
  199. $predeposit_model->changePd('storejoinin_comb_pay', $data_pd);
  200. }
  201. $store_model = model('store');
  202. $store_model->setStoreOpen($order_info, array('joinin_state' => STORE_JOIN_STATE_FINAL, 'payment_code' => 'predeposit'));
  203. // 订单状态 置为已支付
  204. $order_info['pd_amount'] = round($order_info['pd_amount'] + $order_amount, 2);
  205. } else {
  206. //暂冻结预存款,后面还需要 API彻底完成支付
  207. $data_pd['amount'] = $available_pd_amount;
  208. $predeposit_model->changePd('storejoinin_freeze', $data_pd);
  209. //预存款支付金额保存到订单
  210. $data_order = array();
  211. $order_info['pd_amount'] = $data_order['pd_amount'] = round($order_info['pd_amount'] + $available_pd_amount, 2);
  212. $result = $this->editStorejoinin($data_order, array('member_id' => $order_info['member_id']));
  213. if (!$result) {
  214. throw new \think\Exception('订单更新失败', 10006);
  215. }
  216. }
  217. return $order_info;
  218. }
  219. }