package com.sunxung.factoring.web.sys;

import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.sys.Org;
import com.sunxung.factoring.entity.sys.vo.SearchOrg;
import com.sunxung.factoring.entity.sys.vo.SysGradationVo;
import com.sunxung.factoring.service.sys.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @className:OrgController2
 * @description:组织机构controller层
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年1月2日 下午2:13:31
 */
@Controller
public class OrgController {

    private static String PREFIX = "sys/org/";
    @Autowired
    private OrgService orgService;

    @RequestMapping("/org/list")
    public String list(Model model) {
        return PREFIX + "orgList";
    }

    @RequestMapping("/org/departmentContent")
    public String departmentContent(Model model) {
        return PREFIX + "orgDepartmentContent";
    }

    @RequestMapping("/org/memberContent")
    public String memberContent(Model model) {
        return PREFIX + "orgMemberContent";
    }

    @RequestMapping("/org/addMember")
    public String addMember(Model model) {
        return PREFIX + "orgAddMember";
    }

    @RequestMapping("/org/viewMember")
    public String viewMember(Model model) {
        return PREFIX + "orgViewMember";
    }

    @RequestMapping("/org/updateMember")
    public String updateMember(Model model) {
        return PREFIX + "orgUpdateMember";
    }

    @RequestMapping("/org/welcomeIndex")
    public String welcomeIndex(Model model) {
        return PREFIX + "welcomeIndex";
    }

    @RequestMapping(value = "/org/findRootOrgTreeNode", method = {
            RequestMethod.GET, RequestMethod.POST
    })
    @ResponseBody
    public Object findRootOrgTreeNode(Model model) {
        return orgService.findRootOrgTreeNode(false);
    }

    @RequestMapping(value = "/org/findNodeByParentId", method = {
            RequestMethod.GET, RequestMethod.POST
    })
    @ResponseBody
    public Object findOrgTreeNodeByParentId(Long parentId, Model model) {
        return orgService.findOrgTreeNodeByParentId(parentId, false);
    }

    @RequestMapping(value = "/org/findRootOrgTreeNodeNumber", method = RequestMethod.GET)
    @ResponseBody
    public Object do_findRootOrgTreeNodeNumber() {
        return orgService.findRootOrgTreeNode(true);
    }

    @RequestMapping(value = "/org/findNodeByParentIdNumber", method = RequestMethod.GET)
    @ResponseBody
    public Object do_findNodeByParentIdNumber(Long parentId, Model model) {
        return orgService.findOrgTreeNodeByParentId(parentId, true);
    }

    @RequestMapping(value = "/org/findOrgAndUserTreeNode", method = RequestMethod.GET)
    @ResponseBody
    public Object do_findOrgAndUserTreeNode(Long parentId, Model model) {
        return orgService.findOrgAndUserTreeNode(parentId, false);
    }

    /**
     * 查询组织结构和人员信息（只包含深圳、西安、上海三个运营中心和重要客户部）
     *
     * @param parentId
     * @return
     */
    @RequestMapping(value = "/org/findOperationCenterAndUserTreeNode", method = RequestMethod.GET)
    @ResponseBody
    public Object findOperationCenterAndUserTreeNode(Long parentId) {
        return orgService.findOperationCenterAndUserTreeNode(parentId, false);
    }

    @RequestMapping(value = "/org/findOrgAndUserTreeNodeAndAdmin", method = RequestMethod.GET)
    @ResponseBody
    public Object findOrgAndUserTreeNodeAndAdmin(Long parentId, Model model) {
        return orgService.findOrgAndUserTreeNodeAndAdmin(parentId, false);
    }

    @RequestMapping(value = "/org/findOrgAndUserTreeNodeAndAdmin/new", method = RequestMethod.GET)
    @ResponseBody
    public ResponseJson findOrgAndUserTreeNodeAndAdminNew(Long parentId, Model model) {
        return new ResponseJson(orgService.findOrgAndUserTreeNodeAndAdmin(parentId, false));
    }

    @RequestMapping(value = "/org/getListData", method = RequestMethod.GET)
    @ResponseBody
    public Object do_listData(SearchOrg search) {
        return orgService.findListByMapForPage(search);
    }

    @RequestMapping(value = "/org/findOrgUserSearch", method = RequestMethod.GET)
    @ResponseBody
    public Object do_findOrgUserSearch(String keyword) {
        return orgService.findOrgUserList(keyword);
    }

    @RequestMapping(value = "/org/do_add", method = RequestMethod.POST)
    @ResponseBody
    public Object do_add(Org org) {
        return orgService.add(org);
    }

    @RequestMapping(value = "/org/do_update", method = RequestMethod.POST)
    @ResponseBody
    public Object do_update(Org org) {
        return orgService.update(org);
    }

    @RequestMapping(value = "/org/do_delete", method = RequestMethod.POST)
    @ResponseBody
    public Object do_delete(Long id) {
        return orgService.delete(id);
    }

    @RequestMapping(value = "/org/get", method = RequestMethod.GET)
    @ResponseBody
    public Object get(Long orgId) {
        return orgService.get(orgId);
    }

    @RequestMapping(value = "/org/getOrgSelectbox", method = RequestMethod.GET)
    @ResponseBody
    public List<Org> getOrgSelectbox(Long nowId) {
        // 获取长安银科所在组织机构
        Org changanyinke = orgService.getByCode(ConstantConversionUtil.dept_no);
        return orgService.getOrgSelectbox(changanyinke.getId(), nowId);
    }

    @RequestMapping(value = "/org/do_adjustOrder", method = RequestMethod.POST)
    @ResponseBody
    public Object do_adjustOrder(SysGradationVo sysVo) {
        return orgService.adjustOrder(sysVo);
    }

    /**
     * 调用sop接口，同步组织机构和用户信息
     *
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/org/sync", method = RequestMethod.GET)
    @ResponseBody
    public Object do_syncOrgAndUser(Long orgId) {
        orgService.syncOrgAndUser(orgId);
        return new ResponseJson("同步成功");
    }

    /**
     * 调用sop接口，获取指定部门下所有人员信息
     *
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/org/findUserListByOrgId", method = RequestMethod.GET)
    @ResponseBody
    public Object do_findUserListByOrgId(Long orgId) {
        return orgService.findUserListByOrgId(orgId);
    }

}
