ServiceLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\logic;
  15. use app\common\cache\WebScanLoginCache;
  16. use app\common\logic\BaseLogic;
  17. use app\api\service\{UserTokenService, WechatUserService};
  18. use app\common\enum\{LoginEnum, PayEnum, user\UserTerminalEnum, YesNoEnum};
  19. use app\common\service\{
  20. ConfigService,
  21. FileService,
  22. wechat\WeChatConfigService,
  23. wechat\WeChatMnpService,
  24. wechat\WeChatOaService,
  25. wechat\WeChatRequestService
  26. };
  27. use app\common\model\agricultural_machinery\UserService;
  28. use app\common\model\agricultural_machinery\ServiceCategory;
  29. use app\common\model\agricultural_machinery\ServiceScanLog;
  30. use app\common\model\ServiceCharge;
  31. use app\common\model\asset\AssetArea;
  32. use app\common\model\recharge\RechargeOrder;
  33. use app\common\model\user\{User, UserAuth};
  34. use think\facade\{Db, Config};
  35. /**
  36. * 服务商逻辑
  37. * Class LoginLogic
  38. * @package app\api\logic
  39. */
  40. class ServiceLogic extends BaseLogic
  41. {
  42. /**
  43. * @notes 入驻
  44. * @param array $params
  45. * @return bool
  46. * @author 段誉
  47. * @date 2022/9/7 15:37
  48. */
  49. public static function Settled(array $params)
  50. {
  51. Db::startTrans();
  52. try {
  53. $service_fee = ServiceCharge::find(1);
  54. $order_money = 0;
  55. $flag = false ;
  56. switch ($params['type']){
  57. case 1 :
  58. $flag = true ;
  59. $order_money =$service_fee['agricultural_machinery_service_fee'];
  60. break;
  61. case 2 :
  62. $flag = true ;
  63. $order_money =$service_fee['bake_service_fee'];
  64. break;
  65. case 3 :
  66. $flag = true ;
  67. $order_money =$service_fee['air_control_service_fee'];
  68. break;
  69. }
  70. $result = UserService::create([
  71. 'user_id' => $params['user_id'],
  72. 'type' => $params['type'] ?? 1,
  73. 'name' => $params['name'],
  74. 'mobile' => $params['mobile'],
  75. 'agricultural_machinery_model' =>$params['agricultural_machinery_model'],
  76. 'images' => $params['images'],
  77. 'driving_image' => $params['driving_image'],
  78. 'driver_image' => $params['driver_image'],
  79. 'agricultural_image' => $params['agricultural_image'],
  80. 'cate_id' => ','.$params['cate_id'].',',
  81. 'area_id' => ','.$params['area_id'].',',
  82. 'money' => $params['money'],
  83. 'content'=>$params['content']??'',
  84. 'order_id'=>0,
  85. ]);
  86. if($result){
  87. $data = [
  88. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  89. 'user_id' => $params['user_id'],
  90. 'service_id'=>$result['id'],
  91. 'pay_status' => PayEnum::UNPAID,
  92. 'order_amount' => $order_money,
  93. ];
  94. $order = RechargeOrder::create($data);
  95. $user_service_info = UserService::find($result['id']);
  96. $user_service_info->order_id = $order['id'];
  97. $user_service_info->save();
  98. if($flag){
  99. switch ($params['type']){
  100. case 1 :
  101. $updateData['agricultural_id'] =(int)$order['id'];
  102. $updateData['agricultural_status'] =1;
  103. break;
  104. case 2 :
  105. $updateData['bake_id'] =(int)$order['id'];
  106. $updateData['bake_status'] =1;
  107. break;
  108. case 3 :
  109. $updateData['air_id'] =(int)$order['id'];
  110. $updateData['air_status'] =1;
  111. break;
  112. }
  113. $updateData['id'] = $params['user_id'];
  114. User::update($updateData);
  115. }
  116. $order_info = [
  117. 'order_id' => (int)$order['id'],
  118. 'order_sn' => $order['sn'],
  119. 'order_money'=>$order_money,
  120. 'from' => 'service',
  121. 'result'=>$result
  122. ];
  123. }
  124. Db::commit();
  125. return ['code'=>1,'data'=>$order_info];
  126. } catch (\Exception $e) {
  127. Db::rollback();
  128. self::setError($e->getMessage());
  129. return ['code'=>0,'data'=>[]];
  130. }
  131. }
  132. /**
  133. * @notes
  134. * @param $params
  135. * @return array|false
  136. * @author 段誉
  137. * @date 2022/9/6 19:26
  138. */
  139. public static function getInfo($params,$user_id)
  140. {
  141. try {
  142. $where = [];
  143. $where[]=['user_id','=',$user_id];
  144. $where[]=['type','=',$params['type']];
  145. $serviceInfo = UserService::where($where)->append(['type_desc','cate_desc','area_desc','images','user','orderInfo'])->findOrEmpty();
  146. if($serviceInfo->isEmpty()){
  147. $status = -1;
  148. }else{
  149. if($serviceInfo['status'] == 0){
  150. $status = 0;
  151. }else if($serviceInfo['status'] == 2){
  152. $status = 2;
  153. }else{
  154. $status = 1;
  155. }
  156. }
  157. return [
  158. 'status' => $status,
  159. 'info' => $serviceInfo,
  160. ];
  161. } catch (\Exception $e) {
  162. self::setError($e->getMessage());
  163. return false;
  164. }
  165. }
  166. /**
  167. * @notes 地址列表
  168. * @author 段誉
  169. * @date 2022/9/16 17:56
  170. */
  171. public static function getsAreaLists($get)
  172. {
  173. $where=[];
  174. $where[]=['status','=',1];
  175. if(isset($get['title'])){
  176. $where[]=['title','like','%'.$get['title'].'%'];
  177. }
  178. $area_list = AssetArea::where($where)->field('id,id value,level,pid,title,title label')->order('sort desc,id asc')->select()->toArray();
  179. if($get['is_tree'] ==1){
  180. $treeList = linear_to_tree($area_list, 'children');
  181. if (empty($treeList) && !empty($area_list)) {
  182. foreach ($area_list as &$v) {
  183. $v['children'] = [];
  184. }
  185. return $area_list;
  186. }
  187. return $treeList;
  188. }else{
  189. return $area_list;
  190. }
  191. }
  192. /**
  193. * @notes 地址列表
  194. * @author 段誉
  195. * @date 2022/9/16 17:56
  196. */
  197. public static function getsCateLists($get)
  198. {
  199. $where=[];
  200. $where[]=['type','=',$get['type']];
  201. $where[]=['status','=',1];
  202. if(isset($get['name'])){
  203. $where[]=['name','like','%'.$get['name'].'%'];
  204. }
  205. $cate_list = ServiceCategory::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
  206. return $cate_list;
  207. }
  208. /**
  209. * @notes 续费订单
  210. * @param array $params
  211. * @return bool
  212. * @author 段誉
  213. * @date 2022/9/7 15:37
  214. */
  215. public static function Renew(array $params)
  216. {
  217. Db::startTrans();
  218. try {
  219. $where['user_id'] = $params['user_id'];
  220. $where['type'] = $params['type'];
  221. $info = UserService::where($where)->findOrEmpty();
  222. $order_info = [];
  223. if($info){
  224. if($info['expiration_time'] > time()){
  225. Db::rollback();
  226. self::setError('服务未到期,无需进行续费操作');
  227. return ['code'=>0,'data'=>[]];
  228. }
  229. $service_fee = ServiceCharge::find(1);
  230. $order_money = 0;
  231. switch ($params['type']){
  232. case 1 :
  233. $order_money =$service_fee['agricultural_machinery_service_fee'];
  234. break;
  235. case 2 :
  236. $order_money =$service_fee['bake_service_fee'];
  237. break;
  238. case 3 :
  239. $order_money =$service_fee['air_control_service_fee'];
  240. break;
  241. }
  242. $data = [
  243. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  244. 'user_id' => $params['user_id'],
  245. 'service_id'=>$info['id'],
  246. 'pay_status' => PayEnum::UNPAID,
  247. 'order_amount' => $order_money,
  248. ];
  249. $order = RechargeOrder::create($data);
  250. $info->order_id = $order['id'];
  251. $info->status = 0;
  252. $info->save();
  253. $order_info = [
  254. 'order_id' => (int)$order['id'],
  255. 'order_sn' => $order['sn'],
  256. 'from' => 'service',
  257. ];
  258. }
  259. Db::commit();
  260. return ['code'=>1,'data'=>$order_info];
  261. } catch (\Exception $e) {
  262. Db::rollback();
  263. self::setError($e->getMessage());
  264. return ['code'=>0,'data'=>[]];
  265. }
  266. }
  267. public static function updateServiceStatus(){
  268. $where = [];
  269. $where[]=['status','=' ,1];
  270. $where[]=['is_auto_upd','=', 0];
  271. $where[]=['expiration_time','<', time()];
  272. $list = UserService::where($where)->select()->toArray();
  273. $remark = '';
  274. foreach($list as $k=>$v){
  275. $updatewhere['id'] = $v['id'];
  276. $updateData['status'] = 2;
  277. $updateData['is_auto_upd'] = 1;
  278. UserService::where($updatewhere)->update($updateData);
  279. $remark .='id='.$v['id'].'~已到期,状态更新~';
  280. }
  281. if(empty($remark)){
  282. $remark = '无更新数据';
  283. }
  284. return $remark;
  285. }
  286. public static function getServiceContentInfo(){
  287. $where['id'] = 1;
  288. $info = ServiceCharge::field('agricultural_service,bake_service,air_service')->find(1)->toArray();
  289. return $info;
  290. }
  291. public static function getUserServiceInfo($user_id){
  292. $info = User::field('id,nickname,mobile,agricultural_status,agricultural_id,bake_status,bake_id,air_status,air_id')
  293. ->with(['agricultural','bake','air'])->find($user_id)->toArray();
  294. return $info;
  295. }
  296. public static function getServiceDisclaimer(){
  297. $data['disclaimer'] = ConfigService::get('website', 'disclaimer');
  298. return $data;
  299. }
  300. /**
  301. * @notes 插入浏览日志
  302. * @param array $params
  303. * @return bool
  304. * @author 段誉
  305. * @date 2022/9/7 15:37
  306. */
  307. public static function addLog(array $params)
  308. {
  309. try {
  310. $data = [
  311. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  312. 'user_id' => $params['user_id'],
  313. 'type' => $params['log_type'],
  314. 'service_user_id' => $params['service_user_id']??0,
  315. ];
  316. ServiceScanLog::create($data);
  317. return true;
  318. } catch (\Exception $e) {
  319. self::setError($e->getMessage());
  320. return false;
  321. }
  322. }
  323. }