package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.model.dto.MaterialNotPostedDto;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/packing")
@EnableAutoConfiguration
public class MaterialPackingController {

	/**
	 * 更新未入账
	 */
	@PostMapping(value = "/update")
	public AjaxResult update(@RequestBody JSONObject in) {
		MaterialNotPostedDto sl = JSON.toJavaObject(in, MaterialNotPostedDto.class);
        // 获取数据
		List<MaterialPackingKey> collect = sl.getDataList().stream().map(item -> {
			MaterialPackingKey packingKey = new MaterialPackingKey();
			packingKey.setSiteName(item.getSiteName());
			packingKey.setMaterialPackingName(item.getMaterialPackingName());
			return packingKey;
		}).collect(Collectors.toList());
		List<com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking> materialPackings =
				MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(collect);
		// 修改
		for (com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking materialPacking : materialPackings) {
			// 批次号
			materialPacking.setCharge(sl.getCHARGE());
			// 保质期
			materialPacking.setExpiringDate(Timestamp.valueOf(sl.getEXPIRNGDATE()));
			// 制造日期
			materialPacking.setMakeDate(Timestamp.valueOf(sl.getMAKEDATE()));
		}
		MaterialPackingServiceProxy.getMaterialPackingService().update(materialPackings);
		return AjaxResult.me().setSuccess(true).setMessage("过账成功");
	}
}
