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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
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.OrderCloseEnum;
import org.springblade.enums.OrderConfirmEnum;
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.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.business.check.process.wrapper.RtuOrderProcessWrapper;
import org.springblade.modules.business.desk.entity.NoticeViewEntity;
import org.springblade.modules.business.desk.service.INoticeService;
import org.springblade.modules.business.desk.service.INoticeViewService;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionRtuEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanRtuService;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.equipment.inspection.report.service.IEquipmentInspectionReportService;
import org.springblade.modules.business.rtu.base.entity.RtuInfoEntity;
import org.springblade.modules.business.rtu.base.service.IRtuBaseInfoService;
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.business.warning.vo.RtuWarningInfoVO;
import org.springblade.realData.VO.RealDataHomeInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * Date:2022/8/27
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/real/data")
@AllArgsConstructor
@Api(value = "实时数据管理模块", tags = "实时数据管理模块")
public class RealDataController {
	@Autowired
	private RedisTemplate redisTemplate;

	private final IRtuCheckOrderService checkOrderService;
	private final IEquipmentInspectionPlanService planService;
	private final IEquipmentInspectionPlanRtuService planRtuService;
	private final IEquipmentInspectionReportService reportService;
	private final INoticeService noticeService;
	private final INoticeViewService noticeViewService;
	private final IRtuBaseInfoService rtuBaseInfoService;
	private final IRtuWarningService rtuWarningService;

	@GetMapping("/home/info")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R<RealDataHomeInfoVO> detail() {
		//HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
		RealDataHomeInfoVO infoVO = new RealDataHomeInfoVO();
//		if (opsHash.hasKey("real_data_info","rtus")){
//			infoVO.setRtus(Integer.parseInt(opsHash.get("real_data_info","rtus")));
//		}else{
//			infoVO.setRtus(0);
//		}
//		if (opsHash.hasKey("real_data_info","warningRtus")){
//			infoVO.setWarningRtus(Integer.parseInt(opsHash.get("real_data_info","warningRtus")));
//		}else{
//			infoVO.setWarningRtus(0);
//		}
//		if (opsHash.hasKey("real_data_info","delayOrders")){
//			infoVO.setDelayOrders(Integer.parseInt(opsHash.get("real_data_info","delayOrders")));
//		}else{
//			infoVO.setDelayOrders(0);
//		}
//		if (opsHash.hasKey("real_data_info","equipmentInspectionCount")){
//			infoVO.setEquipmentInspectionCount(Integer.parseInt(opsHash.get("real_data_info","equipmentInspectionCount")));
//		}else{
//			infoVO.setEquipmentInspectionCount(0);
//		}


		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			LambdaQueryWrapper<RtuInfoEntity> rtuwrapper = Wrappers.<RtuInfoEntity>query().lambda();
			rtuwrapper.eq(RtuInfoEntity::getOrgId, user.getDeptId());
			rtuwrapper.eq(RtuInfoEntity::getIsDeleted,0);
			long rtuCount = rtuBaseInfoService.count(rtuwrapper);
			infoVO.setRtus((int) rtuCount);

			RtuWarningInfoDTO rtuWarningInfoDTO = new RtuWarningInfoDTO();
			rtuWarningInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
			long warningRtuCount = rtuWarningService.warningRtuCount(rtuWarningInfoDTO);
			infoVO.setWarningRtus((int)warningRtuCount);

			LambdaQueryWrapper<CheckOrderInfoEntity> wrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();

			wrapper.eq(CheckOrderInfoEntity::getOrderConfirmUser, user.getUserId());
			wrapper.eq(CheckOrderInfoEntity::getOrderConfirm, OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
			wrapper.eq(CheckOrderInfoEntity::getOrderClose, OrderCloseEnum.ACTIVE_OPEN.getCode());
			wrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			long count = checkOrderService.count(wrapper);
			infoVO.setMyOrderCount(count);

			wrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			wrapper.eq(CheckOrderInfoEntity::getOrderConfirm, OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
			wrapper.eq(CheckOrderInfoEntity::getOrderClose, OrderCloseEnum.ACTIVE_OPEN.getCode());
			wrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			 count = checkOrderService.count(wrapper);
			infoVO.setProcessingOrderCount(count);

			CheckOrderInfoDTO orderInfoDTO = new CheckOrderInfoDTO();
			orderInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
			orderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
			long unconfirmOrderCount = checkOrderService.unconfirmOrderCount(orderInfoDTO);
			infoVO.setUnconfirmOrderCount(unconfirmOrderCount);

			EquipmentInspectionPlanEntity planEntity = planService.yearPlan();
			Long planInspectionRtus = planRtuService.yearPlanEquipmentInspectionCount(Func.toLong(user.getDeptId()));
			Long reportRtus = reportService.yearPlanEquipmentInspectionReportCount(planEntity.getId(), Func.toLong(user.getDeptId()));
			infoVO.setEquipmentInspectionCount(planInspectionRtus);
			infoVO.setMyInspectionCount(reportRtus);

			String equipmentInspectionPercent = String.format("%.02f", (double) reportRtus / (double) planInspectionRtus * (double) 100);
			infoVO.setEquipmentInspectionPercent(equipmentInspectionPercent);
		} else {
			LambdaQueryWrapper<RtuInfoEntity> rtuwrapper = Wrappers.<RtuInfoEntity>query().lambda();
			rtuwrapper.eq(RtuInfoEntity::getIsDeleted,0);
			long rtuCount = rtuBaseInfoService.count(rtuwrapper);
			infoVO.setRtus((int) rtuCount);

			RtuWarningInfoDTO rtuWarningInfoDTO = new RtuWarningInfoDTO();
			long warningRtuCount = rtuWarningService.warningRtuCount(rtuWarningInfoDTO);
			infoVO.setWarningRtus((int)warningRtuCount);

			CheckOrderInfoDTO orderInfoDTO = new CheckOrderInfoDTO();
			orderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
			long unconfirmOrderCount = checkOrderService.unconfirmOrderCount(orderInfoDTO);
			infoVO.setUnconfirmOrderCount(unconfirmOrderCount);
			EquipmentInspectionPlanEntity planEntity = planService.yearPlan();
			Long planInspectionRtus = planRtuService.yearPlanEquipmentInspectionCount(null);
			Long reportRtus = reportService.yearPlanEquipmentInspectionReportCount(planEntity.getId(), null);
			infoVO.setEquipmentInspectionCount(planInspectionRtus);

			String equipmentInspectionPercent = String.format("%.02f", (double) reportRtus / (double) planInspectionRtus * (double) 100);
			infoVO.setEquipmentInspectionPercent(equipmentInspectionPercent);
		}

		LambdaQueryWrapper<NoticeViewEntity> noticewrapper = Wrappers.<NoticeViewEntity>query().lambda();
		noticewrapper.eq(NoticeViewEntity::getCreateUser, user.getUserId());
		noticewrapper.eq(NoticeViewEntity::getIsDeleted, 0);
		NoticeViewEntity entity = noticeViewService.getOne(noticewrapper);
		if (null != entity) {
			Long count = noticeService.unviewNoticeCount(entity.getViewTime());
			if (count > 0) {
				infoVO.setHasNewNotice(true);
				infoVO.setNewNoticeCount(count);
			} else {
				infoVO.setHasNewNotice(false);
				infoVO.setNewNoticeCount(0L);
			}
		} else {
			infoVO.setHasNewNotice(true);
			infoVO.setNewNoticeCount(0L);
		}

		return R.data(infoVO);
	}
}
