0
0

AssetWithdraw.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AssetWithdraw extends Model
  8. {
  9. public function updates(){
  10. $data = request()->post();
  11. $data['org_id'] = cur_org_id();
  12. $result = validate('AssetWithdraw')->check($data,[],'');
  13. if(true !== $result){
  14. $this->error = validate('AssetWithdraw')->getError();
  15. return false;
  16. }
  17. $data['agent_dep_id'] = Db::name('user_dep')->where('user_id',$data['agent_user_id'])->value('dep_id');
  18. $id = $data['id'];
  19. unset($data['id']);
  20. if($id > 0){
  21. $data['update_time'] = date('Y-m-d H:i:s');
  22. unset($data['items']);
  23. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  24. if(!$ret){
  25. $this->error = '操作失败';
  26. return false;
  27. }
  28. }else{
  29. $data['create_time'] = date('Y-m-d H:i:s');
  30. Db::startTrans();
  31. try{
  32. $data['sn'] = $this->getSn($data['org_id']);
  33. $items = $data['items'];
  34. unset($data['items']);
  35. $receiveId = Db::name('asset_withdraw')->insertGetId($data);
  36. if(!$receiveId){
  37. \exception('操作失败');
  38. }
  39. $d = [];
  40. $itemids = [];
  41. foreach ($items as $k=>$v){
  42. $iteminfo = Db::name('asset_items')
  43. ->where('id',$v['id'])
  44. ->field('id,user_id,dep_id')
  45. ->find();
  46. $d[] = [
  47. 'withdraw_id' => $receiveId,
  48. 'items_id' => $v['id'],
  49. 'user_id' => $iteminfo['user_id'],
  50. 'dep_id' => $iteminfo['dep_id'],
  51. ];
  52. $itemids[] = $v['id'];
  53. }
  54. $res = Db::name('asset_withdraw_items')->insertAll($d);
  55. if($res != count($d)){
  56. \exception('操作失败');
  57. }
  58. $sd = [
  59. 'address' => $data['address'],
  60. 'enable' => 1,
  61. 'user_id' => 0,
  62. 'dep_id' => 0,
  63. 'used'=>'',
  64. 'update_time' => date('Y-m-d H:i:s')
  65. ];
  66. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  67. if($res != count($itemids)){
  68. \exception('操作失败');
  69. }
  70. Db::commit();
  71. }catch (Exception $e){
  72. Db::rollback();
  73. $this->error = $e->getMessage();
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. public function getSn($orgId){
  80. $max = Db::name('asset_withdraw')
  81. ->where('org_id',$orgId)
  82. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  83. ->order('id desc')
  84. ->value('sn');
  85. $maxinit = $max?$max:date('Ymd').'0000';
  86. return $maxinit + 1;
  87. }
  88. public function info($id){
  89. $info = $this->where('id',$id)->find();
  90. if(!$info){
  91. $this->error = '数据不存在';
  92. return false;
  93. }
  94. $data = $info->toArray();
  95. $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('title');
  96. $data['withDepName'] = Db::name('dep')->where('id',$info['with_dep_id'])->value('title');
  97. $data['agentUserName'] = Db::name('user')->where('id',$info['agent_user_id'])->value('real_name');
  98. $items = Db::name('asset_withdraw_items')
  99. ->alias('ari')
  100. ->join('asset_items ai','ai.id = ari.items_id')
  101. ->join('asset_unit au','au.id = ai.unit_id')
  102. ->where('ari.withdraw_id',$id)
  103. ->field('ai.*,au.title as unitName,ari.user_id as old_user_id,ari.dep_id as old_dep_id')
  104. ->select();
  105. $total = 0;
  106. foreach ($items as $k=>$v){
  107. $items[$k]['userName'] = Db::name('user')->where('id',$v['old_user_id'])->value('real_name');
  108. $items[$k]['depName'] = Db::name('dep')->where('id',$v['old_dep_id'])->value('title');
  109. $total += $v['price'];
  110. }
  111. $data['items'] = $items?$items:[];
  112. $data['total'] = $total;
  113. return $data;
  114. }
  115. }