Stock.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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=['a.is_del'=>0,'d.is_del'=>0];
  23. if($status!==""){
  24. $where['a.status'] = $status;
  25. }
  26. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  27. if($username!=""){
  28. $where['a.username'] = ["like","%{$username}%"];
  29. }
  30. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  31. if($nickname!=""){
  32. $where['v.nickname'] = ["like","%{$nickname}%"];
  33. }
  34. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  35. if($mobile!=""){
  36. $where['v.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("account")->alias('a')
  51. ->join("fc_rela_account b","a.id = b.accountid","left")
  52. ->join("fc_account_info v","b.account_info = v.id","left")
  53. ->join("fc_account_stock d","a.id = d.accountid","left")
  54. ->where($where)->count();
  55. $total = ceil($count/$size);
  56. $page = $page>=$total? $total:$page;
  57. $list = Db::name("account")->alias('a')->where($where)->page($page,$size)
  58. ->join("fc_rela_account b","a.id = b.accountid","left")
  59. ->join("fc_account_info v","b.account_info = v.id","left")
  60. ->join("fc_account_stock d","a.id = d.accountid","left")
  61. ->field("`a`.`id` AS `id`,
  62. `a`.`username` AS `username`,
  63. `a`.`status` AS `status`,
  64. `a`.`starttime` AS `starttime`,
  65. `a`.`expiretime` AS `expiretime`,
  66. `a`.`activetime` AS `activetime`,
  67. `a`.`addtime` AS `addtime`,
  68. `v`.`nickname` AS `nickname`,
  69. `v`.`avatar` AS `avatar`,
  70. `v`.`mobile` AS `mobile`,
  71. `v`.`remark` AS `remark`,
  72. ifnull( `d`.`stock_total`, 0 ) AS `stock_total`,
  73. ifnull( `d`.`stock_used`, 0 ) AS `stock_used`,
  74. ifnull( `d`.`stock_balance`, 0 ) AS `stock_balance`,
  75. ifnull( `d`.`stock_delivery`, 0 ) AS `stock_delivery`,
  76. ifnull( `d`.`updatetime`, '' ) AS `stock_update` ")
  77. ->order("d.updatetime desc")->select();
  78. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  79. }
  80. /**
  81. * @param id
  82. * @param stock
  83. * @param type
  84. */
  85. public function Save(){
  86. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post["id"]) :"";
  87. if($id==""){
  88. return error_show(1004,"参数id 不能为空");
  89. }
  90. $account = Db::name("account")->where(["is_del"=>0,"id"=>$id])->find();
  91. if(empty($account)){
  92. return error_show(1005,"账户信息不存在");
  93. }
  94. $stock = isset($this->post['stock'])&&$this->post['stock']!==""? $this->post["stock"] :"";
  95. if($stock==""){
  96. return error_show(1005,"参数stock 不能为空或0");
  97. }
  98. $type = isset($this->post['type'])&&$this->post['type']!=""?intval($this->post['type']) :"";
  99. if($type===""){
  100. return error_show(1005,"参数type 不能为空");
  101. }
  102. if(!in_array($type,[1,2])){
  103. return error_show(1005,"参数type 值无效");
  104. }
  105. $stockinfo = Db::name("account_stock")->where(["accountid"=>$id,"is_del"=>0])->find();
  106. Db::startTrans();
  107. try {
  108. $msg = $type==1?"增加库存":"减少库存";
  109. $log=[
  110. "accountid"=>$id,
  111. "run_stock"=>$stock,
  112. "type"=>$type,
  113. "after_stock"=>isset($stockinfo['stock_balance']) ? $stockinfo['stock_balance']+$stock:$stock,
  114. "before_stock"=>isset($stockinfo['stock_balance']) ? $stockinfo['stock_balance']:0,
  115. "action_uid"=>$this->userinfo['id'],
  116. "action_name"=>$this->userinfo['nickname'],
  117. "addtime"=>date("Y-m-d H:i:s")
  118. ];
  119. $stocklog= Db::name("stock_log")->insert($log);
  120. if($stocklog){
  121. if($stockinfo){
  122. if($type==1){
  123. $stockinfo['stock_total']+=$stock;
  124. $stockinfo['stock_balance']+=$stock;
  125. }else{
  126. if($stockinfo['stock_balance']<$stock){
  127. Db::rollback();
  128. return error_show(1005,"剩余库存不足");
  129. }
  130. $stockinfo['stock_total']-=$stock;
  131. $stockinfo['stock_balance']-=$stock;
  132. }
  133. $stockinfo['updatetime']=date("Y-m-d H:i:s");
  134. $accstock = Db::name("account_stock")->update($stockinfo);
  135. }else{
  136. if($type==1){
  137. $data = [
  138. "accountid"=>$id,
  139. "stock_total"=>$stock,
  140. "stock_balance"=>$stock,
  141. "stock_used"=>0,
  142. "stock_delivery"=>0,
  143. "status"=>1,
  144. "is_del"=>0,
  145. "updatetime"=>date("Y-m-d H:i:s"),
  146. "addtime"=>date("Y-m-d H:i:s")
  147. ];
  148. $accstock = Db::name("account_stock")->insert($data);
  149. }else{
  150. Db::rollback();
  151. return error_show(1005,"账户剩余库存不足");
  152. }
  153. }
  154. if($accstock){
  155. write_log("账户{$account['username']}{$msg}:{$stock}",$this->userinfo,"stock","edit","0");
  156. Db::commit();
  157. return app_show(0,"{$msg}成功");
  158. }
  159. }
  160. Db::rollback();
  161. return error_show(1005,"{$msg}失败");
  162. }catch (\Exception $e){
  163. Db::rollback();
  164. return error_show(1004,$e->getMessage());
  165. }
  166. }
  167. /**
  168. * @param id
  169. * @param page
  170. * @param size
  171. */
  172. public function StockLog(){
  173. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post["id"]) :"";
  174. if($id==""){
  175. return error_show(1004,"参数id 不能为空");
  176. }
  177. $account = Db::name("account")->alias('a')->where(["a.is_del"=>0,"a.id"=>$id])
  178. ->join("fc_rela_account b", "a.id = b.accountid", "left")
  179. ->join("fc_account_info c", "b.account_info= c.id", "left")
  180. ->field("`a`.`id` AS `id`,
  181. `a`.`username` AS `username`,
  182. `a`.`password` AS `password`,
  183. `a`.`salt` AS `salt`,
  184. `a`.`status` AS `status`,
  185. `a`.`is_del` AS `is_del`,
  186. `a`.`starttime` AS `starttime`,
  187. `a`.`expiretime` AS `expiretime`,
  188. `a`.`activetime` AS `activetime`,
  189. `a`.`addtime` AS `addtime`,
  190. `c`.`nickname` AS `nickname`,
  191. `c`.`avatar` AS `avatar`,
  192. `c`.`mobile` AS `mobile`,
  193. `c`.`remark` AS `remark`,
  194. `c`.`sex` AS `sex`")
  195. ->find();
  196. if(empty($account)){
  197. return error_show(1005,"账户信息不存在");
  198. }
  199. $page = isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) : 1;
  200. $size = isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :10;
  201. $count = Db::name("stock_log")->where(["accountid"=>$id])->count();
  202. $total = ceil($count/$size);
  203. $page = $page>=$total? $total:$page;
  204. $list = Db::name("stock_log")->where(["accountid"=>$id])->page($page,$size)->order("addtime desc")->select();
  205. foreach ($list as $key=>$value){
  206. $list[$key]['username']=$account['username'];
  207. $list[$key]['nickname']=$account['nickname'];
  208. }
  209. return app_show(0,"获取成功",['list'=>$list,"count"=>$count]);
  210. }
  211. }