123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Budget extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'title' => input('title','','trim'),
- 'year' => input('year','','trim'),
- 'start_time' => input('startTime','','trim'),
- 'end_time' => input('endTime','','trim'),
- 'org_id' => input('orgId/d',0),
- 'content' => input('content','','trim'),
- 'items' => input('items','','trim'),
- 'auths' => input('auths','','trim'),
- ];
- $result = validate('Budget')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('Budget')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- Db::startTrans();
- try{
- $auths = $data['auths']?json_decode($data['auths'],true):[];
- unset($data['auths']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- Db::name('budget_auth')->where('budget_id',$id)->delete();
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- $id = $this->id;
- }
- if(!$ret){
- \exception('操作失败');
- }
- $arr = [];
- foreach ($auths as $k=>$v){
- $arr[] = [
- 'budget_id' => $id,
- 'dep_id' => $v['depId'],
- 'type' => $v['type']
- ];
- }
- $res = Db::name('budget_auth')->insertAll($arr);
- if($res != count($auths)){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- public function lists($page,$size,$title,$orgId){
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$orgId];
- if($title != ''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- /*$auths = Db::name('budget_auth')
- ->alias('ba')
- ->join('dep d','d.id = ba.dep_id')
- ->where('ba.budget_id',$v['id'])
- ->field('ba.dep_id,d.name')
- ->select();
- $auths = $auths?$auths:[];
- $depids = [];
- foreach ($auths as $kk=>$vv){
- $depids[] = $vv['dep_id'];
- }
- $lists[$k]['auths'] = implode(',',$depids);
- $lists[$k]['budget_auths'] = $auths;*/
- $auths = Db::name('budget_auth')
- ->where('budget_id',$v['id'])
- ->select();
- $nauth = [];
- foreach ($auths as $kk=>$vv){
- $nauth[] = [
- 'type' => $vv['type'],
- 'depId' => $vv['dep_id'],
- ];
- if($vv['type'] == 0){
- $auths[$kk]['name'] = Db::name('dep')->where('id',$vv['dep_id'])->value('name');
- }else{
- $auths[$kk]['name'] = Db::name('user_info')->where('user_id',$vv['dep_id'])->value('name');
- }
- }
- $lists[$k]['auths'] = $nauth?json_encode($nauth):'';
- $lists[$k]['budget_auths'] = $auths?$auths:[];
- }
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function all($orgId){
- $map[] = ['del','=',0];
- $map[] = ['enable','=',1];
- $map[] = ['org_id','=',$orgId];
- $lists = $this
- ->where($map)
- ->field('id,title')
- ->order('id desc')
- ->select();
- return $lists?$lists->toArray():[];
- }
- public function allItems($orgId){
- $map[] = ['del','=',0];
- $map[] = ['enable','=',1];
- $map[] = ['org_id','=',$orgId];
- $lists = Db::name('budget')
- ->where($map)
- ->field('id,title,items')
- ->order('id desc')
- ->select();
- $lists = $lists?$lists:[];
- $company = Db::name('company')->select();
- foreach ($lists as $k=>$v){
- $itemList = [];
- if($v['items']){
- $items = explode(',',$v['items']);
- $itemList = Db::name('budget_items')->where('id','in',$items)->where('del',0)->field('id,title,company_id')->order('sorts asc')->select();
- }
- foreach ($company as $key=>$val){
- $itlist = [];
- foreach ($itemList as $kk=>$vv){
- if($vv['company_id'] == $val['id']){
- $itlist[] = $vv;
- }
- }
- $company[$key]['options'] = $itlist;
- }
- $lists[$k]['items'] = $company;
- }
- return $lists;
- }
- public function del($id){
- $info = Db::name('budget_apply')->where('status',1)->where('budget_id',$id)->find();
- if($info){
- HelpHander::error('已有申请,无法删除');
- }
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function changeEnable($id,$enable){
- $info = Db::name('budget')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($enable == $info['enable']){
- HelpHander::error('已是当前状态,无需重复操作');
- }
- $ret = Db::name('budget')->where('id',$id)->setField('enable',$enable);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function alllist($userId,$orgId){
- $deps = model('Dep')->getUserDep($userId,$orgId,1);
- if(!$deps){
- return [];
- }
- $depids = [];
- foreach ($deps as $k => $v) {
- $depids[] = $v['id'];
- }
- $depids = implode(',',$depids);
- $curYear = date('Y');
- $company = Db::name('company')->field('id,title')->select();
- foreach ($company as $k=>$v){
- $items = Db::name('budget_dep')
- ->alias('bd')
- ->join('budget b','b.id = bd.budget_id')
- ->join('budget_items bi','bi.id = bd.items_id')
- ->where('b.year',$curYear)
- ->where('bd.status',1)
- ->where('bd.del',0)
- ->where('bd.dep_id','in',$depids)
- ->where('bd.company_id',$v['id'])
- ->field('bi.title,bd.id,bd.dep_id')
- ->select();
- $company[$k]['items'] = $items?$items:[];
- }
- return $company;
- }
- }
|