DeliveryStationRepository.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\delivery;
  12. use app\common\dao\delivery\DeliveryStationDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\system\config\ConfigClassifyRepository;
  15. use app\common\repositories\system\config\ConfigValueRepository;
  16. use crmeb\services\DeliverySevices;
  17. use FormBuilder\Factory\Elm;
  18. use think\Exception;
  19. use think\exception\ValidateException;
  20. use think\facade\Cache;
  21. use think\facade\Db;
  22. use think\facade\Route;
  23. class DeliveryStationRepository extends BaseRepository
  24. {
  25. public function __construct(DeliveryStationDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. public function deliveryForm()
  30. {
  31. $formData = systemConfig([
  32. 'delivery_status',
  33. 'uupt_appkey',
  34. 'uupt_app_id',
  35. 'uupt_open_id',
  36. 'delivery_type',
  37. 'dada_app_key',
  38. 'dada_app_sercret',
  39. 'dada_source_id',
  40. ]);
  41. $form = Elm::createForm(Route::buildUrl('systemDeliveryConfigSave')->build());
  42. $form->setRule([
  43. Elm::switches('delivery_status', '是否开启同城配送', $formData['delivery_status'])->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  44. Elm::radio('delivery_type', '配送类型', $formData['delivery_type'])
  45. ->setOptions([
  46. ['value' => DeliverySevices::DELIVERY_TYPE_DADA, 'label' => '达达快送'],
  47. ['value' => DeliverySevices::DELIVERY_TYPE_UU, 'label' => 'UU跑腿'],
  48. ])->control([
  49. [
  50. 'value' => DeliverySevices::DELIVERY_TYPE_DADA,
  51. 'rule' => [
  52. Elm::input('dada_app_key', 'AppKey (达达)')->value($formData['dada_app_key'])->required(),
  53. Elm::input('dada_app_sercret', 'AppSercret (达达)')->value($formData['dada_app_sercret'])->required(),
  54. Elm::input('dada_source_id', '商户ID (达达)')->value($formData['dada_source_id'])->required(),
  55. ]
  56. ],
  57. [
  58. 'value' => DeliverySevices::DELIVERY_TYPE_UU,
  59. 'rule' => [
  60. Elm::input('uupt_appkey', 'AppKey (UU跑腿)')->value($formData['uupt_appkey'])->required(),
  61. Elm::input('uupt_app_id', 'AppId (UU跑腿)')->value($formData['uupt_app_id'])->required(),
  62. Elm::input('uupt_open_id', 'OpenId (UU跑腿)')->value($formData['uupt_open_id'])->required(),
  63. ]
  64. ],
  65. ]),
  66. ]);
  67. return $form->setTitle('同城配送设置');
  68. }
  69. public function getBusiness()
  70. {
  71. $type = systemConfig('delivery_type');
  72. return DeliverySevices::create($type)->getBusiness();
  73. }
  74. /**
  75. * TODO 创建门店
  76. * @param array $data
  77. * @return mixed
  78. * @author Qinii
  79. * @day 2/14/22
  80. */
  81. public function save(array $data)
  82. {
  83. return Db::transaction(function () use($data){
  84. $data['origin_shop_id'] = 'Deliver'.$data['mer_id'].'_'.$this->getSn();
  85. DeliverySevices::create(systemConfig('delivery_type'))->addShop([$data]);
  86. return $this->dao->create($data);
  87. });
  88. }
  89. public function getSn()
  90. {
  91. list($msec, $sec) = explode(' ', microtime());
  92. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  93. $orderId = $msectime . random_int(10000, max(intval($msec * 10000) + 10000, 98369));
  94. return $orderId;
  95. }
  96. /**
  97. * TODO 更新门店
  98. * @param $id
  99. * @param $data
  100. * @return mixed
  101. * @author Qinii
  102. * @day 2/14/22
  103. */
  104. public function edit($id, $merId, $data)
  105. {
  106. $res = $this->dao->getSearch([$this->dao->getPk() => $id, 'mer_id' => $merId])->find();
  107. if (!$res) throw new ValidateException('门店不存在或不属于您');
  108. $type = systemConfig('delivery_type');
  109. $data['origin_shop_id'] = $res['origin_shop_id'];
  110. return Db::transaction(function () use($id, $type, $data, $res){
  111. if ($res['type'] == 2 && $data['type'] == 1) {
  112. DeliverySevices::create($type)->addShop($data);
  113. } else {
  114. DeliverySevices::create($type)->updateShop($data);
  115. }
  116. return $this->dao->update($id, $data);
  117. });
  118. }
  119. /**
  120. * TODO
  121. * @param array $where
  122. * @param int $page
  123. * @param int $limit
  124. * @return array
  125. * @author Qinii
  126. * @day 2/17/22
  127. */
  128. public function merList(array $where, int $page, int $limit)
  129. {
  130. $query = $this->dao->getSearch($where);
  131. $count = $query->count();
  132. $list = $query->page($page, $limit)->order('create_time DESC')->select();
  133. return compact('count', 'list');
  134. }
  135. public function sysList(array $where, int $page, int $limit)
  136. {
  137. $query = $this->dao->getSearch($where)->with([
  138. 'merchant' => function($query) {
  139. $query->field('mer_id,mer_name');
  140. }
  141. ]);
  142. $count = $query->count();
  143. $list = $query->page($page, $limit)->order('create_time DESC')->select();
  144. return compact('count', 'list');
  145. }
  146. public function detail(int $id, ?int $merId)
  147. {
  148. $where[$this->dao->getPk()] = $id;
  149. if ($merId) $where['mer_id'] = $merId;
  150. $res = $this->dao->getSearch($where)->with([
  151. 'merchant' => function($query) {
  152. $query->field('mer_id,mer_name');
  153. }
  154. ])->find();
  155. if (!$res) throw new ValidateException('门店不存在');
  156. return $res;
  157. }
  158. public function destory($id, $merId)
  159. {
  160. $where = [
  161. $this->dao->getPk() => $id,
  162. 'mer_id' => $merId,
  163. ];
  164. $res = $this->dao->getSearch($where)->find();
  165. if (!$res) throw new ValidateException('数据不存在');
  166. // $data = [
  167. // 'origin_shop_id' => $res['origin_shop_id'],
  168. // 'status' => 0,
  169. // ];
  170. // if ($res['type'] == DeliverySevices::DELIVERY_TYPE_DADA) {
  171. // try{
  172. // DeliverySevices::create($res['type'])->updateShop($data);
  173. // }catch (\Exception $exception) {
  174. // }
  175. // }
  176. return $this->dao->delete($id);
  177. }
  178. public function markForm($id, $merId)
  179. {
  180. $where = [
  181. $this->dao->getPk() => $id,
  182. 'mer_id' => $merId,
  183. ];
  184. $formData = $this->dao->getWhere($where);
  185. $form = Elm::createForm(Route::buildUrl('merchantStoreDeliveryMark',['id' => $id])->build());
  186. $form->setRule([
  187. Elm::text('mark', '备注', $formData['mark']),
  188. ]);
  189. return $form->setTitle('备注');
  190. }
  191. public function getOptions($where)
  192. {
  193. return $this->dao->getSearch($where)->field('station_id value, station_name label')->order('create_time DESC')->select();
  194. }
  195. public function getCityLst()
  196. {
  197. $type = systemConfig('delivery_type');
  198. $key = 'delivery_get_city_lst_'.$type;
  199. if (!$data = Cache::get($key)) {
  200. $data = DeliverySevices::create($type)->getCity([]);
  201. Cache::set($key, $data,3600);
  202. }
  203. return $data;
  204. }
  205. public function getBalance()
  206. {
  207. $type = systemConfig('delivery_type');
  208. if (!$type) return ['deliverBalance' => 0];
  209. return DeliverySevices::create(systemConfig('delivery_type'))->getBalance([]);
  210. }
  211. public function getRecharge()
  212. {
  213. return DeliverySevices::create(systemConfig('delivery_type'))->getRecharge([]);
  214. }
  215. }