DepartTips.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. class DepartTips extends Base{
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. }
  11. public function list(){
  12. $post = $this->request->only(["year"=>"","month"=>"","companyNo"=>"","page"=>1,"size"=>15],"post","trim");
  13. $where=[];
  14. if($post["year"]!=='') $where[]=["year","=",$post["year"]];
  15. if($post["month"]!=='') $where[]=["month","=",$post["month"]];
  16. if($post["companyNo"]!=='') $where[]=["companyNo","=",$post["companyNo"]];
  17. $count =Db::name("depart_tips")->where($where)->count();
  18. $total = ceil($count/$post['size']);
  19. $page = $total>=$post["page"]? intval($post['page']): intval($total);
  20. $list = Db::name("depart_tips")->where($where)->page($page,intval($post['size']))->order("id desc")->select();
  21. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  22. }
  23. public function create(){
  24. $post = $this->request->only(["tips"=>[],"companyNo"=>""],"post","trim");
  25. if($post['companyNo']=="") return error_show(1004,"业务公司编号不能为空");
  26. if(empty($post['tips'])) return error_show(1004,"指标数据不能为空");
  27. $rule =Validate::rule([
  28. "year|年份"=>"require|number",
  29. "month|月份"=>"require|number|between:1,12",
  30. "total_tips|业绩指标"=>"require|float"
  31. ]);
  32. $companyAr = UserHandle("getCodeAndName",["code"=>$post["companyNo"]]);
  33. if(!isset($companyAr['code'])|| $companyAr['code']!=0)return error_show(1004,"业务公司信息未找到");
  34. $companyName = $companyAr["data"][$post["companyNo"]]??"";
  35. $inst=[];
  36. Db::startTrans();
  37. try{
  38. foreach ($post["tips"] as $item){
  39. if($rule->check($item)==false) throw new Exception($rule->getError());
  40. $ist =Db::name("depart_tips")->where(["companyNo"=>$post["companyNo"],"month"=>$item["month"],
  41. "year"=>$item["year"]])->findOrEmpty();
  42. if(empty($ist))$inst[]=[
  43. "companyNo"=>$post["companyNo"],
  44. "companyName"=>$companyName,
  45. "year"=>$item["year"],
  46. "month"=>$item["month"],
  47. "total_tips"=>$item["total_tips"]
  48. ];
  49. else Db::name("depart_tips")->where($ist)->update(["total_tips"=>$item["total_tips"]]);
  50. }
  51. if(!empty($inst))Db::name("depart_tips")->insertAll($inst);
  52. Db::commit();
  53. return app_show(0,"数据操作成功");
  54. }catch (\Exception $e){
  55. Db::rollback();
  56. return error_show(1004,$e->getMessage());
  57. }
  58. }
  59. public function departlist(){
  60. $post = $this->request->only(["year"=>"","month"=>"","depart_id"=>"","page"=>1,"size"=>15],"post","trim");
  61. $where=[];
  62. if($post["year"]!=='') $where[]=["year","=",$post["year"]];
  63. if($post["month"]!=='') $where[]=["month","=",$post["month"]];
  64. if($post["depart_id"]!=='') $where[]=["depart_id","=",$post["depart_id"]];
  65. $count =Db::connect("mysql_wsm3.0")->name("depart_tips")->where($where)->count();
  66. $total = ceil($count/$post['size']);
  67. $page = $total>=$post["page"]? intval($post['page']): intval($total);
  68. $list = Db::connect("mysql_wsm3.0")->name("depart_tips")->where($where)->page($page,intval($post['size']))
  69. ->order("id desc")->select();
  70. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  71. }
  72. public function departcreate(){
  73. $post = $this->request->only(["tips"=>[],"depart_id"=>""],"post","trim");
  74. if($post['depart_id']=="") return error_show(1004,"业务部门不能为空");
  75. if(empty($post['tips'])) return error_show(1004,"指标数据不能为空");
  76. $rule =Validate::rule([
  77. "year|年份"=>"require|number",
  78. "month|月份"=>"require|number|between:1,12",
  79. "total_tips|业绩指标"=>"require|float"
  80. ]);
  81. $companyAr = UserHandle("get_part",["itemid"=>$post["depart_id"]]);
  82. if(!isset($companyAr['code'])|| $companyAr['code']!=0)return error_show(1004,"业务部门信息未找到");
  83. $companyName = $companyAr["data"][$post["depart_id"]]??"";
  84. $inst=[];
  85. Db::connect("mysql_wsm3.0")->startTrans();
  86. try{
  87. foreach ($post["tips"] as $item){
  88. if($rule->check($item)==false) throw new Exception($rule->getError());
  89. $ist =Db::connect("mysql_wsm3.0")->name("depart_tips")->where(["depart_id"=>$post["depart_id"],
  90. "month"=>$item["month"],"year"=>$item["year"]])->findOrEmpty();
  91. if(empty($ist))$inst[]=[
  92. "depart_id"=>$post["companyNo"],
  93. "depart_item"=>$companyName,
  94. "year"=>$item["year"],
  95. "month"=>$item["month"],
  96. "total_tips"=>$item["total_tips"]
  97. ];
  98. else Db::connect("mysql_wsm3.0")->name("depart_tips")->where($ist)->update(["total_tips"=>$item["total_tips"]]);
  99. }
  100. if(!empty($inst))Db::connect("mysql_wsm3.0")->name("depart_tips")->insertAll($inst);
  101. Db::connect("mysql_wsm3.0")->commit();
  102. return app_show(0,"数据操作成功");
  103. }catch (\Exception $e){
  104. Db::connect("mysql_wsm3.0")->rollback();
  105. return error_show(1004,$e->getMessage());
  106. }
  107. }
  108. }