package com.philisense.www.controller.web;

import com.philisense.www.common.config.CommonConst;
import com.philisense.www.service.ProOrgService;
import com.philisense.www.service.ProUserOrgService;
import com.philisense.www.annotation.log.OperateLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.philisense.www.controller.BaseUtilController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import java.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(CommonConst.WEB_REST_SERVICE_PREFICX+"prouserorg")
@Api(tags = "ProUserOrgControllerForWeb|一个用来操作-{用户机构权限表}-业务模块的控制器")
public class ProUserOrgControllerForWeb extends BaseUtilController{

        @Autowired
        private ProUserOrgService proUserOrgService;

        @Autowired
        private ProOrgService proOrgService;


        @GetMapping( "/delete")
        @ApiOperation(value="根据-{用户机构权限表}-主键删除记录", notes="成功返回:{\"success\":true,\"message\",\"数据删除成功\"},失败返回:{\"success\":false,\"message\",\"数据删除失败\"}")
        @ApiImplicitParam(paramType="query", name = "recordid", value = "记录主键", required = true, dataTypeClass = String.class)
        @OperateLog(name = "根据-{用户机构权限表}-主键删除记录",value = "delete",type = "PC")
        public Map<String, Object> deleteByPrimaryKeyForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestParam String recordid)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                int flag = proUserOrgService.deleteByPrimaryKey(recordid);
                if(1<=flag) {
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, DELETE_SUCCESS_MSG);
                }else{
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, DELETE_FAIL_MSG);
                }

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, DELETE_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @GetMapping( "/get")
        @ApiOperation(value="根据-{用户机构权限表}-主键查询记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"datarecord\":\"{}\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParam(paramType="query", name = "recordid", value = "记录主键", required = true, dataTypeClass = String.class)
        @OperateLog(name = "根据-{用户机构权限表}-主键查询记录",value = "query",type = "PC")
        public Map<String, Object> selectByPrimaryKeyForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestParam String recordid)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATARECORD, proUserOrgService.selectByPrimaryKey(recordid));
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/insert")
        @ApiOperation(value="根据-{用户机构权限表}-对象插入记录", notes="成功返回:{\"success\":true,\"message\",\"数据插入成功\"},失败返回:{\"success\":false,\"message\",\"数据插入失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象插入记录",value = "insert",type = "PC")
        public Map<String, Object>  insertForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                map.put("id", UUID.randomUUID().toString().replaceAll("-", ""));

                proUserOrgService.insert(map);
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, INSERT_SUCCESS_MSG);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, INSERT_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/update")
        @ApiOperation(value="根据-{用户机构权限表}-对象更新记录", notes="成功返回:{\"success\":true,\"message\",\"数据更新成功\"},失败返回:{\"success\":false,\"message\",\"数据更新失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象更新记录",value = "update",type = "PC")
        public Map<String, Object>  updateForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                Map<String, Object> paramsmap = proUserOrgService.selectByPrimaryKey(map.get("id")+"");

                int flag = 0;

                if( null!=paramsmap &&paramsmap.size()>0) {
                paramsmap.putAll(map);
                flag = proUserOrgService.update(paramsmap);
                }

                if(1<=flag){
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, UPDATE_SUCCESS_MSG);
                }else{
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, UPDATE_FAIL_MSG);
                }

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, UPDATE_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/deleteByMap")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map删除记录", notes="成功返回:{\"success\":true,\"message\",\"数据删除成功\"},失败返回:{\"success\":false,\"message\",\"数据删除失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map删除记录",value = "delete",type = "PC")
        public Map<String, Object>  deleteByMapForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                int flag = proUserOrgService.deleteByMap(map);
                if(1<=flag) {
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, DELETE_SUCCESS_MSG);
                }else{
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, DELETE_FAIL_MSG);
                }

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, DELETE_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/updateByMap")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map更新记录", notes="成功返回:{\"success\":true,\"message\",\"数据更新成功\"},失败返回:{\"success\":false,\"message\",\"数据更新失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map更新记录",value = "update",type = "PC")
        public Map<String, Object>  updateByMapForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {


                int flag = proUserOrgService.updateByMap(map);
                if(1<=flag){
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, UPDATE_SUCCESS_MSG);
                }else{
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, UPDATE_FAIL_MSG);
                }

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, UPDATE_FAIL_MSG+e.getMessage());
                }
                return result;
        }
        @PostMapping( "/getOne")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map查询记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"datarecord\":\"{}\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map查询记录",value = "query",type = "PC")
        public Map<String, Object>  getOneForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATARECORD, proUserOrgService.getOne(map));
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/getAll")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map查询所有记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map查询所有记录",value = "query",type = "PC")
        public Map<String, Object>  getAllForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
                {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATALIST_VUE, proUserOrgService.getAll(map));
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/queryPage")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map分页记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
            @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map分页记录",value = "query",type = "PC")
        public Map<String, Object>  queryPageForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                int currentPage = map.get(DATACURRENT)!=null?Integer.parseInt(map.get(DATACURRENT)+"")-1:0;
                int pageSize = map.get(PAGESIZE)!=null?Integer.parseInt(map.get(PAGESIZE)+""):10;
                int pageStart = currentPage*pageSize ;


                map.put(PAGESIZE,pageSize);
                map.put(PAGESTART,pageStart);

                int total = proUserOrgService.count(map);
                List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
                if(total>0){
                datalist = proUserOrgService.queryPage(map);
                }
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATALIST,datalist);
                Map<String, Object> pagemode = new HashMap<String, Object>();
                pagemode.put(DATATOTAL,total);
                pagemode.put(PAGESIZE,map.get(PAGESIZE)!=null?map.get(PAGESIZE):10);
                pagemode.put(DATACURRENT,map.get(DATACURRENT)!=null?map.get(DATACURRENT):0);
                result.put(PAGINATION,pagemode);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }


        @PostMapping( "/queryPageVue")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map分页记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
            @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map分页记录",value = "query",type = "PC")
        public Map<String, Object>  queryPageVueForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                int currentPage = map.get(DATACURRENT_VUE)!=null?Integer.parseInt(map.get(DATACURRENT_VUE)+"")-1:0;
                int pageSize = map.get(PAGESIZE_VUE)!=null?Integer.parseInt(map.get(PAGESIZE_VUE)+""):10;
                int pageStart = currentPage*pageSize ;


                map.put(PAGESIZE_VUE,pageSize);
                map.put(PAGESTART,pageStart);

                int total = proUserOrgService.count(map);
                List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
                if(total>0){
                datalist = proUserOrgService.queryPage(map);
                }
                Map<String, Object> tempresult = new HashMap<String, Object>();
                tempresult.put(DATASUCCESS, SUCCESSFLAG);
                tempresult.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                tempresult.put(DATALIST_VUE,datalist);
                tempresult.put(DATATOTAL_VUE,total);
                tempresult.put(PAGESIZE_VUE,map.get(PAGESIZE_VUE)!=null?map.get(PAGESIZE_VUE):10);
                tempresult.put(DATACURRENT_VUE,map.get(DATACURRENT_VUE)!=null?map.get(DATACURRENT_VUE):0);
                result.put(DATARESULT, tempresult);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }



        @PostMapping( "/queryPageOracle")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map分页记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
            @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map分页记录",value = "query",type = "PC")
        public Map<String, Object>  queryPageOracleForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                int currentPage = map.get(DATACURRENT)!=null?Integer.parseInt(map.get(DATACURRENT)+""):1;
                int pageSize = map.get(PAGESIZE)!=null?Integer.parseInt(map.get(PAGESIZE)+""):10;
                int pageStart = currentPage*pageSize ;


                map.put(PAGESTART,(currentPage-1)*pageSize);
                map.put(PAGEEND,currentPage*pageSize);

                int total = proUserOrgService.count(map);
                List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
                if(total>0){
                datalist = proUserOrgService.queryPage(map);
                }
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATALIST,datalist);
                Map<String, Object> pagemode = new HashMap<String, Object>();
                pagemode.put(DATATOTAL,total);
                pagemode.put(PAGESIZE,map.get(PAGESIZE)!=null?map.get(PAGESIZE):10);
                pagemode.put(DATACURRENT,map.get(DATACURRENT)!=null?map.get(DATACURRENT):0);
                result.put(PAGINATION,pagemode);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/count")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map统计查询总数记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map统计查询总数记录",value = "query",type = "PC")
        public Map<String, Object>  countForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
                {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATATOTAL_VUE, proUserOrgService.count(map));
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/insertBatch")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map批量插入记录", notes="成功返回:{\"success\":true,\"message\",\"数据插入成功\"},失败返回:{\"success\":false,\"message\",\"数据插入失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map批量插入记录",value = "insert",type = "PC")
        public Map<String, Object>  insertBatchForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
                {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                        List<Map<String, Object>> list = (List)map.get("orgids");
                        if(null!=list && list.size()>0) {
                                for (Map<String, Object> stringObjectMap : list) {
                                        Map<String, Object> one = proOrgService.getOne(new HashMap<String, Object>() {{
                                                put("org_code", stringObjectMap.get("org_code"));
                                        }});
                                        if(null != one) {
                                                stringObjectMap.put("org_id", one.get("org_id"));
                                        }
                                }
                                proUserOrgService.deleteBatch(list);//先把机构与用户删除
                        }
                        List<Map<String, Object>> list1 = (List)map.get("orgids_userids");
                        if(null!=list1 && list1.size()>0) {
                                for (Map<String, Object> stringObjectMap : list1) {
                                        Map<String, Object> one = proOrgService.getOne(new HashMap<String, Object>() {{
                                                put("org_code", stringObjectMap.get("org_code"));
                                        }});
                                        if(null != one) {
                                                stringObjectMap.put("org_id", one.get("org_id"));
                                                stringObjectMap.put("org_code", one.get("org_code"));
                                                stringObjectMap.put("org_name", one.get("org_name"));
                                        }

                                }
                                proUserOrgService.insertBatch(list1);//再重新插入机构和用户
                        }
                        result.put(DATASUCCESS, SUCCESSFLAG);
                        result.put(DATAMESSAGE, INSERT_SUCCESS_MSG);

                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, INSERT_SUCCESS_MSG);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, INSERT_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/deleteBatch")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map批量删除记录", notes="成功返回:{\"success\":true,\"message\",\"数据删除成功\"},失败返回:{\"success\":false,\"message\",\"数据删除失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map批量删除记录",value = "delete",type = "PC")
        public Map<String, Object>  deleteBatchForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody List<Map<String, Object>> list)
                {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                proUserOrgService.deleteBatch(list);
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, DELETE_SUCCESS_MSG);

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, DELETE_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/updateBatch")
        @ApiOperation(value="根据-{用户机构权限表}-对象Map批量更新记录", notes="成功返回:{\"success\":true,\"message\",\"数据更新成功\"},失败返回:{\"success\":false,\"message\",\"数据更新失败\"}")
        @ApiImplicitParams({
        @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{用户机构权限表}-对象Map批量更新记录",value = "update",type = "PC")
        public Map<String, Object>  updateBatchForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestBody List<Map<String, Object>> list)
                {
                Map<String, Object> result = new HashMap<String, Object>();
                try {


                proUserOrgService.updateBatch(list);
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, UPDATE_SUCCESS_MSG);

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, UPDATE_FAIL_MSG+e.getMessage());
                }
                return result;
        }





        @GetMapping( "/export")
        @ApiOperation(value="根据-{{用户机构权限表}}-对象Map导出所有记录", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{{用户机构权限表}}-对象Map导出所有记录",value = "query",type = "PC")
        public void exportForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestParam Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {

                List<Map<String, Object>> rows = proUserOrgService.getAll(map);
                // 通过工具类创建writer
                //创建xlsx格式的
                ExcelWriter writer = ExcelUtil.getWriter(true);
                String filename = "用户机构权限表";
                getWriter(writer,filename);
                writer.setHeaderAlias(getKVAliasMap());


                writer.setOnlyAlias(true);

                // 一次性写出内容，使用默认样式，强制输出标题
                writer.write(rows, true);

                //response为HttpServletResponse对象
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
                response.setHeader("Content-Disposition","attachment;filename="+filename+"_ProUserOrg.xlsx");
                ServletOutputStream out=response.getOutputStream();
                writer.flush(out, true);
                // 关闭writer，释放内存
                writer.close();
                //此处记得关闭输出Servlet流
                IoUtil.close(out);

                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
        }



        @GetMapping( "/exportTemplate")
        @ApiOperation(value="根据-{{用户机构权限表}}-下载导出模板", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{{用户机构权限表}}-下载导出模板",value = "query",type = "PC")
        public void exportTemplateForProUserOrgForWeb(HttpServletRequest request , HttpServletResponse response,@RequestParam Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                        String filename = "用户机构权限表";
                        InputStream resource = getClass().getClassLoader().getResourceAsStream("excelTemplate/prouserorg" + "/"+filename+"_ProUserOrg.xlsx");
                        //response为HttpServletResponse对象
                        response.setContentType("application/vnd.ms-excel;charset=utf-8");
                        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
                        response.setHeader("Content-Disposition","attachment;filename="+filename+"_ProUserOrg.xlsx");
                        ServletOutputStream out=response.getOutputStream();
                        IoUtil.copy(resource,out);
                        //此处记得关闭输出Servlet流
                        IoUtil.close(out);
                        IoUtil.close(resource);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
        }



        @PostMapping( "/importExcel")
        @ApiOperation(value="根据-{{用户机构权限表}}-对象Map批量导入记录", notes="成功返回:{\"success\":true,\"message\",\"数据更新成功\"},失败返回:{\"success\":false,\"message\",\"数据更新失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "user_id", value = "用户Id", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_id", value = "机构Id", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_code", value = "机构编码", required = true, dataTypeClass = String.class),
                @ApiImplicitParam(paramType="body", name = "org_name", value = "机构名字", required = true, dataTypeClass = String.class)
        })
        @OperateLog(name = "根据-{{用户机构权限表}}-对象Map批量导入记录",value = "update",type = "PC")
        public Map<String, Object>  importExcelForProUserOrgForWeb(String baseDir, MultipartFile file) {
                Map<String, Object> result = new HashMap<String, Object>();

                if (!file.isEmpty()) {
                        try {
                        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());

                        reader.setHeaderAlias(getVKAliasMap());


                        List<Map<String, Object>> list = new ArrayList<>();
                        List<Map<String, Object>> read = reader.read(1, 2, Integer.MAX_VALUE);

                        for (Map<String,Object> map:read){


                        if ("".equals(map.get("user_id"))){
                        map.put("user_id",null);
                        }

                        if ("".equals(map.get("org_id"))){
                        map.put("org_id",null);
                        }

                        if ("".equals(map.get("org_code"))){
                        map.put("org_code",null);
                        }

                        if ("".equals(map.get("org_name"))){
                        map.put("org_name",null);
                        }


                        list.add(map);

                        }
                        proUserOrgService.insertBatch(list);

                        result.put(DATASUCCESS, SUCCESSFLAG);
                        result.put(DATAMESSAGE, INSERT_SUCCESS_MSG);
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, INSERT_FAIL_MSG);
                return result;
        }



        protected void getWriter(ExcelWriter writer,String filename){
        // 通过工具类创建writer
        //创建xlsx格式的
                        writer.setColumnWidth(0,25);
                        writer.setColumnWidth(1,25);
                        writer.setColumnWidth(2,25);
                        writer.setColumnWidth(3,25);
                        // 合并单元格后的标题行，使用默认标题样式
                        writer.merge(4, filename);

        }



        protected Map<String,String> getKVAliasMap(){

        Map<String,String> aliasMap = new LinkedHashMap<>();

                        aliasMap.put("user_id", "用户Id");
                        aliasMap.put("org_id", "机构Id");
                        aliasMap.put("org_code", "机构编码");
                        aliasMap.put("org_name", "机构名字");

        return aliasMap;

        }

        protected Map<String,String> getVKAliasMap(){
        Map<String,String> aliasMap = new LinkedHashMap<>();

                        aliasMap.put("用户Id", "user_id");
                        aliasMap.put("机构Id", "org_id");
                        aliasMap.put("机构编码", "org_code");
                        aliasMap.put("机构名字", "org_name");

        return aliasMap;
        }
        @PostMapping( "/getPageProUserByOrgId")
        @ApiOperation(value="根据机构ID查询用户列表", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "org_id", value = "机构ID", required = true,  dataTypeClass = String.class)
        })
        @OperateLog(name = "根据机构ID查询用户列表",value = "query",type = "PC")
        public Map<String, Object>  getPageProUserByOrgId(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                	Map<String, Object> mapauthids = new HashMap<String, Object>();
                int currentPage = map.get(DATACURRENT)!=null?Integer.parseInt(map.get(DATACURRENT)+"")-1:0;
                int pageSize = map.get(PAGESIZE)!=null?Integer.parseInt(map.get(PAGESIZE)+""):10;
                int pageStart = currentPage*pageSize ;


                mapauthids.put("org_id",map.get("org_id"));
                mapauthids.put(PAGESIZE_VUE,pageSize);
                mapauthids.put(PAGESTART,pageStart);
                int total = proUserOrgService.getCountProUserByOrgId(mapauthids);
                List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
                if(total>0){
                        datalist = proUserOrgService.getPageProUserByOrgId(mapauthids);
                }
                result.put(DATASUCCESS, SUCCESSFLAG);
                result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                result.put(DATALIST,datalist);
                Map<String, Object> pagemode = new HashMap<String, Object>();
                pagemode.put(DATATOTAL,total);
                pagemode.put(PAGESIZE,map.get(PAGESIZE)!=null?map.get(PAGESIZE):10);
                pagemode.put(DATACURRENT,map.get(DATACURRENT)!=null?map.get(DATACURRENT):0);
                result.put(PAGINATION,pagemode);
                }catch(Exception e){
                result.put(DATASUCCESS, ERRORFLAG);
                result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }
        @PostMapping( "/getPageProUserByOrgIdVue")
        @ApiOperation(value="根据机构ID查询用户列表", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "org_id", value = "机构ID", required = true,  dataTypeClass = String.class)
        })
        @OperateLog(name = "根据机构ID查询用户列表",value = "query",type = "PC")
        public Map<String, Object>  getPageProUserByOrgIdVue(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {


                        Map<String, Object> mapauthids = new HashMap<String, Object>();

                        int currentPage = map.get(DATACURRENT_VUE)!=null?Integer.parseInt(map.get(DATACURRENT_VUE)+"")-1:0;
                        int pageSize = map.get(PAGESIZE_VUE)!=null?Integer.parseInt(map.get(PAGESIZE_VUE)+""):10;
                        int pageStart = currentPage*pageSize ;
                        mapauthids.put("org_id",map.get("org_id"));
                        mapauthids.put(PAGESIZE_VUE,pageSize);
                        mapauthids.put(PAGESTART,pageStart);
                        int total = proUserOrgService.getCountProUserByOrgId(mapauthids);
                        List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
                        if(total>0){
                                datalist = proUserOrgService.getPageProUserByOrgId(mapauthids);
                        }
                        result.put(DATASUCCESS, SUCCESSFLAG);
                        result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                        result.put(DATALIST_VUE,datalist);
                        Map<String, Object> pagemode = new HashMap<String, Object>();
                        pagemode.put(DATATOTAL_VUE,total);
                        pagemode.put(PAGESIZE_VUE,map.get(PAGESIZE_VUE)!=null?map.get(PAGESIZE_VUE):10);
                        pagemode.put(DATACURRENT_VUE,map.get(DATACURRENT_VUE)!=null?map.get(DATACURRENT_VUE):0);
                        result.put(PAGINATION,pagemode);

                }catch(Exception e){
                        result.put(DATASUCCESS, ERRORFLAG);
                        result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/getSelectedProOrgByUserId")
        @ApiOperation(value="查询所有机构数据根据用户id", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "user_id", value = "用户ID", required = true,  dataTypeClass = String.class)
        })
        @OperateLog(name = "查询所有机构数据根据用户id",value = "query",type = "PC")
        public Map<String, Object>  getSelectedProOrgByUserId(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                        if(null!= map.get("user_id")&&!(map.get("user_id").equals(""))){
                            List<Map<String,Object>> datalist = proUserOrgService.getSelectedProOrgByUserId(map);
                            
                            List<String> list = new ArrayList<String>();
                            if(null!=datalist && datalist.size()>0){
                                    for(Map<String,Object> s:datalist){
                                            list.add(s.get("org_code")+"");
                                    }
                            }
                            
                            result.put(DATASUCCESS, SUCCESSFLAG);
                            result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                            result.put(DATALIST,list);
                        }else {
                            result.put(DATASUCCESS, ERRORFLAG);
                            result.put(DATALIST,new ArrayList());
                            result.put(DATAMESSAGE, "用户参数为空！");
                        }
                     

                }catch(Exception e){
                        result.put(DATASUCCESS, ERRORFLAG);
                        result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

        @PostMapping( "/getSelectedProOrgByUserIdVue")
        @ApiOperation(value="查询所有机构数据根据用户id", notes="成功返回:{\"success\":true,\"message\",\"数据查询成功\",\"data\":\"[]\"},失败返回:{\"success\":false,\"message\",\"数据查询失败\"}")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType="body", name = "user_id", value = "用户ID", required = true,  dataTypeClass = String.class)
        })
        @OperateLog(name = "查询所有机构数据根据用户id",value = "query",type = "PC")
        public Map<String, Object>  getSelectedProOrgByUserIdVue(HttpServletRequest request , HttpServletResponse response,@RequestBody Map<String, Object> map)
        {
                Map<String, Object> result = new HashMap<String, Object>();
                try {
                        if(null!= map.get("user_id")&&!(map.get("user_id").equals(""))){
                            List<Map<String,Object>> datalist = proUserOrgService.getSelectedProOrgByUserId(map);
                            
                            List<String> list = new ArrayList<String>();
                            if(null!=datalist && datalist.size()>0){
                                    for(Map<String,Object> s:datalist){
                                            list.add(s.get("org_code")+"");
                                    }
                            }
                            
                            result.put(DATASUCCESS, SUCCESSFLAG);
                            result.put(DATAMESSAGE, QUERY_SUCCESS_MSG);
                            result.put(DATALIST_VUE,list);
                        }else {
                            result.put(DATASUCCESS, ERRORFLAG);
                            result.put(DATALIST_VUE,new ArrayList());
                            result.put(DATAMESSAGE, "用户参数为空！");
                        }
                     

                }catch(Exception e){
                        result.put(DATASUCCESS, ERRORFLAG);
                        result.put(DATAMESSAGE, QUERY_FAIL_MSG+e.getMessage());
                }
                return result;
        }

}
