123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AssetRepair extends Model
- {
- public function advancedStartAssetRepair($id,$orgId,$userId,$formJson,$extra){
- $formJson = json_decode($formJson,true);
- $data = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'apply_id' => $id,
- 'dep_id' => $extra['depId'],
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ];
- $items = [];
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddassetrepairfield'){
- foreach ($v['components'] as $key=>$val){
- $val['values'] = isset($val['values'])?$val['values']:'';
- switch ($val['idx']){
- case '0':
- $items = json_decode($val['values'],true);
- break;
- case '1':
- $data['company'] = $val['values'];
- break;
- case '2':
- $data['money'] = $val['values'];
- break;
- case '3':
- $data['name'] = $val['values'];
- break;
- case '4':
- $data['phone'] = $val['values'];
- break;
- case '5':
- $data['address'] = $val['values'];
- break;
- case '6':
- $data['remark'] = $val['values'];
- break;
- }
- }
- break;
- }
- }
- $amount = 0;
- foreach ($items as $k=>$v){
- $amount += $v['snums']*$v['price'];
- }
- $data['amount'] = $amount;
- $receiveId = Db::name('asset_repair')->insertGetId($data);
- $sitems = [];
- foreach ($items as $k=>$v){
- $sitems[] = [
- 'repair_id' => $receiveId,
- 'items_id' => $v['id'],
- 'nums' => $v['snums']
- ];
- }
- $count = Db::name('asset_repair_items')->insertAll($sitems);
- if($count != count($sitems)){
- return false;
- }
- return true;
- }
- public function advancedEndAssetRepair($id,$orgId,$userId,$formJson){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 1
- ];
- $info = Db::name('asset_repair')->where('apply_id',$id)->find();
- if(!$info || $info['status'] != 0){
- return false;
- }
- $ret = Db::name('asset_repair')->where('apply_id',$id)->update($data);
- if(!$ret){
- return false;
- }
- // 修改状态
- $itemsids = Db::name('asset_repair_items')->where('repair_id',$info['id'])->column('items_id');
- Db::name('asset_items')->where('id','in',$itemsids)->update([
- 'enable' => 4,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- return $ret?true:false;
- }
- public function advancedDisagreeAssetRepair($id){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 2
- ];
- $ret = Db::name('asset_repair')->where('apply_id',$id)->update($data);
- if(!$ret){
- return false;
- }
- return true;
- }
- public function lists($page,$size,$title,$start,$end,$orgId){
- $map[] = ['or.org_id','=',$orgId];
- if($title){
- $map[] = ['ui.name','like','%'.$title.'%'];
- }
- if($start&&$end){
- $map[] = ['or.create_time','egt',$start];
- $map[] = ['or.create_time','elt',$end];
- }
- $lists = Db::name('asset_repair')
- ->alias('or')
- ->join('user_info ui','ui.user_id = or.agent_user_id')
- ->field('or.*,ui.name as user_name')
- ->where($map)
- ->page($page,$size)
- ->order('or.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['agent_dep_id'])->value('name');
- }
- $total = Db::name('asset_repair')
- ->alias('or')
- ->join('user_info ui','ui.user_id = or.agent_user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'org_id' => input('orgId/d',0),
- 'agent_user_id' => input('agentUserId/d',0),
- 'agent_dep_id' => input('agentDepId/d',0),
- 'company' => input('company','','trim'),
- 'name' => input('name','','trim'),
- 'phone' => input('phone','','trim'),
- 'address' => input('address','','trim'),
- 'reason' => input('reason','','trim'),
- 'method' => input('method','','trim'),
- 'accessories' => input('accessories','','trim'),
- 'remark' => input('remark','','trim'),
- 'files' => input('files','','trim'),
- 'items' => input('items','','trim')
- ];
- $result = validate('AssetRepair')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('AssetRepair')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- unset($data['items']);
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- Db::startTrans();
- try{
- $data['sn'] = $this->getSn($data['org_id']);
- $items = json_decode($data['items'],true);
- unset($data['items']);
- $receiveId = Db::name('AssetRepair')->insertGetId($data);
- if(!$receiveId){
- \exception('操作失败');
- }
- $d = [];
- $itemids = [];
- foreach ($items as $k=>$v){
- $iteminfo = Db::name('asset_items')
- ->where('id',$v['id'])
- ->field('id,user_id,dep_id')
- ->find();
- $d[] = [
- 'repair_id' => $receiveId,
- 'items_id' => $v['id'],
- 'user_id' => $iteminfo['user_id'],
- 'dep_id' => $iteminfo['dep_id'],
- ];
- $itemids[] = $v['id'];
- }
- $res = Db::name('asset_repair_items')->insertAll($d);
- if($res != count($d)){
- \exception('操作失败');
- }
- $sd = [
- 'enable' => 4,
- 'update_time' => date('Y-m-d H:i:s')
- ];
- $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd);
- if($res != count($itemids)){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- }
- return true;
- }
- public function getSn($orgId){
- $max = Db::name('asset_repair')
- ->where('org_id',$orgId)
- ->where('create_time','>=',date('Y-m-d').' 00:00:00')
- ->order('id desc')
- ->value('sn');
- $maxinit = $max?$max:date('Ymd').'0000';
- return $maxinit + 1;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('name');
- $data['agentUserName'] = Db::name('user_info')->where('user_id',$info['agent_user_id'])->value('name');
- $items = Db::name('asset_repair_items')
- ->alias('ari')
- ->join('asset_items ai','ai.id = ari.items_id')
- ->join('asset_unit au','au.id = ai.unit_id')
- ->where('ari.repair_id',$id)
- ->field('ai.*,au.title as unitName')
- ->select();
- $data['items'] = $items?$items:[];
- return $data;
- }
- public function finish(){
- $id = input('id/d',0);
- if($id <= 0){
- HelpHander::error('参数错误');
- }
- $info = Db::name('asset_repair')->where('id',$id)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['finish'] != 0){
- HelpHander::error('记录已处理');
- }
- Db::startTrans();
- try{
- $ret = Db::name('asset_repair')->where('id',$id)->update([
- 'finish' => 1,
- 'finish_time' => date('Y-m-d H:i:s')
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- $items = Db::name('asset_repair_items')
- ->alias('ari')
- ->join('asset_items ai','ai.id = ari.items_id')
- ->where('ari.repair_id',$id)
- ->field('ai.*')
- ->select();
- foreach ($items as $k=>$v){
- $d = [
- 'update_time' => date('Y-m-d H:i:s')
- ];
- if($v['user_id'] > 0){
- $d['enable'] = 2;
- }else{
- $d['enable'] = 1;
- }
- $res = Db::name('asset_items')->where('id',$v['id'])->update($d);
- if(!$res){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- }
|