JhGoodsModel.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model;
  12. use app\common\model\ShopGoodsSpecType as Spec;
  13. use app\common\model\TimeModel;
  14. use think\facade\Db;
  15. use think\Model;
  16. class JhGoodsModel extends Model
  17. {
  18. protected $name = "jh_goods";
  19. public function jhsellSubmit($uid, $params){
  20. if (intval($params['count']) <= 0 || floatval($params['price']) <= 0){
  21. sr_throw('参数错误');
  22. }
  23. $jh_min_money = sysconfig('jhconfig', 'jh_min_money');
  24. $jh_max_money = sysconfig('jhconfig', 'jh_max_money');
  25. if ($params['price'] < $jh_min_money ){
  26. sr_throw('不能小于最低价');
  27. }
  28. if ($params['price'] > $jh_max_money ){
  29. sr_throw('不能高于最高价');
  30. }
  31. // $new_count = intval($params['count']);
  32. if (!(is_numeric($params['count']) && intval($params['count'])==$params['count'])){
  33. sr_throw('个数只能是整数');
  34. }
  35. if (sr_getdianLen($params['price']) > 2){
  36. sr_throw('价格最多两位小数');
  37. }
  38. $model = new UserModel();
  39. $userinfo = $model->getUserDetailByUid($uid);
  40. if ($userinfo['tz_num'] < $params['count']){
  41. sr_throw('当前通证不足');
  42. }
  43. edit_user_tz(8, $uid, $params['count']);
  44. self::insert([
  45. 'uid'=>$uid,
  46. 'jh_count'=>$params['count'],
  47. 'price'=>$params['price'],
  48. 'total_price'=>$params['count'] * $params['price'],
  49. 'create_time'=>sr_getcurtime(time()),
  50. 'status'=>1
  51. ]);
  52. }
  53. public function jhBuyGoodsSubmit($uid, $params){
  54. if (intval($params['count']) <= 0 || floatval($params['price']) <= 0){
  55. sr_throw('参数错误');
  56. }
  57. $jh_min_money = sysconfig('jhconfig', 'jh_min_money');
  58. $jh_max_money = sysconfig('jhconfig', 'jh_max_money');
  59. if ($params['price'] < $jh_min_money ){
  60. sr_throw('不能小于最低价');
  61. }
  62. if ($params['price'] > $jh_max_money ){
  63. sr_throw('不能高于最高价');
  64. }
  65. // $new_count = intval($params['count']);
  66. if (!(is_numeric($params['count']) && intval($params['count'])==$params['count'])){
  67. sr_throw('个数只能是整数');
  68. }
  69. $model = new UserModel();
  70. $userinfo = $model->getUserDetailByUid($uid);
  71. if (sr_getdianLen($params['price']) > 2){
  72. sr_throw('价格最多两位小数');
  73. }
  74. $total_money = $params['count'] * $params['price'];
  75. if ($userinfo['money'] < $total_money){
  76. sr_throw('当前余额不足');
  77. }
  78. edit_user_money(20, $uid, $total_money);
  79. self::insert([
  80. 'uid'=>$uid,
  81. 'type'=>2,
  82. 'jh_count'=>$params['count'],
  83. 'price'=>$params['price'],
  84. 'total_price'=>$total_money,
  85. 'create_time'=>sr_getcurtime(time()),
  86. 'status'=>1
  87. ]);
  88. }
  89. public function getSellList($uid, $para){
  90. $order = 'id asc';
  91. $page = $para['page'];
  92. $limit = $para['limit'];
  93. switch (intval($para['sort'])){
  94. case 1:
  95. $order = 'g.total_price asc';
  96. break;
  97. case 2:
  98. $order = 'g.total_price desc';
  99. break;
  100. case 3:
  101. $order = 'g.price asc';
  102. break;
  103. case 4:
  104. $order = 'g.price desc';
  105. break;
  106. default:
  107. break;
  108. }
  109. $list = $this->alias('g')
  110. ->where('g.status', 1)
  111. ->where('g.type', 1)
  112. ->leftJoin('user u', 'u.id = g.uid')
  113. ->field('g.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  114. ->withAttr('avatar', function ($val, $data){
  115. return getWebUrl().'/'.$val;
  116. })
  117. ->withAttr('level', function ($val, $data){
  118. return 0;
  119. })
  120. ->order($order)
  121. ->page($page, $limit)
  122. ->select()->toArray();
  123. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  124. }
  125. public function jhBuyList($uid, $para){
  126. $order = 'id asc';
  127. $page = $para['page'];
  128. $limit = $para['limit'];
  129. switch (intval($para['sort'])){
  130. case 1:
  131. $order = 'g.total_price asc';
  132. break;
  133. case 2:
  134. $order = 'g.total_price desc';
  135. break;
  136. case 3:
  137. $order = 'g.price asc';
  138. break;
  139. case 4:
  140. $order = 'g.price desc';
  141. break;
  142. default:
  143. break;
  144. }
  145. $list = $this->alias('g')
  146. ->where('g.status', 1)
  147. ->where('g.type', 2)
  148. ->leftJoin('user u', 'u.id = g.uid')
  149. ->field('g.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  150. ->withAttr('avatar', function ($val, $data){
  151. return getWebUrl().'/'.$val;
  152. })
  153. ->withAttr('level', function ($val, $data){
  154. return 0;
  155. })
  156. ->order($order)
  157. ->page($page, $limit)
  158. ->select()->toArray();
  159. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  160. }
  161. public function jhsellList($uid, $para){
  162. $order = 'id desc';
  163. $page = $para['page'];
  164. $limit = $para['limit'];
  165. $status = isset($para['status'])?$para['status']:0;
  166. // switch (intval($para['sort'])){
  167. // case 1:
  168. // $order = 'g.total_price asc';
  169. // break;
  170. // case 2:
  171. // $order = 'g.total_price desc';
  172. // break;
  173. // case 3:
  174. // $order = 'g.price asc';
  175. // break;
  176. // case 4:
  177. // $order = 'g.price desc';
  178. // break;
  179. // default:
  180. // break;
  181. //
  182. //
  183. // }
  184. $arr_status = [];
  185. if ($status == 0|| empty($status)){
  186. $arr_status = [1,2,3,4];
  187. }else{
  188. $arr_status = [$status];
  189. }
  190. $list = $this->where('uid', $uid)
  191. ->where('status', 'in',$arr_status)
  192. ->where('type', 1)
  193. // ->leftJoin('user u', 'u.id = g.uid')
  194. // ->field('g.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  195. // ->withAttr('avatar', function ($val, $data){
  196. // return getWebUrl().'/'.$val;
  197. // })
  198. ->order($order)
  199. ->page($page, $limit)
  200. ->select()->toArray();
  201. return $list;
  202. }
  203. public function jhMyBuyList($uid, $para){
  204. $order = 'id desc';
  205. $page = $para['page'];
  206. $limit = $para['limit'];
  207. $status = isset($para['status'])?$para['status']:0;
  208. // switch (intval($para['sort'])){
  209. // case 1:
  210. // $order = 'g.total_price asc';
  211. // break;
  212. // case 2:
  213. // $order = 'g.total_price desc';
  214. // break;
  215. // case 3:
  216. // $order = 'g.price asc';
  217. // break;
  218. // case 4:
  219. // $order = 'g.price desc';
  220. // break;
  221. // default:
  222. // break;
  223. //
  224. //
  225. // }
  226. $arr_status = [];
  227. if ($status == 0|| empty($status)){
  228. $arr_status = [1,2,3,4];
  229. }else{
  230. $arr_status = [$status];
  231. }
  232. $list = $this->where('uid', $uid)
  233. ->where('status', 'in',$arr_status)
  234. ->where('type', 2)
  235. // ->leftJoin('user u', 'u.id = g.uid')
  236. // ->field('g.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  237. // ->withAttr('avatar', function ($val, $data){
  238. // return getWebUrl().'/'.$val;
  239. // })
  240. ->order($order)
  241. ->page($page, $limit)
  242. ->select()->toArray();
  243. return $list;
  244. }
  245. public function jhUnSellGoods($uid, $para){
  246. $goods_info = $this->where('id', $para['g_id'])->find();
  247. $time = time();
  248. if (!$goods_info){
  249. sr_throw("参数错误");
  250. }
  251. if ($goods_info['uid'] != $uid){
  252. sr_throw('无权操作');
  253. }
  254. if ($goods_info['status'] != 1){
  255. sr_throw('状态错误');
  256. }
  257. $goods_info->save([
  258. 'status'=>2,
  259. 'unsell_time'=>sr_getcurtime($time)
  260. ]);
  261. edit_user_tz(10,$uid ,$goods_info['jh_count']);
  262. }
  263. public function jhUnBuyGoods($uid, $para){
  264. $goods_info = $this->where('id', $para['g_id'])->find();
  265. $time = time();
  266. if (!$goods_info){
  267. sr_throw("参数错误");
  268. }
  269. if ($goods_info['uid'] != $uid){
  270. sr_throw('无权操作');
  271. }
  272. if ($goods_info['status'] != 1){
  273. sr_throw('状态错误');
  274. }
  275. $goods_info->save([
  276. 'status'=>2,
  277. 'unsell_time'=>sr_getcurtime($time)
  278. ]);
  279. edit_user_money(21,$uid ,$goods_info['total_price']);
  280. }
  281. }