package com.mortals.xhx.module.matter.web;

import com.alibaba.fastjson.JSON;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.common.code.MatterSourceEnum;
import com.mortals.xhx.module.matter.model.MatterDatumEntity;
import com.mortals.xhx.module.matter.model.MatterDatumQuery;
import com.mortals.xhx.module.matter.service.MatterDatumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.service.MatterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
* <p>Title: 事项列表</p>
* <p>Description: MatterController  </p>
* <p>Copyright: Copyright &reg;  </p>
* <p>Company: </p>
* @author zxfei
* @version 1.0.0
*/
@RestController
@RequestMapping("matter")
public class MatterController extends BaseCRUDJsonMappingController<MatterService,MatterForm,MatterEntity,Long> {


    @Autowired
    private MatterDatumService matterDatumService;

    public MatterController(){
        super.setFormClass(MatterForm.class);
        super.setModuleDesc("事项列表");
    }


    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, MatterForm form, Map<String, Object> model, Context context) {
        super.init(request, response, form, model, context);
        this.addDict(model, "source", MatterSourceEnum.getEnumMap());
    }

    @Override
    protected int doListAfter(HttpServletRequest request, HttpServletResponse response, MatterForm form, Map<String, Object> model, Context context) throws AppException {

        List<MatterEntity> list = (List<MatterEntity>) model.get("result");
        list.stream().peek(item->{
            List<MatterDatumEntity> datumEntityList = matterDatumService.find(new MatterDatumQuery().matterId(item.getId()));
            item.setDatumList(datumEntityList);
        }).count();

        model.put("result",list);
        return super.doListAfter(request, response, form, model, context);
    }

    public static void main(String[] args) {
        MatterForm matterForm = new MatterForm();
        matterForm.getQuery().setIdList(Arrays.asList());
        ArrayList<MatterDatumEntity> matterDatumEntities = new ArrayList<>();
        MatterDatumEntity datumEntity = new MatterDatumEntity();
        datumEntity.setId(1L);
        datumEntity.setMaterialName("测试");
        matterDatumEntities.add(datumEntity);
        matterForm.getQuery().setDatumList(matterDatumEntities);

        System.out.println(JSON.toJSONString(matterForm));

    }
}