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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.area.model.AreaEntity;
import com.mortals.xhx.module.area.model.AreaQuery;
import com.mortals.xhx.module.area.model.AreaTreeSelect;
import com.mortals.xhx.module.area.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * 区域
 *
 * @author zxfei
 * @date 2022-01-19
 */
@RestController
@RequestMapping("area")
public class AreaController extends BaseCRUDJsonBodyMappingController<AreaService, AreaEntity, Long> {

    @Autowired
    private ParamService paramService;

    public AreaController() {
        super.setModuleDesc("区域");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "haveSonArea", paramService.getParamBySecondOrganize("Area", "haveSonArea"));
        this.addDict(model, "haveSonDept", paramService.getParamBySecondOrganize("Area", "haveSonDept"));
        this.addDict(model, "haveGetDept", paramService.getParamBySecondOrganize("Area", "haveGetDept"));
        this.addDict(model, "haveGetMatterList", paramService.getParamBySecondOrganize("Area", "haveGetMatterList"));
        this.addDict(model, "areaLevel", paramService.getParamBySecondOrganize("Area", "areaLevel"));
        this.addDict(model, "status", paramService.getParamBySecondOrganize("Area", "status"));
        super.init(model, context);
    }


    /**
     * 获取站点下拉树列表
     */
    @PostMapping("treeselect")
    @UnAuth
    public String treeselect() {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        int code = VALUE_RESULT_SUCCESS;
        try {
            List<AreaEntity> list = this.service.find(new AreaQuery());
            List<AreaTreeSelect> treeSelects = this.service.buildAreaTreeSelect(list);
            model.put(KEY_RESULT_DATA, 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")
    @UnAuth
    public String getListByParentId(String parentId) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc() + "子节点";
        try {
            List<AreaTreeSelect> treeList = this.service.getListByParentId(parentId, getContext());
            model.put(KEY_RESULT_DATA, 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();
    }


    /**
     * 根据parentId查询子信息
     */
    @GetMapping(value = "getListByRootId")
    @UnAuth
    public String getListByRootId(String rootId) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc() + "子节点";
        try {
            List<AreaTreeSelect> treeList = this.service.getListByRootId(rootId, getContext());
            model.put(KEY_RESULT_DATA, treeList);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc + "【成功】");
        } catch (Exception e) {
            log.error("根据rootId查询子信息错误", 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();
    }


}