/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/23
 */
package org.springblade.modules.business.equipment.inspection.report.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.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.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.check.order.wrapper.RtuCheckOrderWrapper;
import org.springblade.modules.business.check.process.dto.OrderProcessInfoDTO;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
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.equipment.inspection.plan.wrapper.EquipmentInspectionPlanWrapper;
import org.springblade.modules.business.equipment.inspection.report.dto.EquipmentInspectionReportDTO;
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.equipment.inspection.report.vo.EquipmentInspectionReportVO;
import org.springblade.modules.business.equipment.inspection.report.wrapper.EquipmentInspectionReportWrapper;
import org.springblade.modules.business.rtu.base.entity.RtuInfoEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

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

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "planId", value = "巡检计划ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionReportVO>> list(@ApiIgnore @RequestParam Map<String, Object> reportEntity, Query query) {
		EquipmentInspectionReportWrapper.build().reportQuery(reportEntity);
		IPage<EquipmentInspectionReportEntity> pages = reportService.page(Condition.getPage(query), Condition.getQueryWrapper(reportEntity, EquipmentInspectionReportEntity.class));
		return R.data(EquipmentInspectionReportWrapper.build().pageVO(pages));
	}

	/**
	 * 分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "planId", value = "巡检计划ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionReportVO>> page(@ApiIgnore EquipmentInspectionReportDTO reportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			reportDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			reportDTO.setOrgId(null);
		}
		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
		wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0L);
		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getInspectionStartTime);
		List<EquipmentInspectionPlanEntity> planEntities = planService.list(wrapper);
		if (null != planEntities && planEntities.size() > 0) {
			Long planId = planEntities.get(0).getId();
			reportDTO.setPlanId(planId);
		}
		IPage<EquipmentInspectionReportVO> pages = reportService.selectPage(Condition.getPage(query), reportDTO);
		List<EquipmentInspectionReportVO> list = pages.getRecords();
		for (EquipmentInspectionReportVO v : list) {
			EquipmentInspectionReportWrapper.build().toVoInfo(v);
		}
		return R.data(pages);
	}

	/**
	 * 查询我填报的巡检信息
	 */
	@GetMapping("/my/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionReportVO>> myPage(@ApiIgnore EquipmentInspectionReportDTO reportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		EquipmentInspectionPlanEntity planEntity = planService.yearPlan();
		reportDTO.setPlanId(planEntity.getId());
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
			reportDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else if (BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			reportDTO.setOrgId(Func.toLong(user.getDeptId()));
			reportDTO.setCreateUser(user.getUserId());
		} else {
			reportDTO.setOrgId(null);
		}

		IPage<EquipmentInspectionReportVO> pages = reportService.selectPage(Condition.getPage(query), reportDTO);
		return R.data(pages);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入orderProcessInfoEntity")
	public R<EquipmentInspectionReportVO> detail(EquipmentInspectionReportEntity reportEntity) {
		EquipmentInspectionReportEntity detail = reportService.getOne(Condition.getQueryWrapper(reportEntity));
		return R.data(EquipmentInspectionReportWrapper.build().entityVO(detail));
	}

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

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

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

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

	/**
	 * 查询当天的巡检填报信息
	 */
	@GetMapping("/today/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查询")
	public R<List<EquipmentInspectionReportVO>> todayList() {
		BladeUser user = AuthUtil.getUser();
		EquipmentInspectionReportDTO reportDTO = new EquipmentInspectionReportDTO();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			reportDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			reportDTO.setOrgId(null);
		}
		Date date = new Date();
		String dt = Func.formatDateTime(date);
		reportDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
		EquipmentInspectionPlanEntity planEntity = planService.yearPlan();
		reportDTO.setPlanId(planEntity.getId());
		List<EquipmentInspectionReportVO> list = reportService.selectTodayReportList(reportDTO);
		return R.data(list);
	}

}
