package com.mortals.xhx.base.system.site.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.site.model.SiteEntity;
import com.mortals.xhx.base.system.site.model.SiteQuery;
import com.mortals.xhx.base.system.site.model.TreeSelect;
import com.mortals.xhx.base.system.site.service.SiteService;
import com.mortals.xhx.common.code.SiteSatusEnum;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.PostMapping;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>Title: 站点信息表</p>
 * <p>Description: SiteController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 *
 * @author zxfei
 * @version 1.0.0
 */
@RestController
@RequestMapping("site")
public class SiteController extends BaseCRUDJsonMappingController<SiteService, SiteForm, SiteEntity, Long> {

    public SiteController() {
        super.setFormClass(SiteForm.class);
        super.setModuleDesc("站点信息表");
    }

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


    @PostMapping("list/exclude")
    public String excludeList(SiteForm form) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        Long id = form.getId()[0];
        int code=VALUE_RESULT_SUCCESS;
        try {
            List<SiteEntity> collect = this.service.find(new SiteQuery()).stream().map(item -> {
                if (item.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(item.getAncestors(), ","), id + "")) {
                    return null;
                }
                return item;
            }).filter(f -> f != null).collect(Collectors.toList());
            model.put("result", collect);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
     * 获取站点下拉树列表
     */
    @PostMapping("treeselect")
    public String treeselect(SiteForm form) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        int code=VALUE_RESULT_SUCCESS;
        try {
            List<SiteEntity> list = this.service.find(new SiteQuery());
            List<TreeSelect> treeSelects = this.service.buildSiteTreeSelect(list);
             model.put("result", treeSelects);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


}