/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.business.rtu.base.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.*;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.DictBizCache;
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.equipment.inspection.report.dto.EquipmentInspectionReportDTO;
import org.springblade.modules.business.project.servicePerson.entity.ServicePersonEntity;
import org.springblade.modules.business.project.servicePerson.vo.OrgPersonContactVO;
import org.springblade.modules.business.project.servicePerson.vo.ServicePersonVO;
import org.springblade.modules.business.rtu.base.dto.RtuInfoDTO;
import org.springblade.modules.business.rtu.base.entity.RtuInfoEntity;
import org.springblade.modules.business.rtu.base.service.IRtuBaseInfoService;
import org.springblade.modules.business.rtu.base.vo.RtuInfoVO;
import org.springblade.modules.business.rtu.base.vo.RtuStatisticsInfoVO;
import org.springblade.modules.business.rtu.base.wrapper.RtuInfoWrapper;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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/rtu/base")
@AllArgsConstructor
@Api(value = "测站设备基础管理", tags = "测站设备基础管理")
public class RtuBaseInfoController extends BladeController {
	private final IRtuBaseInfoService rtuBaseInfoService;
	private final IDeptService deptService;
	private final IRtuWarningService rtuWarningService;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuInfoVO>> list(Query query) {
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>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(RtuInfoEntity::getOrgId,user.getDeptId());
		}
		wrapper.eq(RtuInfoEntity::getIsDeleted,0);
		wrapper.orderByAsc(RtuInfoEntity::getOrgId);
		wrapper.orderByAsc(RtuInfoEntity::getProjectId);
		wrapper.orderByAsc(RtuInfoEntity::getRtuCode);

		IPage<RtuInfoEntity> pages = rtuBaseInfoService.page(Condition.getPage(query),wrapper);
		return R.data(RtuInfoWrapper.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 = "分页")
	public R<IPage<RtuInfoVO>> page(@ApiIgnore RtuInfoDTO rtuInfoDTO, 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())) {
			rtuInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		}

		IPage<RtuInfoVO> pages = rtuBaseInfoService.selectPage(Condition.getPage(query),rtuInfoDTO);
		List<RtuInfoVO> list = pages.getRecords();
		for (RtuInfoVO rtuInfoVO : list) {
			RtuInfoWrapper.build().appendInfo(rtuInfoVO);
		}
		return R.data(pages);
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入rtuInfoEntity")
	public R<RtuInfoVO> detail(RtuInfoEntity rtuInfoEntity) {
		RtuInfoEntity detail = rtuBaseInfoService.getOne(Condition.getQueryWrapper(rtuInfoEntity));
		return R.data(RtuInfoWrapper.build().entityVO(detail));
	}


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

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

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

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

	@GetMapping("/dict/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<DictBiz>> dictList(@RequestParam String code) {
		List<DictBiz> dicts = DictBizCache.getList(code);

		return R.data(dicts);
	}


	/**
	 * 按机构统计测站信息
	 * @return
	 */
	@GetMapping("/statistics")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<RtuStatisticsInfoVO>> rtuStatistics() {
		List<RtuStatisticsInfoVO> list = new LinkedList<>();
		BladeUser user = AuthUtil.getUser();
		List<Dept> root = this.deptService.getDeptChild(Func.toLong(user.getDeptId()));

		for (Dept dept : root) {
			RtuStatisticsInfoVO rtuStatisticsInfoVO = new RtuStatisticsInfoVO();
			rtuStatisticsInfoVO.setId(dept.getId());
			rtuStatisticsInfoVO.setOrgName(dept.getDeptName());
			LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
			wrapper.eq(RtuInfoEntity::getOrgId,dept.getId());
			wrapper.eq(RtuInfoEntity::getIsDeleted,0);
			Long rtus =rtuBaseInfoService.count(wrapper);
			rtuStatisticsInfoVO.setRtuCount(rtus);

			RtuWarningInfoDTO rtuWarningInfoDTO=new RtuWarningInfoDTO();
			rtuWarningInfoDTO.setOrgId(dept.getId());
			Long warningRtus = rtuWarningService.warningRtuCount(rtuWarningInfoDTO);
			rtuStatisticsInfoVO.setWarningRtuCount(warningRtus);

			list.add(rtuStatisticsInfoVO);
		}

		return R.data(list);
	}

}
