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

import com.mortals.framework.ap.SysConstains;
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.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.asset.model.AssetFlowEventEntity;
import com.mortals.xhx.module.asset.model.AssetFlowEventLogEntity;
import com.mortals.xhx.module.asset.model.AssetFlowEventQuery;
import com.mortals.xhx.module.asset.service.AssetFlowEventLogService;
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.*;
import java.util.stream.Collectors;

/**
 * <p>
 * Title: 资产事件日志
 * </p>
 * <p>
 * Description: AssetFlowEventLogController
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * @author
 * @version 1.0.0
 */
@RestController
@RequestMapping("asset/flow/eventlog")
public class AssetFlowEventLogController extends
        BaseCRUDJsonMappingController<AssetFlowEventLogService, AssetFlowEventLogForm, AssetFlowEventLogEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private AssetFlowEventService assetFlowEventService;

    @Autowired
    private UserService userService;


    public AssetFlowEventLogController() {
        super.setFormClass(AssetFlowEventLogForm.class);
        super.setModuleDesc("资产事件日志");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, AssetFlowEventLogForm form,
            Map<String, Object> model, Context context) {
        this.addDict(model, "afteStatus", paramService.getParamByFirstOrganize(Constant.Param_AssetFlowStatus));
        this.addDict(model, "beforeStatus", paramService.getParamByFirstOrganize(Constant.Param_AssetFlowStatus));
        AssetFlowEventQuery query = new AssetFlowEventQuery();
        query.setOrderColList(new ArrayList<OrderCol>() {
            {
                add(new OrderCol("orderNo", OrderCol.ASCENDING));
            }
        });
        List<AssetFlowEventEntity> eventEntityList = assetFlowEventService.find(query, null);
        this.addDict(model, "eventCode", eventEntityList.stream().collect(Collectors.toMap(e->e.getCode() +"", e->e.getName() )));
        List<String> loginNameList = new ArrayList<>();
        if(model.containsKey("result")){
            List<AssetFlowEventLogEntity> list = (List<AssetFlowEventLogEntity>)model.get(SysConstains.RESULT_KEY);

            //List<String> loginNameList = new ArrayList<>();
            if(!list.isEmpty()){
                list.stream().forEach(item->{
                    if(StringUtils.isNotEmpty(item.getEventUser() )){
                        loginNameList.add(item.getEventUser());
                    }
                    if(StringUtils.isNotEmpty(item.getEventAfterUser())){
                        loginNameList.addAll(Arrays.asList(item.getEventAfterUser().split(",")));
                    }
                });
            }

        }else if(model.containsKey("entity")){ //查看编辑页面
            AssetFlowEventLogEntity entity = (AssetFlowEventLogEntity) model.get("entity");
            if(entity != null && StringUtils.isNotEmpty(entity.getEventUser())){
                loginNameList.add(entity.getEventUser());
            }
            if(StringUtils.isNotEmpty(entity.getEventAfterUser())){
                loginNameList.addAll(Arrays.asList(entity.getEventAfterUser().split(",")));
            }
        }
        if(!loginNameList.isEmpty()){
            UserQuery userQuery = new UserQuery();
            userQuery.setLoginNameList(loginNameList);
            Map<String,String> loginNameDict = userService.find(userQuery, null).stream().collect(Collectors.toMap(e->e.getLoginName() +"", e->e.getRealName() +"(" + e.getLoginName() +")" ));
            Map<String,String> loginNameDict2 = new HashMap<>();
            loginNameDict2.putAll(loginNameDict);
            this.addDict(model,"eventUser", loginNameDict);
            this.addDict(model,"eventAfterUser", loginNameDict2);
        }


        super.init(request, response, form, model, context);
    }

    @Override
    protected void doListBefore(HttpServletRequest request, HttpServletResponse response, AssetFlowEventLogForm form, Map<String, Object> model, Context context) throws AppException {
        form.getQuery().setOrderColList(new ArrayList<OrderCol>() {
            {
                add(new OrderCol("id", OrderCol.DESCENDING));
            }
        });
        super.doListBefore(request, response, form, model, context);
    }


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

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

}