Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / EventsManage / DivisionOrgManageController.java
package com.newfiber.api.pc.controller.EventsManage;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.core.support.HttpCode;
import com.newfiber.api.pc.dao.EventsManage.AdministrativeDivisionDao;
import com.newfiber.api.pc.dao.EventsManage.DivisionOrgDao;
import com.newfiber.api.pc.model.EventsManage.AdministrativeDivision;
import com.newfiber.api.pc.model.EventsManage.AdministrativeDivisionQuery;
import com.newfiber.api.pc.model.EventsManage.DivisionOrg;
import com.newfiber.api.pc.model.EventsManage.DivisionOrgQuery;
import com.newfiber.api.pc.model.UserManager.Sys_Roles;
import com.newfiber.api.pc.model.UserManager.UserInfo;
import com.newfiber.api.pc.service.RestTemplateService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by XuChengChao on 2017/11/28.区域机构关联控制器
 */
@Controller
public class DivisionOrgManageController {

    @Autowired
    private DivisionOrgDao divisionOrgDao;

    @Autowired
    private AdministrativeDivisionDao administrativeDivisionDao;

    @Resource
    private RestTemplateService restTemplateService;

    /**
     * 添加行政区划机构关联
     * @return
     */
    @PostMapping(value = "division/addDivisionOrgManage",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj addDivisionOrgManage(HttpServletRequest request, @RequestBody ReqBodyObj<Map<String,Object>> param){

        Map<String,Object> map =param.getData();
        String orgNo = (String)map.get("orgNo");
        String divisionNo = (String) map.get("divisionNo");
        try {

            String userNO  = request.getHeader("userNo");
            //再将数据添加到机构区划关联表当中
            DivisionOrg divisionOrg = new DivisionOrg();
            divisionOrg.setCreateUserNo(userNO);
            divisionOrg.setDivisionNo(divisionNo);
            divisionOrg.setOrgNo(orgNo);
            int add = divisionOrgDao.insertSelective(divisionOrg);

            return RespBodyObj.ok();
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }

    }


    /**
     * 根据区划编号查询机构
     * @return
     */
    @PostMapping(value = "division/selectOrgByDivisionNo",produces = "application/json;cahrset=UTF-8")
    @ResponseBody
    public RespBodyObj selectOrgByDivisionNo(@RequestBody ReqBodyObj<Map<String,String>> param){

        String divisionNo = param.getData().get("divisionNo");
        Integer page = param.getCurrent();
        Integer rows = param.getSize();
        DivisionOrgQuery query  = new DivisionOrgQuery();
        if (page != null){
            query.setPageNo(page);
            query.setPageSize(rows);
        }
        try {
            Page page1= new Page();
            query.createCriteria().andDivisionNoEqualTo(divisionNo);

            List<DivisionOrg> list = divisionOrgDao.selectByExample(query);

            int count = divisionOrgDao.countByExample(query);
            page1.setTotal(count);
            page1.setRecords(list);
            return RespBodyObj.ok(page1);


        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }

    }


    /**
     * 删除机构区划关联数据
     * @return
     */
    @PostMapping(value = "division/deleteDivisionOrgManage",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj deleteDivisionOrgManage(@RequestBody ReqBodyObj<Map<String,Long []>> param){
        Long [] serials = param.getData().get("serial");
        JSONObject json = new JSONObject();
        try {

            DivisionOrgQuery query  = new DivisionOrgQuery();

            for (int i=0;i<serials.length;i++){
                Long serial = serials[i];

                query.createCriteria().andSerialEqualTo(serial);

                int delete= divisionOrgDao.deleteByExample(query);

                query.clear();

            }
            return RespBodyObj.ok();


        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();

        }


    }


    @PostMapping(value = "division/getDivisionOrgUsers")
    @ResponseBody
    public RespBodyObj getUsersByDivision(@RequestBody ReqBodyObj<String> param ){

        if (param.getData() == null){
            return  RespBodyObj.error(HttpCode.BAD_REQUEST);
        }
        //根据行政区划编号查询机构下的人员信息
        List<UserInfo> list = divisionOrgDao.getUsersByDivisionNo(param.getData());
        return RespBodyObj.ok(list);

    }

    /**
     * 添加下级区划机构
     * @param divisionNo
     * @return
     */
    public List<Map<String,Object>> addLowerOrg(String divisionNo,String roleNo){

        AdministrativeDivisionQuery divisionQuery = new AdministrativeDivisionQuery();
        divisionQuery.createCriteria().andParentDivisionNoEqualTo(divisionNo);
        //查询下级行政区划
        List<AdministrativeDivision> divisions = administrativeDivisionDao.selectByExample(divisionQuery);
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            if (divisions.size()>0){

                for (AdministrativeDivision ad : divisions){
                    String dNo = ad.getDivisionNo();//行政区划编号
                    String dName = ad.getDivisionName();//行政区划名称
                    DivisionOrgQuery divisionOrgQuery = new DivisionOrgQuery();
                    divisionOrgQuery.createCriteria().andDivisionNoEqualTo(dNo);
                    List<DivisionOrg> orgList = divisionOrgDao.selectByExample(divisionOrgQuery);
                    if (orgList.size()>0){
                        for (DivisionOrg item : orgList){
                            Map<String,Object> map1 = new HashMap<>();
                            String oName = item.getOrgName();//机构名称
                            String oNo = item.getOrgNo();//机构编号

                            Sys_Roles role = restTemplateService.selectRoleByOrgNo(oNo);//查询该机构属于哪个角色
                            String RNO = role.getRoleNo();
                            if (RNO.equals(roleNo)){
                                map1.put("id",oNo);
                                map1.put("text",oName);
                                List<Map<String,Object>> list1 = addLowerOrg(dNo,roleNo);
                                map1.put("children",list1);
                                list.add(map1);
                            }
                        }
                    }
                }
            }

            return list;
        }catch (Exception e){
            e.printStackTrace();
            return list;
        }



    }


    /**
     * 获取行政区划机构树形
     * @return
     */
    @PostMapping(value = "getOrganizationTree",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getOrganizationTree(){
        AdministrativeDivisionQuery query  = new AdministrativeDivisionQuery();
        query.createCriteria().andParentDivisionNoEqualTo("0");
        List<AdministrativeDivision> provinceList = administrativeDivisionDao.selectByExample(query);
        Map<String,Object> map1 = new HashMap<>();//存放机构
        try {
            if (provinceList.size()>0){
                map1.put("id","");
                map1.put("text","机构");
                for (AdministrativeDivision ad : provinceList){
                    String divisionNo = ad.getDivisionNo();//区划编号
                    DivisionOrgQuery example = new DivisionOrgQuery();
                    example.createCriteria().andDivisionNoEqualTo(divisionNo);
                    List<DivisionOrg> orgList = divisionOrgDao.selectByExample(example);
                    if (orgList.size()>0){
                        List<Map<String,Object>> list = new ArrayList<>();
                        for (DivisionOrg org : orgList){
                            Map<String,Object> map2 = new HashMap<>();
                            String orgNo = org.getOrgNo();//机构编号
                            String orgName = org.getOrgName();//机构名称
                            Sys_Roles role = restTemplateService.selectRoleByOrgNo(orgNo);//根据机构编号查询角色信息
                            String roleNo = role.getRoleNo();//角色编号
                            map2.put("id",orgNo);
                            map2.put("text",orgName);
                            List<Map<String,Object>> list1 = addLowerOrg(divisionNo,roleNo);
                            map2.put("children",list1);
                            list.add(map2);
                        }
                        map1.put("children",list);


                    }


                }
            }
            return RespBodyObj.ok(map1);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.ok(map1);
        }



        }









}