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

import com.mortals.framework.exception.AppException;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.business.model.BusinessEntity;
import com.mortals.xhx.module.business.model.BusinessQuery;
import com.mortals.xhx.module.business.web.BusinessForm;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import com.mortals.framework.model.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.site.model.SiteBusinessEntity;
import com.mortals.xhx.module.site.model.SiteBusinessQuery;
import com.mortals.xhx.module.site.service.SiteBusinessService;
import com.mortals.xhx.module.site.model.SiteBusinessTreeSelect;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;

import static com.mortals.framework.ap.SysConstains.RESULT_KEY;

/**
*
* 站点事项
*
* @author zxfei
* @date 2022-01-13
*/
@RestController
@RequestMapping("site/business")
public class SiteBusinessController extends BaseCRUDJsonMappingController<SiteBusinessService,SiteBusinessForm,SiteBusinessEntity,Long> {

    @Autowired
    private ParamService paramService;

    public SiteBusinessController(){
        super.setFormClass(SiteBusinessForm.class);
        super.setModuleDesc( "站点事项");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, SiteBusinessForm form, Map<String, Object> model, Context context) {
        this.addDict(model, "isBusiness", paramService.getParamBySecondOrganize("SiteBusiness","isBusiness"));
        this.addDict(model, "status", paramService.getParamBySecondOrganize("SiteBusiness","status"));
        super.init(request, response, form, model, context);
    }

    @Override
    protected int doListAfter(HttpServletRequest request, HttpServletResponse response, SiteBusinessForm form, Map<String, Object> model, Context context) throws AppException {
        List<SiteBusinessEntity> businessEntities = (List<SiteBusinessEntity>) model.get(RESULT_KEY);
        businessEntities.stream().peek(item->{
            List<SiteBusinessEntity> childs = this.service.find(new SiteBusinessQuery().parentId(item.getBusinessId()));
            if(!ObjectUtils.isEmpty(childs)){
                item.setChildren(childs);
            }else{
                item.setChildren(null);
            }
        }).count();
        model.put(RESULT_KEY,businessEntities);
        return super.doListAfter(request, response, form, model, context);
    }

    @PostMapping("list/exclude")
    public String excludeList(SiteBusinessForm 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<SiteBusinessEntity> collect = this.service.find(new SiteBusinessQuery()).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(SiteBusinessForm form) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        int code=VALUE_RESULT_SUCCESS;
        try {
            List<SiteBusinessEntity> list = this.service.find(new SiteBusinessQuery());
            List<SiteBusinessTreeSelect> treeSelects = this.service.buildSiteBusinessTreeSelect(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();
    }




}