AssetRepair.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 AssetRepair extends Model
  8. {
  9. public function updates(){
  10. $data = request()->post();
  11. $data['org_id'] = cur_org_id();
  12. $result = validate('AssetRepair')->check($data,[],'');
  13. if(true !== $result){
  14. $this->error = validate('AssetRepair')->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('AssetRepair')->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. 'repair_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_repair_items')->insertAll($d);
  55. if($res != count($d)){
  56. \exception('操作失败');
  57. }
  58. $sd = [
  59. 'enable' => 4,
  60. 'update_time' => date('Y-m-d H:i:s')
  61. ];
  62. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  63. if($res != count($itemids)){
  64. \exception('操作失败');
  65. }
  66. Db::commit();
  67. }catch (Exception $e){
  68. Db::rollback();
  69. $this->error = $e->getMessage();
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. public function getSn($orgId){
  76. $max = Db::name('asset_repair')
  77. ->where('org_id',$orgId)
  78. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  79. ->order('id desc')
  80. ->value('sn');
  81. $maxinit = $max?$max:date('Ymd').'0000';
  82. return $maxinit + 1;
  83. }
  84. public function info($id){
  85. $info = $this->where('id',$id)->find();
  86. if(!$info){
  87. HelpHander::error('数据不存在');
  88. }
  89. $data = $info->toArray();
  90. $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('title');
  91. $data['agentUserName'] = Db::name('user')->where('id',$info['agent_user_id'])->value('real_name');
  92. $items = Db::name('asset_repair_items')
  93. ->alias('ari')
  94. ->join('asset_items ai','ai.id = ari.items_id')
  95. ->join('asset_unit au','au.id = ai.unit_id')
  96. ->where('ari.repair_id',$id)
  97. ->field('ai.*,au.title as unitName')
  98. ->select();
  99. $total = 0;
  100. foreach ($items as $k=>$v){
  101. $items[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  102. $items[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  103. $total += $v['price'];
  104. }
  105. $data['items'] = $items?$items:[];
  106. $data['total'] = $total;
  107. return $data;
  108. }
  109. public function finish(){
  110. $id = input('id/d',0);
  111. if($id <= 0){
  112. $this->error = '参数错误';
  113. return false;
  114. }
  115. $info = Db::name('asset_repair')->where('id',$id)->find();
  116. if(!$info){
  117. $this->error = '记录不存在';
  118. return false;
  119. }
  120. if($info['finish'] != 0){
  121. $this->error = '记录已处理';
  122. return false;
  123. }
  124. Db::startTrans();
  125. try{
  126. $ret = Db::name('asset_repair')->where('id',$id)->update([
  127. 'finish' => 1,
  128. 'finish_time' => date('Y-m-d H:i:s')
  129. ]);
  130. if(!$ret){
  131. \exception('操作失败');
  132. }
  133. $items = Db::name('asset_repair_items')
  134. ->alias('ari')
  135. ->join('asset_items ai','ai.id = ari.items_id')
  136. ->where('ari.repair_id',$id)
  137. ->field('ai.*')
  138. ->select();
  139. foreach ($items as $k=>$v){
  140. $d = [
  141. 'update_time' => date('Y-m-d H:i:s'),
  142. 'user_id'=>0,
  143. 'dep_id'=>0
  144. ];
  145. if($v['user_id'] > 0){
  146. $d['enable'] = 2;
  147. }else{
  148. $d['enable'] = 1;
  149. }
  150. $res = Db::name('asset_items')->where('id',$v['id'])->update($d);
  151. if(!$res){
  152. \exception('操作失败');
  153. }
  154. }
  155. Db::commit();
  156. }catch (Exception $e){
  157. Db::rollback();
  158. $this->error = $e->getMessage();
  159. return false;
  160. }
  161. return true;
  162. }
  163. }