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

import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.YesNo;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.AssetFlowStatusEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.api.handler.AssetFlowEventHandler;
import com.mortals.xhx.module.asset.model.AssetFlowEventEntity;
import com.mortals.xhx.module.asset.service.AssetFlowEventService;
import org.springframework.beans.factory.annotation.Autowired;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * Title: 资产事件表
 * </p>
 * <p>
 * Description: AssetFlowEventController
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author
 * @version 1.0.0
 */
@RestController
@RequestMapping("asset/flow/event")
public class AssetFlowEventController
		extends BaseCRUDJsonMappingController<AssetFlowEventService, AssetFlowEventForm, AssetFlowEventEntity, Long> {
	@Autowired
	private ParamService paramService;

	@Override
	protected void init(HttpServletRequest request, HttpServletResponse response, AssetFlowEventForm form,
			Map<String, Object> model, Context context) {
		this.addDict(model, "beforeFlowStatus", paramService.getParamByFirstOrganize(Constant.Param_AssetFlowStatus,
				AssetFlowStatusEnum.Special_598.getValue(), AssetFlowStatusEnum.Special_599.getValue()));
		this.addDict(model, "afterFlowStatus", paramService.getParamByFirstOrganize(Constant.Param_AssetFlowStatus,
				AssetFlowStatusEnum.Special_597.getValue()));
		this.addDict(model, "enabled", IBaseEnum.getEnumMap(YesNo.class));
		Map<String,String> scriptDict =  GlobalSysInfo.getBeansByType(AssetFlowEventHandler.class)
				.keySet().stream().collect(Collectors.toMap(item->StringUtils.capitalize(item), item-> StringUtils.capitalize(item)));
		this.addDict(model, "script", scriptDict);
		super.init(request, response, form, model, context);
	}

	@Override
	protected void doListBefore(HttpServletRequest request, HttpServletResponse response, AssetFlowEventForm form,
			Map<String, Object> model, Context context) throws AppException {
		if (StringUtils.isNotEmpty(form.getQuery().getBeforeFlowStatus())) {
			form.getQuery().setBeforeFlowStatus("%" + form.getQuery().getBeforeFlowStatus() + "%");
		}
		List<OrderCol> orderColList =  form.getQuery().getOrderColList();
		if(orderColList == null || orderColList.isEmpty()){
			//默认按照排序号排序
			form.getQuery().setOrderColList(new ArrayList<OrderCol>() {
				{
					add(new OrderCol("orderNo", OrderCol.ASCENDING));
				}
			});
		}
		super.doListBefore(request, response, form, model, context);
		form.getPageInfo().setPrePageResult(-1);
	}

	@Override
	protected void saveBefore(HttpServletRequest request, HttpServletResponse response, AssetFlowEventForm form,
			Map<String, Object> model, Context context) throws AppException {
		super.saveBefore(request, response, form, model, context);
	}

	@Override
	protected int editAfter(HttpServletRequest request, HttpServletResponse response, AssetFlowEventForm form,
			Map<String, Object> model, AssetFlowEventEntity entity, Context context) throws AppException {
		return super.editAfter(request, response, form, model, entity, context);
	}

	@Override
	protected void addAfter(HttpServletRequest request, HttpServletResponse response, AssetFlowEventForm form,
			Map<String, Object> model) throws AppException {
		super.addAfter(request, response, form, model);
	}

	public AssetFlowEventController() {
		super.setFormClass(AssetFlowEventForm.class);
		super.setModuleDesc("资产事件表");
	}

}