Stock.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\Admin\controller;
  3. use think\Db;
  4. class Stock extends Base
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. /**
  11. * @param status
  12. * @param username
  13. * @param nickname
  14. * @param mobile
  15. * @param stock_low
  16. * @param stock_up
  17. */
  18. public function StockList(){
  19. $page = isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) : 1;
  20. $size = isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :10;
  21. $status = isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']) :"";
  22. $where=[];
  23. if($status!==""){
  24. $where['status'] = $status;
  25. }
  26. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  27. if($username!=""){
  28. $where['username'] = ["like","%{$username}%"];
  29. }
  30. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  31. if($nickname!=""){
  32. $where['nickname'] = ["like","%{$nickname}%"];
  33. }
  34. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  35. if($mobile!=""){
  36. $where['mobile'] = ["like","%{$mobile}%"];
  37. }
  38. $stock_low = isset($this->post['stock_low'])&&$this->post['stock_low']!=="" ? intval($this->post['stock_low'])
  39. :"";
  40. $wherestock="1=1";
  41. if($stock_low!=""){
  42. // $where['stock_balance'] = [">=",$stock_low];
  43. $wherestock .=" and stock_balance>={$stock_low}";
  44. }
  45. $stock_up = isset($this->post['stock_up'])&&$this->post['stock_up']!=="" ? intval($this->post['stock_up']) :"";
  46. if($stock_up!=""){
  47. // $where['stock_balance'] = ["<=",$stock_up];
  48. $wherestock .=" and stock_balance<={$stock_up}";
  49. }
  50. $count= Db::name("stock_list")->where($where)->where($wherestock)->count();
  51. $total = ceil($count/$size);
  52. $page = $page>=$total? $total:$page;
  53. $list = Db::name("stock_list")->where($where)->where($wherestock)->page($page,$size)->field("id,username,status,nickname,mobile,stock_balance,stock_update")->order("stock_update desc")->select();
  54. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  55. }
  56. /**
  57. * @param id
  58. * @param stock
  59. * @param type
  60. */
  61. public function Save(){
  62. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post["id"]) :"";
  63. if($id==""){
  64. return error_show(1004,"参数id 不能为空");
  65. }
  66. $account = Db::name("account")->where(["is_del"=>0,"id"=>$id])->find();
  67. if(empty($account)){
  68. return error_show(1005,"账户信息不存在");
  69. }
  70. $stock = isset($this->post['stock'])&&$this->post['stock']!==""? $this->post["stock"] :"";
  71. if($stock==""){
  72. return error_show(1005,"参数stock 不能为空或0");
  73. }
  74. $type = isset($this->post['type'])&&$this->post['type']!=""?intval($this->post['type']) :"";
  75. if($type===""){
  76. return error_show(1005,"参数type 不能为空");
  77. }
  78. if(!in_array($type,[1,2])){
  79. return error_show(1005,"参数type 值无效");
  80. }
  81. $stockinfo = Db::name("account_stock")->where(["accountid"=>$id,"is_del"=>0])->find();
  82. Db::startTrans();
  83. try {
  84. $msg = $type==1?"增加库存":"减少库存";
  85. $log=[
  86. "accountid"=>$id,
  87. "run_stock"=>$stock,
  88. "type"=>$type,
  89. "after_stock"=>isset($stockinfo['stock_balance']) ? $stockinfo['stock_balance']+$stock:$stock,
  90. "before_stock"=>isset($stockinfo['stock_balance']) ? $stockinfo['stock_balance']:0,
  91. "action_uid"=>$this->userinfo['id'],
  92. "action_name"=>$this->userinfo['nickname'],
  93. "addtime"=>date("Y-m-d H:i:s")
  94. ];
  95. $stocklog= Db::name("stock_log")->insert($log);
  96. if($stocklog){
  97. if($stockinfo){
  98. if($type==1){
  99. $stockinfo['stock_total']+=$stock;
  100. $stockinfo['stock_balance']+=$stock;
  101. }else{
  102. if($stockinfo['stock_balance']<$stock){
  103. Db::rollback();
  104. return error_show(1005,"剩余库存不足");
  105. }
  106. $stockinfo['stock_total']-=$stock;
  107. $stockinfo['stock_balance']-=$stock;
  108. }
  109. $stockinfo['updatetime']=date("Y-m-d H:i:s");
  110. $accstock = Db::name("account_stock")->update($stockinfo);
  111. }else{
  112. if($type==1){
  113. $data = [
  114. "accountid"=>$id,
  115. "stock_total"=>$stock,
  116. "stock_balance"=>$stock,
  117. "stock_used"=>0,
  118. "stock_delivery"=>0,
  119. "status"=>1,
  120. "is_del"=>0,
  121. "updatetime"=>date("Y-m-d H:i:s"),
  122. "addtime"=>date("Y-m-d H:i:s")
  123. ];
  124. $accstock = Db::name("account_stock")->insert($data);
  125. }else{
  126. Db::rollback();
  127. return error_show(1005,"账户剩余库存不足");
  128. }
  129. }
  130. if($accstock){
  131. write_log("账户{$account['username']}{$msg}:{$stock}",$this->userinfo,"stock","edit","0");
  132. Db::commit();
  133. return app_show(0,"{$msg}成功");
  134. }
  135. }
  136. Db::rollback();
  137. return error_show(1005,"{$msg}失败");
  138. }catch (\Exception $e){
  139. Db::rollback();
  140. return error_show(1004,$e->getMessage());
  141. }
  142. }
  143. /**
  144. * @param id
  145. * @param page
  146. * @param size
  147. */
  148. public function StockLog(){
  149. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post["id"]) :"";
  150. if($id==""){
  151. return error_show(1004,"参数id 不能为空");
  152. }
  153. $account = Db::name("account_list")->where(["is_del"=>0,"id"=>$id])->find();
  154. if(empty($account)){
  155. return error_show(1005,"账户信息不存在");
  156. }
  157. $page = isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) : 1;
  158. $size = isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :10;
  159. $count = Db::name("stock_log")->where(["accountid"=>$id])->count();
  160. $total = ceil($count/$size);
  161. $page = $page>=$total? $total:$page;
  162. $list = Db::name("stock_log")->where(["accountid"=>$id])->page($page,$size)->order("addtime desc")->select();
  163. foreach ($list as $key=>$value){
  164. $list[$key]['username']=$account['username'];
  165. $list[$key]['nickname']=$account['nickname'];
  166. }
  167. return app_show(0,"获取成功",['list'=>$list,"count"=>$count]);
  168. }
  169. }