123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AssetRepair extends Model
- {
- public function updates(){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $result = validate('AssetRepair')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('AssetRepair')->getError();
- return false;
- }
- $data['agent_dep_id'] = Db::name('user_dep')->where('user_id',$data['agent_user_id'])->value('dep_id');
- $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){
- $this->error = '操作失败';
- return false;
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- Db::startTrans();
- try{
- $data['sn'] = $this->getSn($data['org_id']);
- $items = $data['items'];
- 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();
- $this->error = $e->getMessage();
- return false;
- }
- }
- 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('title');
- $data['agentUserName'] = Db::name('user')->where('id',$info['agent_user_id'])->value('real_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();
- $total = 0;
- foreach ($items as $k=>$v){
- $items[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
- $items[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
- $total += $v['price'];
- }
- $data['items'] = $items?$items:[];
- $data['total'] = $total;
- return $data;
- }
- public function finish(){
- $id = input('id/d',0);
- if($id <= 0){
- $this->error = '参数错误';
- return false;
- }
- $info = Db::name('asset_repair')->where('id',$id)->find();
- if(!$info){
- $this->error = '记录不存在';
- return false;
- }
- if($info['finish'] != 0){
- $this->error = '记录已处理';
- return false;
- }
- 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'),
- 'user_id'=>0,
- 'dep_id'=>0
- ];
- 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();
- $this->error = $e->getMessage();
- return false;
- }
- return true;
- }
- }
|