/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.business.check.order.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.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.ConcurrentDateFormat;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.*;
import org.springblade.modules.baseinfo.stbase.service.IMvAttStBaseService;
import org.springblade.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.mapper.RtuCheckOrderMapper;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.order.vo.CheckOrderCountVO;
import org.springblade.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.check.order.vo.OrgCheckOrderCountVO;
import org.springblade.modules.business.check.order.wrapper.RtuCheckOrderWrapper;
import org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.business.check.process.wrapper.RtuOrderProcessWrapper;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.project.base.entity.ProjectTreeNodeEntity;
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.RtuStatisticsInfoVO;
import org.springblade.modules.business.rtu.data.entity.RtuDataRainEntity;
import org.springblade.modules.business.rtu.data.entity.RtuDataRiverEntity;
import org.springblade.modules.business.rtu.data.entity.RtuDataRsvrEntity;
import org.springblade.modules.business.rtu.data.service.IRtuDataGroundService;
import org.springblade.modules.business.rtu.data.service.IRtuDataRainService;
import org.springblade.modules.business.rtu.data.service.IRtuDataRiverService;
import org.springblade.modules.business.rtu.data.service.IRtuDataRsvrService;
import org.springblade.modules.business.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
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.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
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/check/order")
@AllArgsConstructor
@Api(value = "测站设备故障工单管理模块", tags = "测站设备故障工单管理模块")
public class RtuCheckOrderController extends BladeController {

	private final IRtuCheckOrderService checkOrderService;
	private final IRtuOrderProcessService processService;
	private final IRtuWarningService warningService;
	private final IDictBizService dictBizService;
	private final IRtuManageService manageService;
	private final IUserService userService;
	private final IDeptService deptService;
	private final IRtuBaseInfoService rtuBaseInfoService;
	private final IMvAttStBaseService iMvAttStBaseService;
	private final IRtuDataRainService iRtuDataRainService;
	private final IRtuDataRiverService iRtuDataRiverService;
	private final IRtuDataRsvrService iRtuDataRsvrService;
	private final IRtuDataGroundService iRtuDataGroundService;
	private final IRtuManageService iRtuManageService;

	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入CheckOrderInfoDTO")
	public R<List<CheckOrderInfoVO>> page(CheckOrderInfoDTO checkOrderInfoDTO) {


		LambdaQueryWrapper<CheckOrderInfoEntity> wrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
		if (checkOrderInfoDTO.getRtuCode() != null) {
			wrapper.eq(CheckOrderInfoEntity::getRtuCode, checkOrderInfoDTO.getRtuCode());
		}
		wrapper.orderByAsc(CheckOrderInfoEntity::getOrderStatusKey);
		wrapper.orderByDesc(CheckOrderInfoEntity::getUpdateTime);
		//wrapper.last("limit 1");
		List<CheckOrderInfoEntity> pages = checkOrderService.list(wrapper);

		return R.data(RtuCheckOrderWrapper.build().listVO(pages));
	}

	/**
	 * 工单查询，分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "orgId", value = "机构ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "orderConfirm", value = "是否确认", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "orderClose", value = "是否关闭", paramType = "query", dataType = "int"),
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入checkOrderInfoDTO")
	public R<IPage<CheckOrderInfoVO>> page(@ApiIgnore CheckOrderInfoDTO checkOrderInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			checkOrderInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		}
		IPage<CheckOrderInfoVO> pages = checkOrderService.selectPage(Condition.getPage(query), checkOrderInfoDTO);
		List<CheckOrderInfoVO> list = pages.getRecords();
		for (CheckOrderInfoVO v : list) {
			RtuCheckOrderWrapper.build().addInfoVO(v);
		}
		return R.data(pages);
	}

	/**
	 * 我的工单查询，分页
	 */
	@GetMapping("/my/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入checkOrderInfoDTO")
	public R<IPage<CheckOrderInfoVO>> mypage(@ApiIgnore CheckOrderInfoDTO checkOrderInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		checkOrderInfoDTO.setOrderConfirmUser(user.getUserId());
		checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
		checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
		IPage<CheckOrderInfoVO> pages = checkOrderService.selectPage(Condition.getPage(query), checkOrderInfoDTO);
		List<CheckOrderInfoVO> list = pages.getRecords();
		for (CheckOrderInfoVO v : list) {
			RtuCheckOrderWrapper.build().addInfoVO(v);
		}
		return R.data(pages);
	}


	/**
	 * 未确认工单列表，不分页
	 */
	@GetMapping("/unconfirm/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "不分页", notes = "")
	public R<List<CheckOrderInfoVO>> unconfirmOrderList() {
		BladeUser user = AuthUtil.getUser();
		CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
		LambdaQueryWrapper<CheckOrderInfoEntity> wrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
		checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			checkOrderInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			checkOrderInfoDTO.setOrgId(null);
		}
		List<CheckOrderInfoVO> list = checkOrderService.unconfirmOrderList(checkOrderInfoDTO);
		for (CheckOrderInfoVO v : list) {
			RtuCheckOrderWrapper.build().addInfoVO(v);
		}
		return R.data(list);
	}

	/**
	 * 今日工单动态
	 */
	@GetMapping("/today/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入checkOrderInfoDTO")
	public R<IPage<CheckOrderInfoVO>> todayPage(@ApiIgnore CheckOrderInfoDTO checkOrderInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			checkOrderInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			checkOrderInfoDTO.setOrgId(null);
		}
		Date date = new Date();
		String dt = Func.formatDateTime(date);
		checkOrderInfoDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
		IPage<CheckOrderInfoVO> pages = checkOrderService.selectPage(Condition.getPage(query), checkOrderInfoDTO);
		List<CheckOrderInfoVO> list = pages.getRecords();
		for (CheckOrderInfoVO v : list) {
			RtuCheckOrderWrapper.build().addInfoVO(v);
		}
		return R.data(pages);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入CheckOrderInfoEntity")
	public R<CheckOrderInfoVO> detail(CheckOrderInfoEntity checkOrderInfoEntity) {
		CheckOrderInfoEntity detail = checkOrderService.getOne(Condition.getQueryWrapper(checkOrderInfoEntity));

		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getRtuCode, detail.getRtuCode());
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0L);
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(wrapper);

		CheckOrderInfoVO vo = RtuCheckOrderWrapper.build().entityVO(detail);

		vo.setRtuName(rtuInfoEntity.getRtuName());
		vo.setAdCity(rtuInfoEntity.getAdCity());
		vo.setAdDist(rtuInfoEntity.getAdDist());
		vo.setAreaName(rtuInfoEntity.getAdCity() + "/" + rtuInfoEntity.getAdDist());
		vo.setLocationDesc(rtuInfoEntity.getLocationDesc());
		vo.setLng(rtuInfoEntity.getLng());
		vo.setLat(rtuInfoEntity.getLat());

		LambdaQueryWrapper<RtuStatusEntity> statusWwrapper = Wrappers.<RtuStatusEntity>query().lambda();
		statusWwrapper.eq(RtuStatusEntity::getRtuCode, detail.getRtuCode());
		statusWwrapper.eq(RtuStatusEntity::getIsDeleted, 0L);
		RtuStatusEntity statusEntity = iRtuManageService.getOne(statusWwrapper);
		if (null != statusEntity) {
			vo.setLastUpTime(statusEntity.getLastUpTime());
		}

		vo.setIsRain(rtuInfoEntity.getIsRain());
		vo.setIsRiver(rtuInfoEntity.getIsRiver());
		vo.setIsRes(rtuInfoEntity.getIsRes());
		vo.setIsGround(rtuInfoEntity.getIsGround());

		if (rtuInfoEntity.getIsRain() == 1) {
			LambdaQueryWrapper<RtuDataRainEntity> rainwrapper = Wrappers.<RtuDataRainEntity>query().lambda();
			rainwrapper.eq(RtuDataRainEntity::getRtuCode, detail.getRtuCode());
			rainwrapper.eq(RtuDataRainEntity::getIsDeleted, 0L);
			RtuDataRainEntity rainEntity = iRtuDataRainService.getOne(rainwrapper);
			vo.setDrp(rainEntity.getDrp());
		} else if (rtuInfoEntity.getIsRiver() == 1) {
			LambdaQueryWrapper<RtuDataRiverEntity> riverwrapper = Wrappers.<RtuDataRiverEntity>query().lambda();
			riverwrapper.eq(RtuDataRiverEntity::getRtuCode, detail.getRtuCode());
			riverwrapper.eq(RtuDataRiverEntity::getIsDeleted, 0L);
			RtuDataRiverEntity riverEntity = iRtuDataRiverService.getOne(riverwrapper);
			vo.setZ(riverEntity.getZ());
		} else if (rtuInfoEntity.getIsRes() == 1) {
			LambdaQueryWrapper<RtuDataRsvrEntity> rsvrWrapper = Wrappers.<RtuDataRsvrEntity>query().lambda();
			rsvrWrapper.eq(RtuDataRsvrEntity::getRtuCode, detail.getRtuCode());
			rsvrWrapper.eq(RtuDataRsvrEntity::getIsDeleted, 0L);
			RtuDataRsvrEntity rsvrEntity = iRtuDataRsvrService.getOne(rsvrWrapper);
			vo.setRz(rsvrEntity.getRz());
		} else if (rtuInfoEntity.getIsGround() == 1) {
//			LambdaQueryWrapper<RtuDataRainEntity> rainwrapper = Wrappers.<RtuDataRainEntity>query().lambda();
//			rainwrapper.eq(RtuDataRainEntity::getRtuCode,detail.getRtuCode());
//			rainwrapper.eq(RtuDataRainEntity::getIsDeleted,0L);
//			RtuDataRainEntity rainEntity=iRtuDataRainService.getOne(rainwrapper);
//			vo.setDrp(rainEntity.getDrp());
		}


		return R.data(vo);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入checkOrderInfoEntity")
	public R save(@RequestBody CheckOrderInfoEntity checkOrderInfoEntity) {
		List<DictBiz> statusdicts = DictBizCache.getList("rtu_check_order_status");
		for (DictBiz biz : statusdicts) {
			if (biz.getDictKey().equals("" + OrderStatusEnum.STATUS_CREATE.getCode())) {
				checkOrderInfoEntity.setOrderStatusKey(biz.getId());
				break;
			}
		}

		checkOrderService.save(checkOrderInfoEntity);
		OrderProcessInfoEntity processInfoEntity = new OrderProcessInfoEntity();
		processInfoEntity.setOrderId(checkOrderInfoEntity.getId());
		processInfoEntity.setOrderStatusKey(checkOrderInfoEntity.getOrderStatusKey());
		processInfoEntity.setProcessDesc(checkOrderInfoEntity.getOrderDesc());
		return R.status(processService.save(processInfoEntity));
	}

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

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

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

	/**
	 * 工单关闭
	 *
	 * @param checkOrderInfoEntity
	 * @return
	 */
	@PostMapping("/close")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "工单ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "orderDesc", value = "处理意见", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "工单关闭", notes = "传入checkOrderInfoEntity")
	public R orderClose(@ApiIgnore CheckOrderInfoEntity checkOrderInfoEntity) {
		BladeUser user = AuthUtil.getUser();
		List<DictBiz> statusdicts = dictBizService.getList("rtu_check_order_status");
		for (DictBiz biz : statusdicts) {
			if (biz.getDictKey().equals("" + OrderStatusEnum.STATUS_CLOSE.getCode())) {
				checkOrderInfoEntity.setOrderStatusKey(biz.getId());
				break;
			}
		}
		CheckOrderInfoEntity detail = this.checkOrderService.getById(checkOrderInfoEntity.getId());
		if (null == detail || detail.getOrderClose() == OrderCloseEnum.ACTIVE_CLOSE.getCode()) {
			return R.status(false);
		}
		detail.setOrderStatusKey(checkOrderInfoEntity.getOrderStatusKey());
		detail.setOrderClose(OrderCloseEnum.ACTIVE_CLOSE.getCode());
		detail.setUpdateTime(new Date());
		detail.setUpdateUser(user.getUserId());
		checkOrderService.updateById(detail);
		OrderProcessInfoEntity processInfoEntity = new OrderProcessInfoEntity();
		processInfoEntity.setOrderId(detail.getId());
		processInfoEntity.setOrderStatusKey(detail.getOrderStatusKey());
		processInfoEntity.setProcessDesc(checkOrderInfoEntity.getOrderDesc());
		processInfoEntity.setCreateUser(user.getUserId());
		processInfoEntity.setUpdateUser(user.getUserId());
		processService.save(processInfoEntity);
		if (null != detail.getWarnId()) {
			List<DictBiz> orderFromDict = dictBizService.getList("rtu_check_order_from");
			for (DictBiz biz : orderFromDict) {
				if (biz.getDictKey().equals(OrderFromEnum.FROM_WARNING.getCode())) {
					RtuWarningInfoEntity warningInfoEntity = this.warningService.getById(detail.getWarnId());
					if (null != warningInfoEntity) {
						warningInfoEntity.setWarningRecoveryTime(new Date());
						warningInfoEntity.setWarningStatus(WarningStatusEnum.STATUS_CLOSE.getCode());
						warningInfoEntity.setUpdateTime(new Date());
						warningInfoEntity.setUpdateUser(user.getUserId());
						this.warningService.updateById(warningInfoEntity);
					}
					break;
				}
			}
		}
		RtuStatusEntity rtuStatusEntity = new RtuStatusEntity();
		rtuStatusEntity.setRunStatusId(EquipmentStatusEnum.STATUS_RUN.getCode());
		this.manageService.updateRtuStatus(rtuStatusEntity);
		return R.success("工单关闭已操作成功！");
	}


	/**
	 * 查询最新的数据
	 */
	@GetMapping("/last/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<CheckOrderInfoVO>> lastList() {
		LambdaQueryWrapper<CheckOrderInfoEntity> wrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
		wrapper.orderByDesc(CheckOrderInfoEntity::getUpdateTime);
		wrapper.last("limit 1");
		List<CheckOrderInfoEntity> pages = checkOrderService.list(wrapper);
		return R.data(RtuCheckOrderWrapper.build().listVO(pages));
	}


	/**
	 * 工单确认
	 *
	 * @param checkOrderInfoEntity
	 * @return
	 */
	@PostMapping("/confirm")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "工单确认", notes = "传入checkOrderInfoEntity")
	public R orderConfirm(@RequestBody CheckOrderInfoEntity checkOrderInfoEntity) {
		List<DictBiz> statusdicts = DictBizCache.getList("rtu_check_order_status");
		for (DictBiz biz : statusdicts) {
			if (biz.getDictKey().equals("" + OrderStatusEnum.STATUS_CONFIRM.getCode())) {
				checkOrderInfoEntity.setOrderStatusKey(biz.getId());
				break;
			}
		}
		BladeUser user = AuthUtil.getUser();
		User userInfo = userService.getById(user.getUserId());

		CheckOrderInfoEntity detail = this.checkOrderService.getById(checkOrderInfoEntity.getId());
		if (null == detail || null != detail.getOrderConfirmUser()) {
			return R.status(false);
		}

		detail.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
		detail.setOrderStatusKey(checkOrderInfoEntity.getOrderStatusKey());
		detail.setOrderConfirmUser(user.getUserId());
		detail.setOrderConfirmTime(new Date());
		detail.setUpdateTime(new Date());
		checkOrderService.updateById(detail);

		OrderProcessInfoEntity processInfoEntity = new OrderProcessInfoEntity();
		processInfoEntity.setOrderId(checkOrderInfoEntity.getId());
		processInfoEntity.setOrderStatusKey(checkOrderInfoEntity.getOrderStatusKey());
		processInfoEntity.setProcessDesc("工单确认: " + userInfo.getRealName());
		return R.status(processService.save(processInfoEntity));
	}


	/**
	 * 工单统计
	 *
	 * @return
	 */
	@GetMapping("/statistics")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "统计表格")
	public R<List<CheckOrderCountVO>> orderStatistics() {
		CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			checkOrderInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			checkOrderInfoDTO.setOrgId(null);
		}
		List<CheckOrderCountVO> pages = checkOrderService.orderReportCount(checkOrderInfoDTO);
		return R.data(pages);
	}

	/**
	 * 按机构统计工单
	 *
	 * @return
	 */
	@GetMapping("/org/statistics")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "统计表格")
	public R<List<OrgCheckOrderCountVO>> orgOrderStatistics() {
		List<OrgCheckOrderCountVO> list = new LinkedList<>();
		BladeUser user = AuthUtil.getUser();
		List<Dept> root = this.deptService.getDeptChild(Func.toLong(user.getDeptId()));

		for (Dept dept : root) {
			OrgCheckOrderCountVO orgCheckOrderCountVO = new OrgCheckOrderCountVO();
			orgCheckOrderCountVO.setId(dept.getId());
			orgCheckOrderCountVO.setOrgName(dept.getDeptName());

			CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();

			checkOrderInfoDTO.setOrgId(Func.toLong(dept.getId()));

			Long orderCount = checkOrderService.orderCount(checkOrderInfoDTO);

			checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
			Long unCloseOrderCount = checkOrderService.unCloseOrderCount(checkOrderInfoDTO);


			orgCheckOrderCountVO.setOrderCount(orderCount);
			orgCheckOrderCountVO.setProcessingOrderCount(unCloseOrderCount);


			list.add(orgCheckOrderCountVO);
		}


		return R.data(list);
	}

}
