Manager.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\ManagerChange;
  5. use think\App;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. class Manager extends Base
  9. {
  10. protected $model=null;
  11. public function __construct(App $app) {
  12. parent::__construct($app);
  13. $this->model=new ManagerChange();
  14. }
  15. public function create(){
  16. $param = $this->request->only(["orderCode"=>[],"manager"=>"","managerid"=>"","companyNo"=>"","remark"=>""],"post","trim");
  17. $valid=Validate::rule([
  18. "orderCode|订单集合"=>"require|array",
  19. "manager|项目经理"=>"require|max:255",
  20. "managerid|项目经理"=>"require|number|gt:0",
  21. "companyNo|业务公司"=>"require"
  22. ]);
  23. if($valid->check($param)==false) return error_show(1004,$valid->getError());
  24. $orderinfo= Db::name("sale")->whereIn("orderCode",$param['orderCode'])->where("supplierNo",$param['companyNo'])
  25. ->column("orderCode,manager,managerid","orderCode");
  26. if(count($orderinfo)!= count($param['orderCode'])) return error_show(1004,"数据存在业务公司不一致");
  27. $userCommon = \app\admin\common\User::getIns();
  28. $supp = $userCommon->handle("getCodeAndName",["code"=>$param['companyNo']]);
  29. if(!isset($supp['data']) || empty($supp['data'])) return error_show(1004,'业务公司数据未获取到');
  30. $companyName = $supp['data'][$param['companyNo']]?:"";
  31. foreach ($orderinfo as $item){
  32. $data[]=[
  33. 'apply_id'=>$this->uid,
  34. 'apply_name'=>$this->uname,
  35. 'orderCode'=>$item['orderCode'],
  36. 'old_manager'=>$item['manager'],
  37. 'old_managerid'=>$item['managerid'],
  38. 'companyName'=>$companyName,
  39. 'companyNo'=>$param['companyNo'],
  40. 'manager'=>$param['manager'],
  41. 'managerid'=>$param['managerid'],
  42. 'remark'=>$param['remark'],
  43. 'status'=>1
  44. ];
  45. }
  46. $ad =$this->model->saveAll($data);
  47. return $ad? app_show(0,"创建成功"): error_show(1004,"创建失败");
  48. }
  49. public function list(){
  50. $param = $this->request->only(['orderCode'=>'','oaCode'=>'','companyNo'=>'','apply_name'=>'','status'=>'','manager'=>'','old_manager'=>'','page'=>1,'size'=>15],'post','trim');
  51. $where=[];
  52. $param['orderCode']==''?: $where[]=['orderCode','like',"%{$param['orderCode']}%"];
  53. $param['apply_name']==''?: $where[]=['apply_name','like',"%{$param['apply_name']}%"];
  54. $param['companyNo']==''?: $where[]=['companyNo','like',"%{$param['companyNo']}%"];
  55. $param['manager']==''?: $where[]=['manager','like',"%{$param['manager']}%"];
  56. $param['old_manager']==''?: $where[]=['old_manager','like',"%{$param['old_manager']}%"];
  57. $param['status']==''?: $where[]=['status','=',$param['status']];
  58. $list = $this->model->where($where)->order('id','desc')->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  59. return app_show(0,'获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  60. }
  61. public function info(){
  62. $param = $this->request->only(['id'],'post','trim');
  63. $info = $this->model->findOrEmpty($param['id']);
  64. return app_show(0,'获取成功',$info);
  65. }
  66. public function status(){
  67. $param = $this->request->only(['id'=>'','status'=>'','remark'=>''],'post','trim');
  68. $Valid =Validate::rule(['id|申请id'=>'require','status|状态'=>'require|number|in:2,3']);
  69. if($Valid->check($param)==false) return error_show(1004,$Valid->getError());
  70. $info = $this->model->findOrEmpty($param['id']);
  71. if($info->isEmpty())return error_show(1004,'未找到申请单数据');
  72. if($info->status!=1) return error_show(1004,'申请单数据已审核');
  73. $info->status=$param['status'];
  74. $info->remark=$param['remark'];
  75. $this->model->startTrans();
  76. try{
  77. $up=$info->save();
  78. if($up==false)throw new \Exception('申请单审核失败');
  79. if($param['status']==2){
  80. $update=["manager"=>$info->manager,"managerid"=>$info->managerid,"updatetime"=>date("Y-m-d H:i:s")];
  81. Db::name("sale")->where("orderCode",$info->orderCode)->update($update);
  82. }
  83. $this->model->commit();
  84. }catch (\Exception $exception){
  85. $this->model->rollback();
  86. return error_show(1004,$exception->getMessage());
  87. }
  88. return app_show(0,'申请数据处理完成');
  89. }
  90. //批量导入
  91. public function export(){
  92. $param = $this->request->only(['list'=>[],'companyNo'=>''],'post','trim');
  93. $Valid =Validate::rule([
  94. 'list|申请修改数据'=>'require|array',
  95. 'companyNo|业务公司'=>'require'
  96. ]);
  97. if($Valid->check($param)==false) return error_show(1004,$Valid->getError());
  98. $valid=Validate::rule([
  99. 'orderCode|订单集合'=>'require',
  100. 'manager|项目经理'=>'require|max:255',
  101. 'managerid|项目经理'=>'require|number|gt:0'
  102. ]);
  103. $userCommon = \app\admin\common\User::getIns();
  104. $supp = $userCommon->handle('getCodeAndName',['code'=>$param['companyNo']]);
  105. if(!isset($supp['data']) || empty($supp['data'])) return error_show(1004,'业务公司数据未获取到');
  106. $companyName = $supp['data'][$param['companyNo']]?:'';
  107. $orderUp=[];
  108. $changeIn=[];
  109. $sale=new \app\admin\model\Sale();
  110. foreach ($param['list'] as $item){
  111. if($valid->check($item)==false) return error_show(1004,$valid->getError());
  112. $orderInfo = $sale->where(["orderCode"=>$item['orderCode'],"is_del"=>0])->findOrEmpty();
  113. if($orderInfo->isEmpty()) return error_show(1004,"{$item['orderCode']} 未找到数据");
  114. if($orderInfo['supplierNo']!=$param['companyNo'])return error_show(1004,"{$item['orderCode']} 业务公司不一致");
  115. $orderUp[]=["id"=>$orderInfo['id'],"manager"=>$item['manager'],"managerid"=>$item['managerid'],"updatetime"=>date("Y-m-d H:i:s")];
  116. $changeIn[]=[
  117. 'orderCode'=>$item['orderCode'],
  118. 'apply_id'=>$this->uid,
  119. 'apply_name'=>$this->uname,
  120. 'manager'=>$item['manager'],
  121. 'managerid'=>$item['managerid'],
  122. 'companyName'=>$companyName,
  123. 'companyNo'=>$param['companyNo'],
  124. 'old_manager'=>$orderInfo->manager,
  125. 'remark'=>'',
  126. 'status'=>2,
  127. 'old_managerid'=>$orderInfo->managerid
  128. ];
  129. }
  130. $this->model->startTrans();
  131. try{
  132. if(empty($orderUp)==false)$sale->saveAll($orderUp);
  133. if(empty($orderUp)==false)$this->model->saveAll($changeIn);
  134. $this->model->commit();
  135. }catch (\Exception $e){
  136. $this->model->rollback();
  137. return error_show(1004,$e->getMessage());
  138. }
  139. return app_show(0,'申请数据处理完成');
  140. }
  141. }