AssetRepair.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 advancedStartAssetRepair($id,$orgId,$userId,$formJson,$extra){
  10. $formJson = json_decode($formJson,true);
  11. $data = [
  12. 'org_id' => $orgId,
  13. 'user_id' => $userId,
  14. 'apply_id' => $id,
  15. 'dep_id' => $extra['depId'],
  16. 'create_time' => date('Y-m-d H:i:s'),
  17. 'status' => 0,
  18. ];
  19. $items = [];
  20. foreach ($formJson as $k=>$v){
  21. if($v['componentName'] == 'ddassetrepairfield'){
  22. foreach ($v['components'] as $key=>$val){
  23. $val['values'] = isset($val['values'])?$val['values']:'';
  24. switch ($val['idx']){
  25. case '0':
  26. $items = json_decode($val['values'],true);
  27. break;
  28. case '1':
  29. $data['company'] = $val['values'];
  30. break;
  31. case '2':
  32. $data['money'] = $val['values'];
  33. break;
  34. case '3':
  35. $data['name'] = $val['values'];
  36. break;
  37. case '4':
  38. $data['phone'] = $val['values'];
  39. break;
  40. case '5':
  41. $data['address'] = $val['values'];
  42. break;
  43. case '6':
  44. $data['remark'] = $val['values'];
  45. break;
  46. }
  47. }
  48. break;
  49. }
  50. }
  51. $amount = 0;
  52. foreach ($items as $k=>$v){
  53. $amount += $v['snums']*$v['price'];
  54. }
  55. $data['amount'] = $amount;
  56. $receiveId = Db::name('asset_repair')->insertGetId($data);
  57. $sitems = [];
  58. foreach ($items as $k=>$v){
  59. $sitems[] = [
  60. 'repair_id' => $receiveId,
  61. 'items_id' => $v['id'],
  62. 'nums' => $v['snums']
  63. ];
  64. }
  65. $count = Db::name('asset_repair_items')->insertAll($sitems);
  66. if($count != count($sitems)){
  67. return false;
  68. }
  69. return true;
  70. }
  71. public function advancedEndAssetRepair($id,$orgId,$userId,$formJson){
  72. $data = [
  73. 'update_time' => date('Y-m-d H:i:s'),
  74. 'status' => 1
  75. ];
  76. $info = Db::name('asset_repair')->where('apply_id',$id)->find();
  77. if(!$info || $info['status'] != 0){
  78. return false;
  79. }
  80. $ret = Db::name('asset_repair')->where('apply_id',$id)->update($data);
  81. if(!$ret){
  82. return false;
  83. }
  84. // 修改状态
  85. $itemsids = Db::name('asset_repair_items')->where('repair_id',$info['id'])->column('items_id');
  86. Db::name('asset_items')->where('id','in',$itemsids)->update([
  87. 'enable' => 4,
  88. 'update_time' => date('Y-m-d H:i:s')
  89. ]);
  90. return $ret?true:false;
  91. }
  92. public function advancedDisagreeAssetRepair($id){
  93. $data = [
  94. 'update_time' => date('Y-m-d H:i:s'),
  95. 'status' => 2
  96. ];
  97. $ret = Db::name('asset_repair')->where('apply_id',$id)->update($data);
  98. if(!$ret){
  99. return false;
  100. }
  101. return true;
  102. }
  103. public function lists($page,$size,$title,$start,$end,$orgId){
  104. $map[] = ['or.org_id','=',$orgId];
  105. if($title){
  106. $map[] = ['ui.name','like','%'.$title.'%'];
  107. }
  108. if($start&&$end){
  109. $map[] = ['or.create_time','egt',$start];
  110. $map[] = ['or.create_time','elt',$end];
  111. }
  112. $lists = Db::name('asset_repair')
  113. ->alias('or')
  114. ->join('user_info ui','ui.user_id = or.agent_user_id')
  115. ->field('or.*,ui.name as user_name')
  116. ->where($map)
  117. ->page($page,$size)
  118. ->order('or.id desc')
  119. ->select();
  120. $lists = $lists?$lists:[];
  121. foreach ($lists as $k=>$v){
  122. $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['agent_dep_id'])->value('name');
  123. }
  124. $total = Db::name('asset_repair')
  125. ->alias('or')
  126. ->join('user_info ui','ui.user_id = or.agent_user_id')
  127. ->where($map)->count();
  128. $data = [
  129. 'total' => $total,
  130. 'list' => $lists
  131. ];
  132. return $data;
  133. }
  134. public function add(){
  135. $data = [
  136. 'id' => input('id/d',0),
  137. 'org_id' => input('orgId/d',0),
  138. 'agent_user_id' => input('agentUserId/d',0),
  139. 'agent_dep_id' => input('agentDepId/d',0),
  140. 'company' => input('company','','trim'),
  141. 'name' => input('name','','trim'),
  142. 'phone' => input('phone','','trim'),
  143. 'address' => input('address','','trim'),
  144. 'reason' => input('reason','','trim'),
  145. 'method' => input('method','','trim'),
  146. 'accessories' => input('accessories','','trim'),
  147. 'remark' => input('remark','','trim'),
  148. 'files' => input('files','','trim'),
  149. 'items' => input('items','','trim')
  150. ];
  151. $result = validate('AssetRepair')->check($data,[],'');
  152. if(true !== $result){
  153. HelpHander::error(validate('AssetRepair')->getError());
  154. }
  155. $id = $data['id'];
  156. unset($data['id']);
  157. if($id > 0){
  158. $data['update_time'] = date('Y-m-d H:i:s');
  159. unset($data['items']);
  160. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  161. if(!$ret){
  162. HelpHander::error('操作失败');
  163. }
  164. }else{
  165. $data['create_time'] = date('Y-m-d H:i:s');
  166. Db::startTrans();
  167. try{
  168. $data['sn'] = $this->getSn($data['org_id']);
  169. $items = json_decode($data['items'],true);
  170. unset($data['items']);
  171. $receiveId = Db::name('AssetRepair')->insertGetId($data);
  172. if(!$receiveId){
  173. \exception('操作失败');
  174. }
  175. $d = [];
  176. $itemids = [];
  177. foreach ($items as $k=>$v){
  178. $iteminfo = Db::name('asset_items')
  179. ->where('id',$v['id'])
  180. ->field('id,user_id,dep_id')
  181. ->find();
  182. $d[] = [
  183. 'repair_id' => $receiveId,
  184. 'items_id' => $v['id'],
  185. 'user_id' => $iteminfo['user_id'],
  186. 'dep_id' => $iteminfo['dep_id'],
  187. ];
  188. $itemids[] = $v['id'];
  189. }
  190. $res = Db::name('asset_repair_items')->insertAll($d);
  191. if($res != count($d)){
  192. \exception('操作失败');
  193. }
  194. $sd = [
  195. 'enable' => 4,
  196. 'update_time' => date('Y-m-d H:i:s')
  197. ];
  198. $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
  199. if($res != count($itemids)){
  200. \exception('操作失败');
  201. }
  202. Db::commit();
  203. }catch (Exception $e){
  204. Db::rollback();
  205. HelpHander::error($e->getMessage());
  206. }
  207. }
  208. return true;
  209. }
  210. public function getSn($orgId){
  211. $max = Db::name('asset_repair')
  212. ->where('org_id',$orgId)
  213. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  214. ->order('id desc')
  215. ->value('sn');
  216. $maxinit = $max?$max:date('Ymd').'0000';
  217. return $maxinit + 1;
  218. }
  219. public function info($id){
  220. $info = $this->where('id',$id)->find();
  221. if(!$info){
  222. HelpHander::error('数据不存在');
  223. }
  224. $data = $info->toArray();
  225. $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('name');
  226. $data['agentUserName'] = Db::name('user_info')->where('user_id',$info['agent_user_id'])->value('name');
  227. $items = Db::name('asset_repair_items')
  228. ->alias('ari')
  229. ->join('asset_items ai','ai.id = ari.items_id')
  230. ->join('asset_unit au','au.id = ai.unit_id')
  231. ->where('ari.repair_id',$id)
  232. ->field('ai.*,au.title as unitName')
  233. ->select();
  234. $data['items'] = $items?$items:[];
  235. return $data;
  236. }
  237. public function finish(){
  238. $id = input('id/d',0);
  239. if($id <= 0){
  240. HelpHander::error('参数错误');
  241. }
  242. $info = Db::name('asset_repair')->where('id',$id)->find();
  243. if(!$info){
  244. HelpHander::error('记录不存在');
  245. }
  246. if($info['finish'] != 0){
  247. HelpHander::error('记录已处理');
  248. }
  249. Db::startTrans();
  250. try{
  251. $ret = Db::name('asset_repair')->where('id',$id)->update([
  252. 'finish' => 1,
  253. 'finish_time' => date('Y-m-d H:i:s')
  254. ]);
  255. if(!$ret){
  256. \exception('操作失败');
  257. }
  258. $items = Db::name('asset_repair_items')
  259. ->alias('ari')
  260. ->join('asset_items ai','ai.id = ari.items_id')
  261. ->where('ari.repair_id',$id)
  262. ->field('ai.*')
  263. ->select();
  264. foreach ($items as $k=>$v){
  265. $d = [
  266. 'update_time' => date('Y-m-d H:i:s')
  267. ];
  268. if($v['user_id'] > 0){
  269. $d['enable'] = 2;
  270. }else{
  271. $d['enable'] = 1;
  272. }
  273. $res = Db::name('asset_items')->where('id',$v['id'])->update($d);
  274. if(!$res){
  275. \exception('操作失败');
  276. }
  277. }
  278. Db::commit();
  279. }catch (Exception $e){
  280. Db::rollback();
  281. HelpHander::error($e->getMessage());
  282. }
  283. return true;
  284. }
  285. }