/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/23
 */
package org.springblade.modules.business.equipment.inspection.base.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 io.swagger.annotations.ApiParam;
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.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.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.vo.CheckOrderCountVO;
import org.springblade.modules.business.check.order.vo.OrgCheckOrderCountVO;
import org.springblade.modules.business.equipment.inspection.base.dto.EquipmentInspectionInfoDTO;
import org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity;
import org.springblade.modules.business.equipment.inspection.base.service.IEquipmentInspectionService;
import org.springblade.modules.business.equipment.inspection.base.vo.EquipmentInspectionInfoVO;
import org.springblade.modules.business.equipment.inspection.base.vo.EquipmentInspectionStatisticsVO;
import org.springblade.modules.business.equipment.inspection.base.vo.OrgEquipmentInspectionStatisticsVO;
import org.springblade.modules.business.equipment.inspection.base.wrapper.EquipmentInspectionWrapper;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.rtu.base.entity.RtuInfoEntity;
import org.springblade.modules.business.rtu.base.service.IRtuBaseInfoService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.*;

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

/***
 * Date:2022/8/23
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/equipment/inspection")
@AllArgsConstructor
@Api(value = "设备巡检管理模块", tags = "设备巡检管理模块")
public class EquipmentInspectionController extends BladeController {
	private final IEquipmentInspectionService equipmentInspectionService;
	private final IEquipmentInspectionPlanService planService;
	private final IDeptService deptService;
	private final IRtuBaseInfoService rtuBaseInfoService;
	/**
	 * 全部列表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "全部列表，不分页")
	public R<List<EquipmentInspectionInfoVO>> list() {
		List<EquipmentInspectionInfoEntity> pages = equipmentInspectionService.list();
		return R.data(EquipmentInspectionWrapper.build().listVO(pages));
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入orderProcessInfoEntity")
	public R<EquipmentInspectionInfoVO> detail(EquipmentInspectionInfoEntity equipmentInspectionInfoEntity) {
		EquipmentInspectionInfoEntity detail = equipmentInspectionService.getOne(Condition.getQueryWrapper(equipmentInspectionInfoEntity));
		return R.data(EquipmentInspectionWrapper.build().entityVO(detail));
	}


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

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

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

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

	/**
	 * 巡检统计
	 *
	 * @return
	 */
	@GetMapping("/statistics")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "统计表格")
	public R<List<EquipmentInspectionStatisticsVO>> inspectionStatistics() {
		EquipmentInspectionInfoDTO inspectionInfoDTO = new EquipmentInspectionInfoDTO();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			inspectionInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			inspectionInfoDTO.setOrgId(null);
		}
		EquipmentInspectionPlanEntity planEntity = planService.yearPlan();
		inspectionInfoDTO.setPlanId(planEntity.getId());
		List<EquipmentInspectionStatisticsVO> pages = equipmentInspectionService.inspectionReportCount(inspectionInfoDTO);
		return R.data(pages);
	}


	/**
	 * 巡检统计
	 *
	 * @return
	 */
	@GetMapping("/org/statistics")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "统计表格")
	public R<List<OrgEquipmentInspectionStatisticsVO>> orgInspectionStatistics() {
		List<OrgEquipmentInspectionStatisticsVO> list = new LinkedList<>();
		BladeUser user = AuthUtil.getUser();
		List<Dept> root = this.deptService.getDeptChild(Func.toLong(user.getDeptId()));

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

			LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
			wrapper.eq(RtuInfoEntity::getIsDeleted,0);
			wrapper.eq(RtuInfoEntity::getOrgId,dept.getId());
			Long rtuCount =rtuBaseInfoService.count(wrapper);
			orgEquipmentInspectionStatisticsVO.setRtuCount(rtuCount);

			EquipmentInspectionPlanEntity planEntity = planService.yearPlan();
			EquipmentInspectionInfoDTO inspectionInfoDTO = new EquipmentInspectionInfoDTO();
			inspectionInfoDTO.setPlanId(planEntity.getId());
			inspectionInfoDTO.setOrgId(dept.getId());

			Long inspectionCount = equipmentInspectionService.inspectionCount(inspectionInfoDTO);

			orgEquipmentInspectionStatisticsVO.setCompleteCount(inspectionCount);




			list.add(orgEquipmentInspectionStatisticsVO);
		}


		return R.data(list);
	}

}
