/**
* 文件：MateriaSupplierController.java
* 版本：1.0.0
* 日期：
* Copyright &reg; 
* All right reserved.
*/
package com.mortals.xhx.module.materia.web;


import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.materia.model.MateriaQuery;
import com.mortals.xhx.module.materia.model.MateriaSupplierEntity;
import com.mortals.xhx.module.materia.model.MateriaSupplierQuery;
import com.mortals.xhx.module.materia.service.MateriaService;
import com.mortals.xhx.module.materia.service.MateriaSupplierService;
import com.mortals.xhx.module.supplier.model.SupplierQuery;
import com.mortals.xhx.module.supplier.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>Title: 物料供应商表</p>
 * <p>Description: MateriaSupplierController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author 
 * @version 1.0.0
 */
@RestController
@RequestMapping("materia/supplier")
public class MateriaSupplierController extends BaseCRUDJsonMappingController<MateriaSupplierService,MateriaSupplierForm,MateriaSupplierEntity,Long> {

	@Autowired
	private ParamService paramService;
	@Autowired
	private MateriaService materiaService;
	@Autowired
	private SupplierService supplierService;

	public MateriaSupplierController(){
		super.setFormClass(MateriaSupplierForm.class);
		super.setModuleDesc("物料供应商表");
	}

	@Override
	protected void saveBefore(HttpServletRequest request, HttpServletResponse response, MateriaSupplierForm form, Map<String, Object> model, Context context) throws AppException {
		//判断当前物料供应商是否存在，如果存在则更新
		MateriaSupplierQuery query = new MateriaSupplierQuery();
		query.setMateriaId(form.getEntity().getMateriaId());
		query.setSupplierId(form.getEntity().getSupplierId());
		MateriaSupplierEntity supplierEntity = this.service.find(query, context).stream().findAny().orElseGet(() -> null);
		if(!ObjectUtils.isEmpty(supplierEntity)){
			form.getEntity().setId(supplierEntity.getId());
		}

	}

	@Override
	protected void init(HttpServletRequest request, HttpServletResponse response, MateriaSupplierForm form, Map<String, Object> model, Context context) {
		this.addDict(model, "deliveryPeriodUnit", paramService.getParamByFirstOrganize(Constant.Param_DeliveryPeriodUnit));
		this.addDict(model, "unitOfMeasure", paramService.getParamByFirstOrganize(Constant.Param_MatriaUnitOfMeasure));
		this.addDict(model, "supplierId", supplierService.find(new SupplierQuery(),getContext()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getName(),(n, o)->o)));
		this.addDict(model, "materiaId", materiaService.find(new MateriaQuery(),getContext()).stream().collect(Collectors.toMap(x->x.getId().toString(), y->y.getName(),(n, o)->o)));

	}
}