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

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.dept.model.DeptQuery;
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.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.dept.model.DeptTreeSelect;
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 java.util.Arrays;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 部门信息
 *
 * @author zxfei
 * @date 2023-03-06
 */
@RestController
@RequestMapping("dept")
public class DeptController extends BaseCRUDJsonBodyMappingController<DeptService, DeptEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private DeptService deptService;


    public DeptController() {
        super.setModuleDesc("部门信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "deptStatus", paramService.getParamBySecondOrganize("Dept", "deptStatus"));

        super.init(model, context);
    }

    @PostMapping("list/exclude")
    public String excludeList(@RequestParam(name = "id") Long id) {
        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<DeptEntity> collect = this.service.find(new DeptQuery()).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<DeptEntity> list = this.service.find(new DeptQuery());
            List<DeptTreeSelect> treeSelects = this.service.buildDeptTreeSelect(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();
    }


    /**
     * 根据parentId查询子信息
     */
    @GetMapping(value = "getListByParentId")
    public String getListByParentId(Long parentId) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc() + "子节点";
        try {
            List<DeptTreeSelect> treeList = this.service.getListByParentId(parentId, getContext());
            model.put(RESULT_KEY, treeList);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc + "【成功】");
        } catch (Exception e) {
            log.error("根据parentId查询子信息错误", e);
            this.doException(request, busiDesc, model, e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }


}