/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.order.controller;

import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import jakarta.validation.Valid;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.order.pojo.vo.ExamineOrderVo;
import org.springblade.modules.order.pojo.vo.OrderHomeVo;
import org.springblade.modules.order.pojo.vo.OrderListConditionVo;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.order.pojo.entity.OperationOrderEntity;
import org.springblade.modules.order.pojo.vo.OperationOrderVO;
import org.springblade.modules.order.excel.OperationOrderExcel;
import org.springblade.modules.order.wrapper.OperationOrderWrapper;
import org.springblade.modules.order.service.IOperationOrderService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 工单基础信息表 控制器
 *
 * @author lzp
 * @since 2024-06-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("sf/operationOrder")
@Tag(name = "工单基础信息表", description = "工单基础信息表接口")
public class OperationOrderController extends BladeController {

	private final IOperationOrderService operationOrderService;

	/**
	 * 工单基础信息表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入id" )
	public R<OperationOrderVO> detail(@Parameter(name = "id",description = "主键id",required = true) @RequestParam String id) {
		OperationOrderVO detail = operationOrderService.findOrderDetail(id);
		return R.data(detail);
	}

	/**
	 * 工单基础信息表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入operationOrder")
	public R<IPage<OperationOrderVO>> page(OrderListConditionVo conditionVo, Query query) {
		IPage<OperationOrderVO> pages = operationOrderService.selectOperationOrderPage(Condition.getPage(query), conditionVo);
		return R.data(pages);
	}

	/**
	 * 工单基础信息表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入operationOrder")
	public R save(@Valid @RequestBody OperationOrderEntity operationOrder) {
		return operationOrderService.saveOrder(operationOrder);
	}

	/**
	 * 工单基础信息表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入operationOrder")
	public R update(@Valid @RequestBody OperationOrderEntity operationOrder) {
		return R.status(operationOrderService.updateOrder(operationOrder));
	}


	/**
	 *  无效/删除工单
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "删除/无效", description  = "传入id")
	public R remove(@Parameter(name = "id",description = "主键id",required = true) @RequestParam String id,
					@Parameter(name = "status", description = " 6.无效工单 7.删除",required = true) @RequestParam String status,
					@Parameter(name="disableResource",description = "无效原因") String disableResource) {
		return R.status(operationOrderService.deleteLogic(id,status,disableResource));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-operationOrder")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入operationOrder")
	public void exportOperationOrder(@Parameter(hidden = true) @RequestParam Map<String, Object> operationOrder, BladeUser bladeUser, HttpServletResponse response) {

		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(OperationOrder::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(OperationOrderEntity::getsta, BladeConstant.DB_NOT_DELETED);
		List<OperationOrderExcel> list = operationOrderService.exportOperationOrder(operationOrder);
		ExcelUtil.export(response, "工单基础信息表数据" + DateUtil.time(), "工单基础信息表数据表", list, OperationOrderExcel.class);
	}
	/**
	 * 小程序首页统计
	*/
	@GetMapping("/orderHomeIndex")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "小程序首页统计", description  = "无参数" )
	public R<OrderHomeVo> orderHomeIndex() {
		return R.data(operationOrderService.orderHomeIndex());
	}


	@PostMapping("/turnMainOrder")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "电站转运维", description  = "传入id")
	public R turnMainOrder(@Parameter(name = "id",description = "id",required = true) @RequestParam String id) {

		return R.status(operationOrderService.turnMainOrder(id));
	}
	/**
	 *  审核工单
	 */
	@PostMapping("/toExamineOrder")
	@Parameters({
		@Parameter(name = "id", description = "主键id",required = true),
		@Parameter(name = "callbackResult", description = "审核结果 1已完成、2仍需上门",required = true),
		@Parameter(name = "memoNote", description = "审核备注")
	})
	@ApiOperationSupport(order = 16)
	@Operation(summary = "审核工单", description  = "传入id")
	public R toExamineOrder(@Valid @RequestBody ExamineOrderVo exc ) {
		return R.status(operationOrderService.toExamineOrder(exc));
	}
}
