/**
 * 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.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.fitting.pojo.dto.BhMaterialDTO;
import org.springblade.modules.fitting.pojo.vo.*;
import org.springblade.modules.fitting.pojo.dto.ApplyDto;
import org.springblade.modules.fitting.pojo.vo.AddFittingApplyVo;
import org.springblade.modules.fitting.pojo.vo.FittingApplyDetailVo;
import org.springblade.modules.fitting.pojo.vo.SiteToExamineVo;
import org.springblade.modules.material.pojo.dto.MaterialDTO;
import org.springblade.modules.material.pojo.entity.MaterialEntity;
import org.springblade.modules.material.service.impl.MaterialServiceImpl;
import org.springblade.modules.system.pojo.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
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.PlatformFittingApplyEntity;
import org.springblade.modules.fitting.excel.PlatformFittingApplyExcel;
import org.springblade.modules.fitting.wrapper.PlatformFittingApplyWrapper;
import org.springblade.modules.fitting.service.IPlatformFittingApplyService;
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-08-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-platformFittingApply/platformFittingApply")
@Tag(name = "服务商向平台备件申请领用记录表", description = "服务商向平台备件申请领用记录表接口")
public class PlatformFittingApplyController extends BladeController {

	private final IPlatformFittingApplyService platformFittingApplyService;

	@Autowired
	MaterialServiceImpl materialService;
	/**
	 * 服务商向平台备件申请领用记录表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "物料申请详情", description  = "传入platformFittingApply")
	public R<FittingApplyDetailVo> detail(String id) {
		FittingApplyDetailVo detailVo=platformFittingApplyService.getFittingApplyDetailById(id);
		return R.data(detailVo);
	}


	/**
	 * 服务商向平台备件申请领用记录表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入platformFittingApply")
	public R<IPage<PlatformFittingApplyEntity>> page(PlatformFittingApplyVO platformFittingApply, Query query) {
		IPage<PlatformFittingApplyEntity> pages = platformFittingApplyService.selectPlatformFittingApplyPage(Condition.getPage(query), platformFittingApply);
		return R.data(pages);
	}

	/**
	 * 服务商向平台备件申请领用记录表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增物料申请", description  = "传入platformFittingApply")
	public R save(@Valid @RequestBody AddFittingApplyVo addFittingApplyVo) {
		return R.status(platformFittingApplyService.saveFittingApply(addFittingApplyVo));
	}

	/**
	 * 服务商向平台备件申请领用记录表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改物料申请", description  = "传入platformFittingApply")
	public R update(@Valid @RequestBody AddFittingApplyVo addFittingApplyVo) {
		return R.status(platformFittingApplyService.editFittingApply(addFittingApplyVo));
	}

	/**
	 * 新增备货申请
	 */
     @PostMapping(value = "addfittingBh")
	 @ApiOperationSupport(order = 6)
	 @Operation(summary = "新增备货申请")
	 public R addfittingBh(@Valid @RequestBody AddBhVo addBhVo){
     	return R.status(platformFittingApplyService.fittingBh(addBhVo));
	 }

	/**
	 * 编辑备货申请
	 */
	 @PostMapping(value = "editFittingBh")
	 @ApiOperationSupport(order = 6)
	 @Operation(summary = "编辑备货申请")
	 public R editFittingBh(@Valid @RequestBody OperaAddFittingBatchVo batchVo){
		 return R.status(platformFittingApplyService.editFittingBh(batchVo));
	 }

	/**
	 * 删除物料申请
	 */
	@GetMapping(value = "deleteMaterialApply")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "删除物料申请", description  = "传入 applyId 申请的id")
	public R deleteMaterialApply(String applyId){
		return R.data(platformFittingApplyService.deleteApplyFitting(applyId));
	}

	/**
	 * 备货申请页面根据 物料编码 、物料名称 获取物料信息
	 */
	@GetMapping(value = "getMaterialListByCodeOrName")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "备货申请页面根据 物料编码 、物料名称 获取物料信息", description  = "")
	public R getMaterialListByCodeOrName(ChooseMaterialListVo conditonVo){
		List<BhMaterialDTO> list=platformFittingApplyService.getMaterialListByCodeOrName(conditonVo);
		return R.data(list);
	}


	/**
	 * 物料申请的时候，获取规格下拉
	 */
	@GetMapping(value = "getMaterialSpecList")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "物料申请页面，获取规格下拉")
	public R getMaterialSpecList(String materialType){
		List<String> specs=platformFittingApplyService.getMaterialSpecList(materialType);
		return R.data(specs);
	}

	/**
	 * 物料申请的时候，根据物料代码，检索物料信息
	 */
	@GetMapping(value = "getMaterialDetailByMaterialCode")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "根据物料代码，检索物料信息", description  = "传入platformFittingApply")
	public R getMaterialDetailByMaterialCode(String materialCode){
		MaterialDTO materialEntity=materialService.getMaterialDetailByCode(materialCode);
		return R.data(materialEntity);
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/export-platformFittingApply")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入platformFittingApply")
	public void exportPlatformFittingApply(@Parameter(hidden = true) @RequestParam Map<String, Object> platformFittingApply, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<PlatformFittingApplyEntity> queryWrapper = Condition.getQueryWrapper(platformFittingApply, PlatformFittingApplyEntity.class);

		//queryWrapper.lambda().eq(PlatformFittingApplyEntity::getSiteId, BladeConstant.DB_NOT_DELETED);
		List<PlatformFittingApplyExcel> list = platformFittingApplyService.exportPlatformFittingApply(queryWrapper);
		ExcelUtil.export(response, "服务商向平台备件申请领用记录表数据" + DateUtil.time(), "服务商向平台备件申请领用记录表数据表", list, PlatformFittingApplyExcel.class);
	}

	@GetMapping("/toExamine")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "一、二级审核", description  = "")
	@Parameters({
		@Parameter(name = "category", description = "公告类型", in = ParameterIn.QUERY, schema = @Schema(type = "integer")),
		@Parameter(name = "title", description = "公告标题", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
	})
	public R toExamine(@Parameter(name = "id",description = "主键id", required = true) @RequestParam String id, @Parameter(name = "remarks" ,description = "审核备注") @RequestParam String remarks, @Parameter(name = "type",description = "状态值 1.一级 2.二级", required = true) @RequestParam String type,@Parameter(name = "examineType",description = "审核类型，1.通过 2.驳回",required = true) String examineType) {
		//@Parameter(name = "主键id", required = true) @RequestParam String id,@Parameter(name = "状态值", required = true) @RequestParam String status)
		return R.status(platformFittingApplyService.toExamine(id, remarks, type,examineType));
	}

	/**
	 * 运维商审核
	*/
	@PostMapping("/siteToExamine")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "运维商审核", description  = "传入platformFittingApply")
	public R siteToExamine(@Valid @RequestBody SiteToExamineVo vo) {

		return R.status(platformFittingApplyService.siteToExamine(vo));
	}

	/**
	 * 发货
	*/
	@GetMapping("/sendOutGoods")
	@Parameters({
			@Parameter(name = "id", description = "申请单号id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
			@Parameter(name = "code", description = "物流单号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
			@Parameter(name = "name", description = "物流名称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
	})
	@ApiOperationSupport(order = 11)
	@Operation(summary = "平台发货", description = "传入send")
	public R sendOutGoods(@Parameter(hidden = true) @RequestParam Map<String, Object> sendmap) {

		return R.status(platformFittingApplyService.sendOutGoods(sendmap));
	}

	/**
	 * 签收
	*/
	@GetMapping("/siteSignFor")
	@Parameters({
			@Parameter(name = "id", description = "申请单号id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
	})
	@ApiOperationSupport(order = 12)
	@Operation(summary = "签收", description = "id")
	public R siteSignFor(String id){

		return R.status(platformFittingApplyService.siteSignFor(id));
	}

	/**
	 * 运维商出库
	*/
	@GetMapping("/siteOutbound")
	@Parameters({
		@Parameter(name = "id", description = "申请单号id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
	})
	@ApiOperationSupport(order = 12)
	@Operation(summary = "运维商出库", description = "id")
	public R siteOutbound(String id){
		return R.status(platformFittingApplyService.siteOutbound(id));
	}


	/**
	 * 工程师领取
	 */
	@GetMapping("/empReceiveMate")
	@Parameters({
		@Parameter(name = "id", description = "申请单号id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
	})
	@ApiOperationSupport(order = 12)
	@Operation(summary = "领取", description = "id")
	public R empReceiveMate(String id){
	return R.status(platformFittingApplyService.empReceiveMate(id));
	}

	/**
	 * 小程序表头统计
	*/
	@GetMapping("/findHomeFittingCount")
	@ApiOperationSupport(order = 13)
	@Operation(summary = "小程序表头统计", description = "")
	public R<ApplyDto> findHomeFittingCount(){
		return R.data(platformFittingApplyService.findHomeFittingCount());
	}



    @GetMapping(value = "/auditFittingMatchingMaterial")
	@ApiOperationSupport(order =14)
	@Operation(summary = "运营商审核的时候匹配物料")
	@Parameters({
		@Parameter(name = "materialCode", description = "物料代码", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
		@Parameter(name = "materialType", description = "物料类型名称", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
		@Parameter(name = "materialSpec", description = "物料规格", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
	})
	public R<List<OperationOrderMaterialVO>> auditFittingMatchingMaterial(@RequestParam Map<String, Object> params){
		List<OperationOrderMaterialVO> list=platformFittingApplyService.auditFittingMatchingMaterial(params);
		return R.data(list);
	}

}
