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

import cn.hutool.db.Page;
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.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.IsValidEnum;
import org.springblade.enums.RtuReportStatusEnum;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.desk.entity.NoticeEntity;
import org.springblade.modules.business.desk.wrapper.NoticeWrapper;
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.wrapper.EquipmentInspectionWrapper;
import org.springblade.modules.business.equipment.inspection.plan.dto.EquipmentInspectionRtuDTO;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
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.plan.vo.EquipmentInspectionPlanVO;
import org.springblade.modules.business.equipment.inspection.plan.vo.EquipmentInspectionRtuVO;
import org.springblade.modules.business.equipment.inspection.plan.wrapper.EquipmentInspectionPlanWrapper;
import org.springblade.modules.business.equipment.inspection.report.entity.EquipmentInspectionReportEntity;
import org.springblade.modules.business.equipment.inspection.report.service.IEquipmentInspectionReportService;
import org.springblade.modules.business.project.base.entity.ProjectInfoEntity;
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.wrapper.RtuInfoWrapper;
import org.springblade.modules.business.rtumanage.entity.TreeNodeEntity;
import org.springblade.modules.system.entity.Dept;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/***
 * Date:2022/8/23
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/equipment/inspection/plan")
@AllArgsConstructor
@Api(value = "设备巡检计划管理模块", tags = "设备巡检计划管理模块")
public class EquipmentInspectionPlanController extends BladeController {
	private final IEquipmentInspectionPlanService planService;
	private final IEquipmentInspectionPlanRtuService planRtuService;
	private final IRtuBaseInfoService rtuBaseInfoService;
	private final IEquipmentInspectionReportService reportService;


	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "inspectionId", value = "巡检年度ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionPlanVO>> list(@ApiIgnore @RequestParam Map<String, Object> planEntity, Query query) {
		EquipmentInspectionPlanWrapper.build().planQuery(planEntity);
		IPage<EquipmentInspectionPlanEntity> pages = planService.page(Condition.getPage(query), Condition.getQueryWrapper(planEntity, EquipmentInspectionPlanEntity.class));
		return R.data(EquipmentInspectionPlanWrapper.build().pageVO(pages));
	}

	/**
	 * 分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionPlanVO>> page(Query query) {
		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
		wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0L);
		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getInspectionStartTime);

		IPage<EquipmentInspectionPlanEntity> pages = planService.page(Condition.getPage(query), wrapper);
		return R.data(EquipmentInspectionPlanWrapper.build().pageVO(pages));
	}

	/**
	 * 不分页列表
	 */
	@GetMapping("/all/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<EquipmentInspectionPlanVO>> allPlan() {
		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
		wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0L);
		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getInspectionStartTime);
		List<EquipmentInspectionPlanEntity> list = planService.list(wrapper);
		return R.data(EquipmentInspectionPlanWrapper.build().listVO(list));
	}


	/**
	 * 列表，不分页
	 *
	 * @return
	 */
	@GetMapping("/rtu/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "orgId", value = "机构ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "projectId", value = "项目ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "planId", value = "计划ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionRtuVO>> rtuPage(@ApiIgnore EquipmentInspectionRtuDTO rtuDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {

		} else {
		     if (user.getDeptId().equals(""+rtuDTO.getOrgId())){
				 rtuDTO.setOrgId(null);
				 rtuDTO.setProjectId(null);
			 }
		}

		IPage<EquipmentInspectionRtuVO> page = planRtuService.selectPage(Condition.getPage(query), rtuDTO);
		List<EquipmentInspectionRtuVO> rtuInfoVOList = page.getRecords();
		for (EquipmentInspectionRtuVO vo : rtuInfoVOList) {
			if (null != vo.getReportTime()) {
				vo.setReportStatus(RtuReportStatusEnum.STATUS_1.getCode());
			} else {
				vo.setReportStatus(RtuReportStatusEnum.STATUS_0.getCode());
			}
		}
		return R.data(page);
	}

	/**
	 * 巡检任务
	 * @param rtuDTO
	 * @param query
	 * @return
	 */
	@GetMapping("/my/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionRtuVO>> myRtuPage(@ApiIgnore EquipmentInspectionRtuDTO rtuDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			rtuDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
//			if (user.getDeptId().equals(""+rtuDTO.getOrgId())){
//				rtuDTO.setOrgId(null);
//				rtuDTO.setProjectId(null);
//			}
		}
		EquipmentInspectionPlanEntity planEntity= planService.yearPlan();
		rtuDTO.setPlanId(planEntity.getId());
		IPage<EquipmentInspectionRtuVO> page = planRtuService.myPage(Condition.getPage(query), rtuDTO);
		return R.data(page);
	}


	/**
	 * 查询当前年度巡检计划
	 */
	@GetMapping("/list/year")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "projectId", value = "项目ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表")
	public R<IPage<EquipmentInspectionPlanVO>> currentYearList(EquipmentInspectionPlanEntity planEntity, Query query) {

		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>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(EquipmentInspectionPlanEntity::getOrgId, user.getDeptId());
//		}
		if (planEntity.getPlanTitle() != null && planEntity.getPlanTitle().length() > 0) {
			wrapper.like(EquipmentInspectionPlanEntity::getPlanTitle, planEntity.getPlanTitle());
		}
		if (planEntity.getProjectId() != null) {
			wrapper.eq(EquipmentInspectionPlanEntity::getProjectId, planEntity.getProjectId());
		}


		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
		//wrapper.last("limit 1");



		IPage<EquipmentInspectionPlanEntity> pages = planService.page(Condition.getPage(query), wrapper);
		return R.data(EquipmentInspectionPlanWrapper.build().pageVO(pages));
	}

	@GetMapping("/last")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入orderProcessInfoEntity")
	public R<EquipmentInspectionPlanVO> lastPlan(EquipmentInspectionPlanEntity planEntity) {
		EquipmentInspectionPlanEntity detail = planService.getOne(Condition.getQueryWrapper(planEntity));
		return R.data(EquipmentInspectionPlanWrapper.build().entityVO(detail));
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入orderProcessInfoEntity")
	public R<EquipmentInspectionPlanVO> detail(EquipmentInspectionPlanEntity planEntity) {
		EquipmentInspectionPlanEntity detail = planService.getOne(Condition.getQueryWrapper(planEntity));
		return R.data(EquipmentInspectionPlanWrapper.build().entityVO(detail));
	}


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

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

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

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