/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.business.project.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.desk.entity.NoticeEntity;
import org.springblade.modules.business.project.base.entity.ProjectInfoEntity;
import org.springblade.modules.business.project.base.entity.ProjectTreeNodeEntity;
import org.springblade.modules.business.project.base.entity.ProjectWarningSettingEntity;
import org.springblade.modules.business.project.base.service.IProjectBaseInfoService;
import org.springblade.modules.business.project.base.service.IProjectWarningSettingService;
import org.springblade.modules.business.project.base.vo.ProjectInfoVO;
import org.springblade.modules.business.project.base.wrapper.ProjectInfoWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/project/base")
@AllArgsConstructor
@Api(value = "项目信息基础管理", tags = "项目信息基础管理")
public class ProjectBaseInfoController extends BladeController {
	private final IProjectBaseInfoService projectBaseInfoService;
	private final IProjectWarningSettingService warningSettingService;
	private final IDeptService deptService;
	private final IUserService userService;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<ProjectInfoVO>> list(Query query) {
		IPage<ProjectInfoEntity> pages = projectBaseInfoService.page(Condition.getPage(query));
		return R.data(ProjectInfoWrapper.build().pageVO(pages));
	}

	@GetMapping("/org/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<ProjectInfoVO>> orglist() {
		LambdaQueryWrapper<ProjectInfoEntity> wrapper = Wrappers.<ProjectInfoEntity>query().lambda();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ADMIN.equals(user.getRoleName())) {

		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			wrapper.eq(ProjectInfoEntity::getOrgId, user.getDeptId());
		}
		wrapper.eq(ProjectInfoEntity::getIsDeleted, 0);
		List<ProjectInfoEntity> pages = projectBaseInfoService.list(wrapper);
		return R.data(ProjectInfoWrapper.build().listVO(pages));
	}

	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectName", value = "项目名称", paramType = "query", dataType = "string"),

	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页")
	public R<IPage<ProjectInfoVO>> page(@ApiIgnore  ProjectInfoEntity entity, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ADMIN.equals(user.getRoleName())) {

		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			entity.setOrgId(Func.toLong(user.getDeptId()));
		}
		IPage<ProjectInfoVO> pages = projectBaseInfoService.selectPage(entity, Condition.getPage(query));
		return R.data(pages);
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入projectInfoEntity")
	public R<ProjectInfoVO> detail(ProjectInfoEntity projectInfoEntity) {
		ProjectInfoEntity detail = projectBaseInfoService.getOne(Condition.getQueryWrapper(projectInfoEntity));
		return R.data(ProjectInfoWrapper.build().entityVO(detail));
	}


	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入ProjectInfoEntity")
	public R save(@RequestBody ProjectInfoEntity projectInfoEntity) {
		return R.status(projectBaseInfoService.save(projectInfoEntity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入ProjectInfoEntity")
	public R update(@RequestBody ProjectInfoEntity projectInfoEntity) {
		return R.status(projectBaseInfoService.updateById(projectInfoEntity));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入projectInfoEntity")
	public R submit(@RequestBody ProjectInfoEntity projectInfoEntity) {
		return R.status(projectBaseInfoService.saveOrUpdate(projectInfoEntity));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入projectInfoEntity")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = projectBaseInfoService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}

	/**
	 * 项目预警配置保存或修改
	 *
	 * @param projectInfoEntity
	 * @return
	 */
	@PostMapping("/setting/warning/submit")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "项目ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "warnActive", value = "预警开关", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "delayTime", value = "延时", paramType = "query", dataType = "int")
	})
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "项目预警配置新增或修改", notes = "传入warningSettingEntity")
	public R warningSubmit(@ApiIgnore ProjectInfoEntity projectInfoEntity) {
		//return R.status(warningSettingService.saveOrUpdate(warningSettingEntity));

		LambdaUpdateWrapper<ProjectInfoEntity> wrapper = Wrappers.<ProjectInfoEntity>update().lambda();
		wrapper.set(ProjectInfoEntity::getWarnActive, projectInfoEntity.getWarnActive());
		wrapper.set(ProjectInfoEntity::getDelayTime, projectInfoEntity.getDelayTime());
		wrapper.eq(ProjectInfoEntity::getId, projectInfoEntity.getId());

		this.projectBaseInfoService.update(wrapper);

		return R.success("预警设置操作成功");
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R<List<ProjectTreeNodeEntity>> projectTree() {
		BladeUser user = AuthUtil.getUser();
		List<ProjectTreeNodeEntity> tree = new LinkedList<>();
		if (BusinessConstant.ROLE_ADMIN.equals(user.getRoleName())) {
			LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
			wrapper.eq(Dept::getParentId, 0);
			Dept dept = this.deptService.getOne(wrapper);
			ProjectTreeNodeEntity root = new ProjectTreeNodeEntity();
			root.setText(dept.getDeptName());
			root.setValue("" + dept.getId() + "-0");
			root.setOrgId(dept.getId());

			List<ProjectTreeNodeEntity> orgNode = new LinkedList<>();
			root.setChildren(orgNode);
			tree.add(root);
			List<Dept> childs = this.deptService.getDeptChild(dept.getId());
			if (null != childs && childs.size() > 0) {
				for (Dept child : childs) {
					ProjectTreeNodeEntity entity = new ProjectTreeNodeEntity();
					entity.setText(child.getDeptName());
					entity.setValue("" + child.getId() + "-0");
					entity.setOrgId(child.getId());

					List<ProjectTreeNodeEntity> projectNode = new LinkedList<>();
					entity.setChildren(projectNode);
					orgNode.add(entity);
					LambdaQueryWrapper<ProjectInfoEntity> projectwrapper = Wrappers.<ProjectInfoEntity>query().lambda();
					projectwrapper.eq(ProjectInfoEntity::getOrgId, child.getId());

					List<ProjectInfoEntity> projectInfoEntities = this.projectBaseInfoService.list(projectwrapper);
					for (ProjectInfoEntity p : projectInfoEntities) {
						entity = new ProjectTreeNodeEntity();
						entity.setText(p.getProjectName());
						entity.setValue("" + child.getId() + "-" + p.getId());
						entity.setOrgId(child.getId());
						entity.setProjectId(p.getId());
						projectNode.add(entity);
					}
				}
			}

		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {

			ProjectTreeNodeEntity root = new ProjectTreeNodeEntity();
			root.setText("北京大恒");
			root.setValue("0");
			List<ProjectTreeNodeEntity> projectNode = new LinkedList<>();
			root.setChildren(projectNode);
			tree.add(root);

			ProjectTreeNodeEntity entity = new ProjectTreeNodeEntity();
			entity.setText("赤峰市");
			entity.setValue("1");
			projectNode.add(entity);

			entity = new ProjectTreeNodeEntity();
			entity.setText("锡盟");
			entity.setValue("2");
			projectNode.add(entity);

		}

		return R.data(tree);
	}

}
