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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.framework.web.BasePhpCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.IsBusinessEnum;
import com.mortals.xhx.module.business.model.BusinessEntity;
import com.mortals.xhx.module.site.model.SiteBusinessEntity;
import com.mortals.xhx.module.site.model.SiteBusinessQuery;
import com.mortals.xhx.module.site.model.SiteBusinessTreeSelect;
import com.mortals.xhx.module.site.service.SiteBusinessService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

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

    @Autowired
    private ParamService paramService;

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

    @Override
    protected void doListBefore(SiteBusinessEntity query, Map<String, Object> model, Context context) throws AppException {
        query.setIsBusiness(IsBusinessEnum.一级业务.getValue());
        super.doListBefore(query, model, context);
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "isBusiness", paramService.getParamBySecondOrganize("SiteBusiness", "isBusiness"));
        this.addDict(model, "status", paramService.getParamBySecondOrganize("SiteBusiness", "status"));
        super.init(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() {
        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();
    }

}