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' => $order_money,
  83. 'content'=>$params['content'],
  84. 'order_id'=>0,
  85. ]);
  86. Db::commit();
  87. return ['code'=>1,'data'=>$result];
  88. if($result){
  89. $data = [
  90. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  91. 'user_id' => $params['user_id'],
  92. 'service_id'=>$result['id'],
  93. 'pay_status' => PayEnum::UNPAID,
  94. 'order_amount' => $order_money,
  95. ];
  96. $order = RechargeOrder::create($data);
  97. $user_service_info = UserService::find($result['id']);
  98. $user_service_info->order_id = $order['id'];
  99. $user_service_info->save();
  100. if($flag){
  101. switch ($params['type']){
  102. case 1 :
  103. $updateData['agricultural_id'] =(int)$order['id'];
  104. $updateData['agricultural_status'] =1;
  105. break;
  106. case 2 :
  107. $updateData['bake_id'] =(int)$order['id'];
  108. $updateData['bake_status'] =1;
  109. break;
  110. case 3 :
  111. $updateData['air_id'] =(int)$order['id'];
  112. $updateData['air_status'] =1;
  113. break;
  114. }
  115. $ret = User::where(['id'=> $params['user_id']])->update($updateData);
  116. }
  117. $order_info = [
  118. 'order_id' => (int)$order['id'],
  119. 'order_sn' => $order['sn'],
  120. 'order_money'=>$order_money,
  121. 'from' => 'service',
  122. 'result'=>$result
  123. ];
  124. }
  125. Db::commit();
  126. return ['code'=>1,'data'=>$order_info];
  127. } catch (\Exception $e) {
  128. Db::rollback();
  129. self::setError($e->getMessage());
  130. return ['code'=>0,'data'=>[]];
  131. }
  132. }
  133. /**
  134. * @notes
  135. * @param $params
  136. * @return array|false
  137. * @author 段誉
  138. * @date 2022/9/6 19:26
  139. */
  140. public static function getInfo($params,$user_id)
  141. {
  142. try {
  143. $where = [];
  144. $where[]=['user_id','=',$user_id];
  145. $where[]=['type','=',$params['type']];
  146. $serviceInfo = UserService::where($where)->append(['type_desc','cate_desc','area_desc','images','user','orderInfo'])->findOrEmpty();
  147. if($serviceInfo->isEmpty()){
  148. $status = -1;
  149. }else{
  150. if($serviceInfo['status'] == 0){
  151. $status = 0;
  152. }else if($serviceInfo['status'] == 2){
  153. $status = 2;
  154. }else{
  155. $status = 1;
  156. }
  157. }
  158. return [
  159. 'status' => $status,
  160. 'info' => $serviceInfo,
  161. ];
  162. } catch (\Exception $e) {
  163. self::setError($e->getMessage());
  164. return false;
  165. }
  166. }
  167. /**
  168. * @notes 地址列表
  169. * @author 段誉
  170. * @date 2022/9/16 17:56
  171. */
  172. public static function getsAreaLists($get)
  173. {
  174. $where=[];
  175. $where[]=['status','=',1];
  176. if(isset($get['title'])){
  177. $where[]=['title','like','%'.$get['title'].'%'];
  178. }
  179. $area_list = AssetArea::where($where)->field('id,id value,level,pid,title,title label')->order('sort desc,id asc')->select()->toArray();
  180. if($get['is_tree'] ==1){
  181. $treeList = linear_to_tree($area_list, 'children');
  182. if (empty($treeList) && !empty($area_list)) {
  183. foreach ($area_list as &$v) {
  184. $v['children'] = [];
  185. }
  186. return $area_list;
  187. }
  188. return $treeList;
  189. }else{
  190. return $area_list;
  191. }
  192. }
  193. /**
  194. * @notes 地址列表
  195. * @author 段誉
  196. * @date 2022/9/16 17:56
  197. */
  198. public static function getsCateLists($get)
  199. {
  200. $where=[];
  201. $where[]=['type','=',$get['type']];
  202. $where[]=['status','=',1];
  203. if(isset($get['name'])){
  204. $where[]=['name','like','%'.$get['name'].'%'];
  205. }
  206. $cate_list = ServiceCategory::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
  207. return $cate_list;
  208. }
  209. /**
  210. * @notes 续费订单
  211. * @param array $params
  212. * @return bool
  213. * @author 段誉
  214. * @date 2022/9/7 15:37
  215. */
  216. public static function Renew(array $params)
  217. {
  218. Db::startTrans();
  219. try {
  220. $where['user_id'] = $params['user_id'];
  221. $where['type'] = $params['type'];
  222. $info = UserService::where($where)->findOrEmpty();
  223. $order_info = [];
  224. if($info){
  225. if($info['expiration_time'] > time()){
  226. Db::rollback();
  227. self::setError('服务未到期,无需进行续费操作');
  228. return ['code'=>0,'data'=>[]];
  229. }
  230. $service_fee = ServiceCharge::find(1);
  231. $order_money = 0;
  232. switch ($params['type']){
  233. case 1 :
  234. $order_money =$service_fee['agricultural_machinery_service_fee'];
  235. break;
  236. case 2 :
  237. $order_money =$service_fee['bake_service_fee'];
  238. break;
  239. case 3 :
  240. $order_money =$service_fee['air_control_service_fee'];
  241. break;
  242. }
  243. $data = [
  244. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  245. 'user_id' => $params['user_id'],
  246. 'service_id'=>$info['id'],
  247. 'pay_status' => PayEnum::UNPAID,
  248. 'order_amount' => $order_money,
  249. ];
  250. $order = RechargeOrder::create($data);
  251. $info->order_id = $order['id'];
  252. $info->status = 0;
  253. $info->save();
  254. $order_info = [
  255. 'order_id' => (int)$order['id'],
  256. 'order_sn' => $order['sn'],
  257. 'from' => 'service',
  258. ];
  259. }
  260. Db::commit();
  261. return ['code'=>1,'data'=>$order_info];
  262. } catch (\Exception $e) {
  263. Db::rollback();
  264. self::setError($e->getMessage());
  265. return ['code'=>0,'data'=>[]];
  266. }
  267. }
  268. public static function updateServiceStatus(){
  269. $where = [];
  270. $where[]=['status','=' ,1];
  271. $where[]=['is_auto_upd','=', 0];
  272. $where[]=['expiration_time','<', time()];
  273. $list = UserService::where($where)->select()->toArray();
  274. $remark = '';
  275. foreach($list as $k=>$v){
  276. $updatewhere['id'] = $v['id'];
  277. $updateData['status'] = 2;
  278. $updateData['is_auto_upd'] = 1;
  279. UserService::where($updatewhere)->update($updateData);
  280. $remark .='id='.$v['id'].'~已到期,状态更新~';
  281. }
  282. if(empty($remark)){
  283. $remark = '无更新数据';
  284. }
  285. return $remark;
  286. }
  287. public static function getServiceContentInfo(){
  288. $where['id'] = 1;
  289. $info = ServiceCharge::field('agricultural_service,bake_service,air_service')->find(1)->toArray();
  290. return $info;
  291. }
  292. public static function getUserServiceInfo($user_id){
  293. $info = User::field('id,nickname,mobile,agricultural_status,agricultural_id,bake_status,bake_id,air_status,air_id')
  294. ->with(['agricultural','bake','air'])->find($user_id)->toArray();
  295. return $info;
  296. }
  297. public static function getServiceDisclaimer(){
  298. $data['disclaimer'] = ConfigService::get('website', 'disclaimer');
  299. return $data;
  300. }
  301. /**
  302. * @notes 插入浏览日志
  303. * @param array $params
  304. * @return bool
  305. * @author 段誉
  306. * @date 2022/9/7 15:37
  307. */
  308. public static function addLog(array $params)
  309. {
  310. try {
  311. $data = [
  312. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  313. 'user_id' => $params['user_id'],
  314. 'type' => $params['log_type'],
  315. 'service_user_id' => $params['service_user_id']??0,
  316. ];
  317. ServiceScanLog::create($data);
  318. return true;
  319. } catch (\Exception $e) {
  320. self::setError($e->getMessage());
  321. return false;
  322. }
  323. }
  324. }