<?php


namespace app\admin\controller;

use think\App;
use think\Exception;
use think\facade\Db;
use think\facade\Validate;
class DepartTips extends Base{
	public function __construct(App $app) {
		parent::__construct($app);
	}

	public function list(){
		$post = $this->request->only(["year"=>"","month"=>"","companyNo"=>"","page"=>1,"size"=>15],"post","trim");
		$where=[];
		if($post["year"]!=='') $where[]=["year","=",$post["year"]];
		if($post["month"]!=='') $where[]=["month","=",$post["month"]];
		if($post["companyNo"]!=='') $where[]=["companyNo","=",$post["companyNo"]];
		$count =Db::name("depart_tips")->where($where)->count();
		$total = ceil($count/$post['size']);
		$page = $total>=$post["page"]? intval($post['page']): intval($total);
		$list = Db::name("depart_tips")->where($where)->page($page,intval($post['size']))->order("id desc")->select();
		return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
	}

	public function create(){
		$post = $this->request->only(["tips"=>[],"companyNo"=>""],"post","trim");
		if($post['companyNo']=="") return error_show(1004,"业务公司编号不能为空");
		if(empty($post['tips'])) return error_show(1004,"指标数据不能为空");
		$rule =Validate::rule([
			"year|年份"=>"require|number",
			"month|月份"=>"require|number|between:1,12",
			"total_tips|业绩指标"=>"require|float"
			]);
		$companyAr = UserHandle("getCodeAndName",["code"=>$post["companyNo"]]);
		if(!isset($companyAr['code'])|| $companyAr['code']!=0)return error_show(1004,"业务公司信息未找到");
		$companyName = $companyAr["data"][$post["companyNo"]]??"";
		$inst=[];
		Db::startTrans();
		try{
		foreach ($post["tips"] as $item){
			if($rule->check($item)==false) throw new Exception($rule->getError());
			$ist =Db::name("depart_tips")->where(["companyNo"=>$post["companyNo"],"month"=>$item["month"],
			"year"=>$item["year"]])->findOrEmpty();
			if(empty($ist))$inst[]=[
				"companyNo"=>$post["companyNo"],
				"companyName"=>$companyName,
				"year"=>$item["year"],
				"month"=>$item["month"],
				"total_tips"=>$item["total_tips"]
				];
			else Db::name("depart_tips")->where($ist)->update(["total_tips"=>$item["total_tips"]]);
		}

		if(!empty($inst))Db::name("depart_tips")->insertAll($inst);
		Db::commit();
		return app_show(0,"数据操作成功");
		}catch (\Exception $e){
			Db::rollback();
			return error_show(1004,$e->getMessage());
		}
	}

	public function departlist(){
		$post = $this->request->only(["year"=>"","month"=>"","depart_id"=>"","page"=>1,"size"=>15],"post","trim");
		$where=[];
		if($post["year"]!=='') $where[]=["year","=",$post["year"]];
		if($post["month"]!=='') $where[]=["month","=",$post["month"]];
		if($post["depart_id"]!=='') $where[]=["depart_id","=",$post["depart_id"]];
		$count =Db::connect("mysql_wsm3.0")->name("depart_tips")->where($where)->count();
		$total = ceil($count/$post['size']);
		$page = $total>=$post["page"]? intval($post['page']): intval($total);
		$list = Db::connect("mysql_wsm3.0")->name("depart_tips")->where($where)->page($page,intval($post['size']))
			->order("id desc")->select();
		return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
	}

	public function departcreate(){
		$post = $this->request->only(["tips"=>[],"depart_id"=>""],"post","trim");
		if($post['depart_id']=="") return error_show(1004,"业务部门不能为空");
		if(empty($post['tips'])) return error_show(1004,"指标数据不能为空");
		$rule =Validate::rule([
			"year|年份"=>"require|number",
			"month|月份"=>"require|number|between:1,12",
			"total_tips|业绩指标"=>"require|float"
			]);
		$companyAr = UserHandle("get_part",["itemid"=>$post["depart_id"]]);
		if(!isset($companyAr['code'])|| $companyAr['code']!=0)return error_show(1004,"业务部门信息未找到");
		$companyName = $companyAr["data"][$post["depart_id"]]??"";
		$inst=[];
		Db::connect("mysql_wsm3.0")->startTrans();
		try{
		foreach ($post["tips"] as $item){
			if($rule->check($item)==false) throw new Exception($rule->getError());
			$ist =Db::connect("mysql_wsm3.0")->name("depart_tips")->where(["depart_id"=>$post["depart_id"],
			"month"=>$item["month"],"year"=>$item["year"]])->findOrEmpty();
			if(empty($ist))$inst[]=[
				"depart_id"=>$post["companyNo"],
				"depart_item"=>$companyName,
				"year"=>$item["year"],
				"month"=>$item["month"],
				"total_tips"=>$item["total_tips"]
				];
			else Db::connect("mysql_wsm3.0")->name("depart_tips")->where($ist)->update(["total_tips"=>$item["total_tips"]]);
		}

		if(!empty($inst))Db::connect("mysql_wsm3.0")->name("depart_tips")->insertAll($inst);
		Db::connect("mysql_wsm3.0")->commit();
		return app_show(0,"数据操作成功");
		}catch (\Exception $e){
			Db::connect("mysql_wsm3.0")->rollback();
			return error_show(1004,$e->getMessage());
		}
	}
}