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

import com.alibaba.fastjson.JSON;
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.utils.UUIDPK;
import com.newfiber.api.pc.dao.EventsManage.AdministrativeDivisionDao;
import com.newfiber.api.pc.dao.EventsManage.SystemFileDao;
import com.newfiber.api.pc.model.EventsManage.AdministrativeDivision;
import com.newfiber.api.pc.model.EventsManage.AdministrativeDivisionQuery;
import com.newfiber.api.pc.model.EventsManage.SystemFile;
import com.newfiber.api.pc.model.EventsManage.SystemFileQuery;
import com.newfiber.api.pc.model.EventsManage.Tree;
import com.newfiber.api.pc.model.River.RiverInfoBasis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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/24.行政区划相控制器
 */
@Controller
public class AdminnistrativeDivisionController {

    @Autowired
    private AdministrativeDivisionDao administrativeDivisionDao;

    @Autowired
    private SystemFileDao systemFileDao;

    /**
     * 获取所有一级行政区划列表
     * @return
     */
    @PostMapping(value = "division/getProvinceList",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getProvinceList(){
        AdministrativeDivisionQuery query = new AdministrativeDivisionQuery();
        query.createCriteria().andParentDivisionNoEqualTo("0");
        List<AdministrativeDivision> list = new ArrayList<>();
        try {
            list = administrativeDivisionDao.selectByExample(query);
            return JSON.toJSONString(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSON.toJSONString(list);
        }

    }


    /**
     * 根据上级编号查询下级信息的下拉列表
     * @return
     */
    @PostMapping(value = "division/getCityList",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getDivisionTree(@RequestBody ReqBodyObj<Map<String,Object>> param){
        String parentNo = (String) param.getData().get("parentNo");
        AdministrativeDivisionQuery query = new AdministrativeDivisionQuery();
        query.createCriteria().andParentDivisionNoEqualTo(parentNo);
        List<AdministrativeDivision> list = new ArrayList<>();
        try {
            list = administrativeDivisionDao.selectByExample(query);
            if (list.size()>0){
                for (AdministrativeDivision ad : list){
                    String fileNo  = ad.getPicNo();
                    if (fileNo != null && !("".equals(fileNo))){
                        SystemFileQuery query1 = new SystemFileQuery();
                        query1.createCriteria().andFileNoEqualTo(fileNo);
                        List<SystemFile> files = systemFileDao.selectByExample(query1);
                        ad.setPicList(files);
                    }

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

    }
    /**
     * 根据上级编号查询下级信息的下拉列表
     * @return
     */
    @PostMapping(value = "division/getDivisionByPage",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj<Page<AdministrativeDivision>> getDivisionByPage(@RequestBody ReqBodyObj<Map<String,Object>> param){
        Page<AdministrativeDivision> result = new Page<>();
        String parentNo = (String) param.getData().get("parentNo");
        AdministrativeDivisionQuery query = new AdministrativeDivisionQuery();
        query.createCriteria().andParentDivisionNoEqualTo(parentNo);
        List<AdministrativeDivision> list = new ArrayList<>();
        try {
            int total =administrativeDivisionDao.selectByExample(query).size();
            result.setTotal(total);
            query.setPageNo(param.getCurrent());
            query.setPageSize(param.getSize());
            list = administrativeDivisionDao.selectByExample(query);
            result.setRecords(list);
            if (list.size()>0){
                for (AdministrativeDivision ad : list){
                    String fileNo  = ad.getPicNo();
                    if (fileNo != null && !("".equals(fileNo))){
                        SystemFileQuery query1 = new SystemFileQuery();
                        query1.createCriteria().andFileNoEqualTo(fileNo);
                        List<SystemFile> files = systemFileDao.selectByExample(query1);
                        ad.setPicList(files);
                    }

                }
            }
            return RespBodyObj.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error("查询失败");
        }

    }

    /**
     * 给上级添加下级区域
     * @param
     * @return
     */
    @PostMapping(value = "division/AddDivisionByParentNo",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj AddDivision(HttpServletRequest request,@RequestBody ReqBodyObj<Map<String,Object>> param){
        String parentNo = (String) param.getData().get("parentNo");
        String divisionName = (String) param.getData().get("divisionName");
        String description = (String) param.getData().get("description");
        List<String> pics = (List<String>) param.getData().get("pics");
        String userNo =request.getHeader("userNo");

        AdministrativeDivision administrativeDivision = new AdministrativeDivision();
        administrativeDivision.setDivisionNo(UUIDPK.UUIDGenerator.getUUID());
        administrativeDivision.setDivisionName(divisionName);
        administrativeDivision.setParentDivisionNo(parentNo);
        administrativeDivision.setDivisionDescription(description);
        if (pics.size()!=0){
            String fileNo = UUIDPK.UUIDGenerator.getUUID();
            for (int i=0;i<pics.size();i++){

                if (!(pics.get(i)).isEmpty()){

                    SystemFile systemFile = new SystemFile();
                    systemFile.setCreateUserNo(userNo);
                    systemFile.setFileNo(fileNo);
                    systemFile.setFileType(1);
                    systemFile.setFileTypeName("文件");
                    systemFile.setFileName(pics.get(i).substring(pics.get(i).lastIndexOf("/")+1,pics.get(i).length()));
                    systemFile.setFileAddress(pics.get(i));
                    int add1 = systemFileDao.insertSelective(systemFile);
                }
            }
            administrativeDivision.setPicNo(fileNo);

        }

        try {
            int add = administrativeDivisionDao.insertSelective(administrativeDivision);
            return RespBodyObj.ok();
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }

    }


    /**
     * 删除行政区域
     * @param
     * @return
     */
    @PostMapping(value = "division/deleteDivision",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj deleteByDivisionNo(@RequestBody ReqBodyObj<Map<String,List<String>>> param){

        List<String> divisionNos = param.getData().get("divisionNos");

        JSONObject jsonObject = new JSONObject();
        try {
            for (int i=0;i<divisionNos.size();i++){

                int delete = administrativeDivisionDao.deleteByDivisionNo(divisionNos.get(i));

            }


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


    /**
     * 获取行政区划树形
     * @return
     */
    @PostMapping(value = "division/getDivisionTree",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getDivisionTree(){
        AdministrativeDivisionQuery query  = new AdministrativeDivisionQuery();
        query.createCriteria().andParentDivisionNoEqualTo("0");
        List<AdministrativeDivision> provinceList = administrativeDivisionDao.selectByExample(query);

        List<Map<String,Object>> list1 = new ArrayList<>();//存放省集合
        if (provinceList.size()>0){
            for (AdministrativeDivision aa : provinceList){
                Map<String,Object> map1 = new HashMap<>();
                String id = aa.getDivisionNo();
                String name = aa.getDivisionName();
                map1.put("id",id);
                map1.put("text",name);

                AdministrativeDivisionQuery query1  = new AdministrativeDivisionQuery();
                query1.createCriteria().andParentDivisionNoEqualTo(id);
                List<AdministrativeDivision> cityList = administrativeDivisionDao.selectByExample(query1);

                if (cityList.size()>0){
                    List<Map<String,Object>> list2 = new ArrayList<>();
                    for (AdministrativeDivision bb : cityList){
                        Map<String,Object> map2 = new HashMap<>();
                        String id2 = bb.getDivisionNo();
                        String name2 = bb.getDivisionName();
                        map2.put("id",id2);
                        map2.put("text",name2);

                        AdministrativeDivisionQuery query2  = new AdministrativeDivisionQuery();
                        query2.createCriteria().andParentDivisionNoEqualTo(id2);
                        List<AdministrativeDivision> districtList = administrativeDivisionDao.selectByExample(query2);

                        if (districtList.size()>0){
                            List<Map<String,Object>> list3 = new ArrayList<>();
                            for (AdministrativeDivision cc : districtList){
                                Map<String,Object> map3 = new HashMap<>();
                                String id3 = cc.getDivisionNo();
                                String name3 = cc.getDivisionName();
                                map3.put("id",id3);
                                map3.put("text",name3);

                                AdministrativeDivisionQuery query3  = new AdministrativeDivisionQuery();
                                query3.createCriteria().andParentDivisionNoEqualTo(id3);
                                List<AdministrativeDivision> townList = administrativeDivisionDao.selectByExample(query3);
                                if (townList.size()>0){
                                    List<Map<String,Object>> list4 = new ArrayList<>();
                                    for (AdministrativeDivision dd : townList){
                                        Map<String,Object> map4 = new HashMap<>();
                                        String id4 = dd.getDivisionNo();
                                        String name4 = dd.getDivisionName();
                                        map4.put("id",id4);
                                        map4.put("text",name4);


                                        AdministrativeDivisionQuery query4  = new AdministrativeDivisionQuery();
                                        query4.createCriteria().andParentDivisionNoEqualTo(id4);
                                        List<AdministrativeDivision> villageList = administrativeDivisionDao.selectByExample(query4);

                                        if (villageList.size()>0){
                                            List<Map<String,Object>> list5 = new ArrayList<>();
                                            for (AdministrativeDivision ee : villageList){
                                                Map<String,Object> map5 = new HashMap<>();
                                                String id5 = ee.getDivisionNo();
                                                String name5 = ee.getDivisionName();
                                                map5.put("id",id5);
                                                map5.put("text",name5);
                                                list5.add(map5);
                                            }
                                            if (list5.size()>0){
                                                map4.put("children",list5);
                                            }
                                        }
                                        list4.add(map4);

                                    }
                                    if (list4.size()>0){
                                        map3.put("children",list4);

                                    }
                                }
                                list3.add(map3);
                            }
                            if (list3.size()>0){
                                map2.put("children",list3);
                            }
                        }

                        list2.add(map2);
                    }
                    if (list2.size()>0){
                        map1.put("children",list2);
                    }
                }
                list1.add(map1);
            }


        }
        return RespBodyObj.ok(list1);

    }

    /**
     * 行政区划树新(2 3 级)
     * @Author chenhonggang
     * @Date 2019-01-31
     * @return
     */
    @PostMapping(value = "division/getDivisionTreeNew",produces = "application/json;charset=UTF-8")
    @ResponseBody
//    @Cacheable(value = "divisionTree")
    public RespBodyObj getDivisionTreeNew(){
        List<Tree> provinceList = administrativeDivisionDao.getDivisionTreeNew("208754FE-9576-47F1-B409-2FA729E5FEAD");
        for(Tree t:provinceList){
            List<Tree> chileren = administrativeDivisionDao.getDivisionTreeNew(t.getId());
            t.setChildren(chileren);
        }
        return RespBodyObj.ok(provinceList);
    }



    /**
     * 根据地区编号查询河道信息
     * @param
     * @return
     */
    @PostMapping(value = "division/selectRiverByDivisionNo",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getRiverByDivisionNo(@RequestBody ReqBodyObj<Map<String,String>> param){
        String divisionNo = param.getData().get("divisionNo");
        List<RiverInfoBasis> list = new ArrayList<>();
        try {
           list = administrativeDivisionDao.selectRiverByDivisionNo(divisionNo);
           return RespBodyObj.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.ok(list);
        }
    }


    /**
     * 根据区域编号获取河流树形
     * @param
     * @return
     */
    @PostMapping(value = "river/getDivisionRiverTree",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getRiverTreeByDivisionNo(@RequestBody ReqBodyObj<Map<String,String>> param){
        String divisionNo = param.getData().get("divisionNo");
        List<RiverInfoBasis> list = new ArrayList<>();
        List<Map<String,Object>> dataList = new ArrayList<>();
        try {
            list = administrativeDivisionDao.selectRiverByDivisionNo(divisionNo);
            if (list.size()>0){
                for (RiverInfoBasis river: list){
                    Map<String,Object> map = new HashMap<>();
                    map.put("id",river.getRiverNo());
                    map.put("text",river.getRiverName());
                    dataList.add(map);
                }
            }
            return RespBodyObj.ok(dataList);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.ok(dataList);
        }
    }


    /**
     * 根据行政区划编号查看区域基本信息
     * @param
     * @return
     */
    @PostMapping(value = "division/getDivisionInfoByDivisionNo",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getDivisionInfoByDivisionNo(@RequestBody ReqBodyObj<Map<String,String>> param){
        String divisionNo =param.getData().get("divisionNo");
        AdministrativeDivisionQuery query = new AdministrativeDivisionQuery();
        query.createCriteria().andDivisionNoEqualTo(divisionNo);

        List<AdministrativeDivision> list = administrativeDivisionDao.selectByExample(query);
        return RespBodyObj.ok(list);

    }


    /**
     * 所有区县列表(可模糊查询)
     * @return
     */
    @PostMapping(value = "area/areaList",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj getDivsionList(@RequestBody ReqBodyObj<Map<String,String>> param){
        String str = param.getData().get("fuzzyQuery");
        AdministrativeDivisionQuery query = new AdministrativeDivisionQuery();
        if (str != null){
            query.createCriteria().andDivisionNameLike("%"+str+"%");
        }
        List<AdministrativeDivision> list = administrativeDivisionDao.selectByExample(query);
        return RespBodyObj.ok(list);
    }

    /**
     * 当前用户区县列表(可模糊查询)
     * @return
     */
    @PostMapping(value = "area/areaListCurrent",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj areaListCurrent(HttpServletRequest request){
        String userNo = request.getHeader("userNo");
        //query.createCriteria().andDivisionNameLike("%"+str+"%");
        List<AdministrativeDivision> list = administrativeDivisionDao.getCurrentDivision(userNo);
        return RespBodyObj.ok(list);
    }




}