/**
 * 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.fitting.controller;

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.fitting.pojo.dto.OperationOrderMaterialDTO;
import org.springblade.modules.fitting.pojo.vo.AddOrderFittingUseVo;
import org.springblade.modules.fitting.pojo.vo.OrderUseFittingDetailVo;
import org.springblade.modules.fitting.service.IPlatformFittingApplyService;
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.fitting.pojo.entity.OperationOrderMaterialEntity;
import org.springblade.modules.fitting.pojo.vo.OperationOrderMaterialVO;
import org.springblade.modules.fitting.excel.OperationOrderMaterialExcel;
import org.springblade.modules.fitting.wrapper.OperationOrderMaterialWrapper;
import org.springblade.modules.fitting.service.IOperationOrderMaterialService;
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.HashMap;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 工单备件使用表 控制器
 *
 * @author BladeX
 * @since 2024-08-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-operationOrderMaterial/operationOrderMaterial")
@Tag(name = "工单备件使用表", description = "工单备件使用表接口")
public class OperationOrderMaterialController extends BladeController {

	private final IOperationOrderMaterialService operationOrderMaterialService;

	private final IPlatformFittingApplyService fittingApplyService;

	/**
	 * 工单备件使用表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入operationOrderMaterial")
	public R<OperationOrderMaterialVO> detail(OperationOrderMaterialEntity operationOrderMaterial) {
		OperationOrderMaterialEntity detail = operationOrderMaterialService.getOne(Condition.getQueryWrapper(operationOrderMaterial));
		return R.data(OperationOrderMaterialWrapper.build().entityVO(detail));
	}

	/**
	 * 工单物料申请、物料使用列表
	 */
	@GetMapping("/materialListRelateOrder")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "工单物料申请、物料使用列表", description  = "传入orderId 工单id")
	public R materialListRelateOrder(String orderId) {
        List<OperationOrderMaterialDTO> userFitting=operationOrderMaterialService.getOrderUserdFitting(orderId);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("useFitting",userFitting);
        List<OperationOrderMaterialDTO>  applyFitting=operationOrderMaterialService.getOrderApplyFitting(orderId);
		resultMap.put("applyFitting",applyFitting);
		return R.data(resultMap);
	}

	/**
	 * 物料使用删除
	 */
	@GetMapping("deleteUserFitting")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "物料使用删除", description  = "传入物料使用的id")
	public R deleteUserFitting(String id){
		return R.status(operationOrderMaterialService.deleteUserFitting(id));
	}

	/**
	 * 物料申请删除
	 */
	@GetMapping("deleteApplyFitting")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "物料申请删除", description  = "传入物料申请的id")
	public R deleteApplyFitting(String id){
		return R.status(fittingApplyService.deleteApplyFitting(id));
	}

	/**
	 * 工单备件使用表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入operationOrderMaterial")
	public R<IPage<OperationOrderMaterialVO>> page(OperationOrderMaterialVO operationOrderMaterial, Query query) {
		IPage<OperationOrderMaterialVO> pages = operationOrderMaterialService.selectOperationOrderMaterialPage(Condition.getPage(query), operationOrderMaterial);
		return R.data(pages);
	}

	/**
	 * 工单备件使用,使用扣的都是工程师的库存
	 */
	@PostMapping("/orderFittingUse")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "工单备件使用", description  = "传入operationOrderMaterial")
	public R orderFittingUse(@Valid @RequestBody AddOrderFittingUseVo fittingUseVo) {
		return R.status(operationOrderMaterialService.orderFittingUse(fittingUseVo));
	}

	/**
	 * 工程师已用物料列表
	 */
	@GetMapping("getEmpUseFittingList")
    @ApiOperationSupport(order = 8)
	@Operation(summary = "工程师已用物料列表", description  = "smallConditon 可以作为查询条件，engineerId 工程师 userId")
	public R getEmpUseFittingList(@RequestParam Map<String,String> params, Query query){
		Map<String,Object> resultMap=new HashMap<>();
		List<OperationOrderMaterialEntity> list=operationOrderMaterialService.getEmpUseFittingList(Condition.getPage(query),params);
		resultMap.put("records",list);
		return R.data(resultMap);
	}
	/**
	 * 工程师已用物料详情
	 */
	@GetMapping("getOrderUseFittingDetail")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "工程师已用物料详情")
	public R<OrderUseFittingDetailVo> getOrderUseFittingDetail(String id){
		OrderUseFittingDetailVo useFittingDetailVo= operationOrderMaterialService.getOrderUseFittingDetail(id);
		return R.data(useFittingDetailVo);
	}

	/**
	 * 工程师可用物料详情
	 */
	@GetMapping("getOrderCanUseFittingDetail")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "工程师可用物料详情")
	public R<OrderUseFittingDetailVo> getOrderCanUseFittingDetail(String materialId,String empUserId){
		OrderUseFittingDetailVo useFittingDetailVo= operationOrderMaterialService.getOrderCanUseFittingDetail(materialId,empUserId);
		return R.data(useFittingDetailVo);
	}

}
