package com.emergency.avenue.storage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emergency.avenue.admin.api.entity.SysTreeDict;
import com.emergency.avenue.admin.api.vo.MaterialsFailureDetailVO;
import com.emergency.avenue.admin.service.SysTreeDictService;
import com.emergency.avenue.common.core.util.R;
import com.emergency.avenue.storage.api.entity.MaterialsFailureDetail;
import com.emergency.avenue.storage.service.MaterialsFailureDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/materials-failure")
@Tag(description = "materialsfailuredetail", name = "物资故障管理")
public class MaterialsFailureDetailController {
	@Autowired
	@Lazy
	private  MaterialsFailureDetailService materialsFailureDetailService;
	@Lazy
	@Autowired
	private SysTreeDictService sysTreeDictService;


	@GetMapping("/page")
	@Operation(description = "设备故障信息分页查询", summary = "设备故障信息分页查询")
	public R<Page<MaterialsFailureDetail>> getinboundPage(Page page, MaterialsFailureDetail materialsFailureDetail) {
		QueryWrapper<MaterialsFailureDetail> query = Wrappers.query(materialsFailureDetail);
		if (materialsFailureDetail.getWarehouseId()!=null){
			//拼接查询条件：仓库id
			query.lambda().eq(MaterialsFailureDetail::getWarehouseId,materialsFailureDetail.getWarehouseId());
		}
		if (StringUtils.isNotEmpty(materialsFailureDetail.getMaterialNameVo())) {
			//模糊查询物资名称对应的字典id
			QueryWrapper<SysTreeDict> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(SysTreeDict::getType,"1");
			queryWrapper.lambda().like(SysTreeDict::getName,materialsFailureDetail.getMaterialNameVo());
			List<SysTreeDict> list1 = sysTreeDictService.list(queryWrapper);
			List<String> collect = list1.stream().map(SysTreeDict::getId).collect(Collectors.toList());
			if (collect.size()<1){
				return R.ok(page);
			}
			//拼接查询条件：物资名称
			query.lambda().in(MaterialsFailureDetail::getMaterialName,collect);
		}
		query.lambda().orderByDesc(MaterialsFailureDetail::getSubmitTime);
		Page<MaterialsFailureDetail> page1 = materialsFailureDetailService.page(page, query);
		//获取字典项id与name的映射map
		List<SysTreeDict> list = sysTreeDictService.getList(1);
		Map<String, String> collect = list.stream().collect(Collectors.toMap(SysTreeDict::getId, SysTreeDict::getName));
		List<MaterialsFailureDetail> records = page1.getRecords();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		for (MaterialsFailureDetail materialsFailureDetail1:records){
			materialsFailureDetail1.setMaterialName(collect.get(materialsFailureDetail1.getMaterialName()));
			materialsFailureDetail1.setSubmitTimeVo(materialsFailureDetail1.getSubmitTime().format(formatter));
		}
		return R.ok(page1);
	}

	@GetMapping("/get")
	@Operation(description = "通过ID查询设备故障信息", summary = "通过ID查询设备故障信息")
	public R<MaterialsFailureDetailVO> getById(@RequestParam Long id) {
		MaterialsFailureDetailVO materialsFailureDetailById = materialsFailureDetailService.getMaterialsFailureDetailById(id);
		return R.ok(materialsFailureDetailById);
	}

	@PostMapping("/insert")
	@Operation(description = "添加设备故障信息", summary = "添加设备故障信息")
	public R save(@RequestBody MaterialsFailureDetailVO  materialsFailureDetailVO) {
		return  R.ok(materialsFailureDetailService.saveMaterialsFailureDetail(materialsFailureDetailVO));
	}

	@PostMapping("/update")
	@Operation(description = "修改设备故障信息", summary = "修改设备故障信息")
	public R updateById(@RequestBody MaterialsFailureDetailVO  materialsFailureDetailVO) {
		return R.ok(materialsFailureDetailService.updateMaterialsFailureDetail(materialsFailureDetailVO));
	}

	@GetMapping("/delete")
	@Operation(description = "删除设备故障信息", summary = "删除设备故障信息")
	public R removeById(@RequestParam Long id) {
		return R.ok(materialsFailureDetailService.removeById(id));
	}



}
