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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.model.MatterQuery;
import com.mortals.xhx.module.matter.service.MatterService;
import com.mortals.xhx.module.one.model.OneClassifyEntity;
import com.mortals.xhx.module.one.model.OneClassifyQuery;
import com.mortals.xhx.module.one.service.OneClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
* <p>Title: 分类引导表</p>
* <p>Description: OneClassifyController  </p>
* <p>Copyright: Copyright &reg;  </p>
* <p>Company: </p>
* @author zxfei
* @version 1.0.0
*/
@RestController
@RequestMapping("one/classify")
public class OneClassifyController extends BaseCRUDJsonMappingController<OneClassifyService,OneClassifyForm,OneClassifyEntity,Long> {

//    @Autowired
//    private OneMatterService oneMatterService;
    @Autowired
    private MatterService matterService;


    public OneClassifyController(){
        super.setFormClass(OneClassifyForm.class);
        super.setModuleDesc("分类引导表");
    }

    @PostMapping("list")
    public String list(OneClassifyForm form) {

        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        Context context = this.getContext();
        String busiDesc = "查询" + this.getModuleDesc();
        int code;
        try {
            form.getQuery().setDeleted(YesNoEnum.NO.getValue());
            Result<OneClassifyEntity> result = this.getService().find(form.getQuery(), form.getPageInfo(), context);
            result.getList().stream().peek(item->{
                item.setHasChildren(false);
                List<OneClassifyEntity> subList = this.service.find(new OneClassifyQuery().parentId(item.getId()).deleted(YesNoEnum.NO.getValue()));
                if(!ObjectUtils.isEmpty(subList)){
                    item.setHasChildren(true);
                }

                List<Long> matterIds = Arrays.asList(item.getMatterIds().split(",")).stream().filter(e->!ObjectUtils.isEmpty(e)).map(Long::valueOf).collect(Collectors.toList());

                if(!ObjectUtils.isEmpty(matterIds)){
                    List<MatterEntity> matterEntities = matterService.find(new MatterQuery().idList(matterIds));
                    String matterNames = matterEntities.stream().map(matterEntity -> matterEntity.getMatterName()).collect(Collectors.joining(","));
                    item.setMatterNames(matterNames);

                    List<String> collect = matterEntities.stream().map(MatterEntity::getMatterName).collect(Collectors.toList());
                    item.setMatterNameList(collect);

                }
            }).count();

            model.put("result", result.getList());
            model.put("pageInfo", result.getPageInfo());
            model.putAll(form.getModel());
            code = this.doListAfter(request, response, form, model, context);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        form.getModel().clear();
        this.init(request, response, form, model, context);
        ret.put(KEY_RESULT_CODE, code);
        ret.put(KEY_RESULT_MSG, model.remove("message_info"));
        ret.put(KEY_RESULT_QUERY, form.getQuery());
        ret.put(KEY_RESULT_DATA, model);
        return ret.toJSONString();
    }



}