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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.enums.EquipmentStatusEnum;
import org.springblade.modules.business.project.base.entity.ProjectInfoEntity;
import org.springblade.modules.business.project.base.service.IProjectBaseInfoService;

import org.springblade.modules.business.rtu.base.dto.RtuInfoDTO;
import org.springblade.modules.business.rtu.base.entity.RtuInfoEntity;
import org.springblade.modules.business.rtumanage.entity.TreeNodeEntity;
import org.springblade.modules.business.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.rtumanage.vo.RtuStatusVO;
import org.springblade.modules.business.rtumanage.wrapper.RtuManageWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

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

/***
 * Date:2022/8/18
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */

@RestController
@RequestMapping("galaxy-business/rtu/manage")
@AllArgsConstructor
@Api(value = "测站设备管理工作台", tags = "测站设备管理工作台")
public class RtuManageController extends BladeController {

	private final IRtuManageService rtuManageService;
	private final IDeptService deptService;
	private final IProjectBaseInfoService projectBaseInfoService;

	@GetMapping("/tree")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<TreeNodeEntity>> tree(Query query) {
		List<TreeNodeEntity> tree = new ArrayList<>();
		BladeUser user = AuthUtil.getUser();
		TreeNodeEntity root = new TreeNodeEntity();
		Long indexId = 0L;
		if (BusinessConstant.ROLE_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_SUPER_ADMIN.equals(user.getRoleName())) {
			LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
			wrapper.eq(Dept::getParentId, 0L);
			Dept dept = deptService.getOne(wrapper);
			String id = Func.randomUUID();
			root.setId(indexId);
			indexId++;
			root.setKey(id);
			root.setValue(id);
			root.setText(dept.getDeptName());
			root.setTitle(dept.getDeptName());
			root.setOrgId(dept.getId().toString());
			List<Dept> subDepts = deptService.getDeptChild(dept.getId());
			List<TreeNodeEntity> subDeptNodes = new ArrayList<>();
			for (Dept d : subDepts) {
				TreeNodeEntity node = new TreeNodeEntity();
				id = Func.randomUUID();
				node.setId(indexId);
				indexId++;
				node.setKey(id);
				node.setValue(id);
				node.setText(d.getDeptName());
				node.setTitle(d.getDeptName());
				node.setOrgId(d.getId().toString());


				LambdaQueryWrapper<ProjectInfoEntity> projectwrapper = Wrappers.<ProjectInfoEntity>query().lambda();
				projectwrapper.eq(ProjectInfoEntity::getOrgId, d.getId());
				projectwrapper.eq(ProjectInfoEntity::getIsDeleted, 0);
				projectwrapper.orderByAsc(ProjectInfoEntity::getProjectName);
				List<ProjectInfoEntity> pages = projectBaseInfoService.list(projectwrapper);

				List<TreeNodeEntity> childrn = new ArrayList<>();
				for (ProjectInfoEntity entity : pages) {
					TreeNodeEntity projectnode = new TreeNodeEntity();
					id = Func.randomUUID();
					projectnode.setId(indexId);
					indexId++;
					projectnode.setKey(id);
					projectnode.setValue(id);
					projectnode.setText(entity.getProjectName());
					projectnode.setTitle(entity.getProjectName());
					projectnode.setOrgId(entity.getOrgId().toString());
					projectnode.setProjectId(entity.getId().toString());
					childrn.add(projectnode);
				}

				node.setChildren(childrn);

				subDeptNodes.add(node);
			}
			root.setChildren(subDeptNodes);
		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			Dept dept = deptService.getById(Func.toLong(user.getDeptId()));

			String id = Func.randomUUID();
			root.setId(indexId);
			indexId++;
			root.setKey(id);
			root.setValue(id);
			root.setText(dept.getDeptName());
			root.setTitle(dept.getDeptName());
			root.setOrgId(user.getDeptId());
			LambdaQueryWrapper<ProjectInfoEntity> projectwrapper = Wrappers.<ProjectInfoEntity>query().lambda();
			projectwrapper.eq(ProjectInfoEntity::getOrgId, Func.toLong(user.getDeptId()));
			projectwrapper.eq(ProjectInfoEntity::getIsDeleted, 0);
			projectwrapper.orderByAsc(ProjectInfoEntity::getProjectName);
			List<ProjectInfoEntity> pages = projectBaseInfoService.list(projectwrapper);
			List<TreeNodeEntity> childrn = new ArrayList<>();
			for (ProjectInfoEntity entity : pages) {
				TreeNodeEntity node = new TreeNodeEntity();
				id = Func.randomUUID();
				node.setId(indexId);
				indexId++;
				node.setKey(id);
				node.setValue(id);
				node.setText(entity.getProjectName());
				node.setTitle(entity.getProjectName());
				node.setOrgId(entity.getOrgId().toString());
				node.setProjectId(entity.getId().toString());
				childrn.add(node);
			}
			root.setChildren(childrn);
		}
		tree.add(root);

		return R.data(tree);
	}


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


	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入RtuInfoEntity")
	public R<IPage<RtuStatusVO>> page(@ApiIgnore RtuInfoDTO rtuInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			rtuInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			rtuInfoDTO.setOrgId(null);
		}
		IPage<RtuStatusVO> pages = rtuManageService.selectPage(Condition.getPage(query), rtuInfoDTO);
		List<RtuStatusVO> list = pages.getRecords();
		for (RtuStatusVO vo : list) {
			RtuManageWrapper.build().toVoInfo(vo);
		}
		return R.data(pages);
	}
}
