package com.palmnest.webapp.controller.wechat;

import cn.superdesk.libs.common.json.JsonUtils;

import com.palmnest.application.AppConstants;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppCommonResultDto;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.GenPass;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.VirInfoDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.GoodsReleaseBillInputDto;
import com.palmnest.application.dto.input.GoodsReleaseInputDto;
import com.palmnest.application.dto.output.GoodsReleaseOutDto;
import com.palmnest.application.dto.output.VirAccountDto;
import com.palmnest.application.dto.output.enterprise.GoodsReleaseVo;
import com.palmnest.application.dto.output.returnCompanyDto;
import com.palmnest.model.Element;
import com.palmnest.model.User;
import com.palmnest.model.dinner.Department;
import com.palmnest.model.enterprise.VirPermConfig;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.model.release.GoodsRelease;
import com.palmnest.service.ElementGroupManager;
import com.palmnest.service.RoleManager;
import com.palmnest.service.dinner.manager.DeptManager;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.estateserve.EnterprisePermissionManager;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.service.estateserve.release.GoodsReleaseManager;
import com.palmnest.service.estateserve.release.GoodsReleaseService;
import com.palmnest.service.property.PropertyManager;
import com.palmnest.service.redis.BaseRedisService;
import com.palmnest.service.virtProject.VirtualArchService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;


/**
 * 电子放行条 - 微信端
 */
@Slf4j
@Controller
@RequestMapping(value = "/wechat/release")
public class WxGoodsReleaseController {


	@Autowired
	private GoodsReleaseService goodsReleaseService;
	
	@Autowired
	private ElementGroupManager elementGroupManager;
	
	@Autowired
	private VirtualArchManager  virtualArchManager;
	
	@Autowired
	private ScWxuserManager     scWxuserManager;
	
	@Autowired
	private GoodsReleaseManager goodsReleaseManager;
	
	@Autowired
	private BaseRedisService    baseRedisService;
	
	@Autowired
	private VirtualArchService  virtualArchService;
	
	@Autowired
	private AppUserManager      appUserManager;
	
	@Autowired
	private PropertyManager     propertyManager;
	
	@Autowired
	private DeptManager                 deptManager;
	
	@Autowired
	private EnterprisePermissionManager enterprisePermissionManager;
	
	@Autowired
	private RoleManager           roleManager;


	@RequestMapping(value = "getGoodsCompany")
	@ResponseBody
	public AppResultDto getGoodsCompany(@RequestParam("orgId") Long orgId, HttpServletResponse response) {
		log.info("WxGoodsReleaseController getGoodsCompany======");
		GoodsReleaseVo list= goodsReleaseService.getGoodsCompany(orgId);

		List<Map<String, Object>>  listProcessName = enterprisePermissionManager.getProcessName(orgId,"1,2");
		List<Map<String, Object>> listProcessNameMap  = new ArrayList<>();
		if(listProcessName.size()>=1){
            for(Map processNameMap : listProcessName){
               if(processNameMap.containsKey("processName")){
                    if(null != processNameMap.get("processName") &&
							StringUtils.isNotEmpty(processNameMap.get("processName").toString())){
						listProcessNameMap.add(processNameMap);
					}
			   }
			}
		}

		if(listProcessNameMap.size()>=1){
			list.setListProcessName(listProcessName);
		}


		return AppResultUtil.createSuccesDto(list);
	}

	/**
	 * 提交物品放行申请
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/apply")
	@ResponseBody
	public AppResultDto apply(@ModelAttribute("goodsReleaseBillInputDto") GoodsReleaseBillInputDto dto, MultipartHttpServletRequest request) {

		log.info("WxGoodsReleaseController apply======{}",JsonUtils.toJson(dto));
		AppResultDto rdto = goodsReleaseService.apply(dto, request);
		try {
			GoodsReleaseOutDto gDto = (GoodsReleaseOutDto) ((AppCommonResultDto) rdto).getData();
			log.info("WxGoodsReleaseController gDto======{}",JsonUtils.toJson(gDto));
			goodsReleaseService.push(gDto.getId());
		} catch (Exception e) {
			log.info("WxGoodsReleaseController catch======{}",e.getMessage());
			e.printStackTrace();
		}
		return rdto;
	}


	/**
	 * 放行历史
	 *
	 * @return
	 */
	@RequestMapping(value = "/mine")
	public ModelAndView mine() {
		//报修方式 区域 类型 小类型
		Model model = new ExtendedModelMap();

		return new ModelAndView("wechat/mine", model.asMap());
	}

	/**
	 * 放行历史
	 *
	 * @return
	 */
	@RequestMapping(value = "/changePhone")
	public ModelAndView changePhone() {
		//报修方式 区域 类型 小类型
		Model model = new ExtendedModelMap();

		return new ModelAndView("wechat/changePhone", model.asMap());
	}


	/**
	 * 物品放行首页
	 * 需要参数
	 */
	@RequestMapping(value = "/index")
	@ResponseBody
	public AppResultDto index(HttpServletRequest request) {

		log.info("WxGoodsReleaseController index======");

		GoodsReleaseInputDto dto = new GoodsReleaseInputDto();

		//放行类型
		List<Element> releaseTypeGroup = elementGroupManager.getElementsByGroupName("releaseTypeGroup");
		dto.setReleaseTypeGroup(elementGroupManager.convert(releaseTypeGroup));
		//User user = (User) request.getSession().getAttribute("UserCache");	

		//dto.setApplyUser(user.getUsername());
		//dto.setApplyTel(user.getTelephone());

		return AppResultUtil.createSuccesDto(dto);
	}


	/**
	 * 物品放行根据企业账号查询企业信息
	 */
	@RequestMapping(value = "/getVirByAccount")
	@ResponseBody
	public AppResultDto getVirByAccount(@RequestParam Long virAccountId) {


		log.info("WxGoodsReleaseController getVirByAccount virAccountId:" + virAccountId);

		VirAccountDto dto = virtualArchService.getVirAccountBy(virAccountId);
		return AppResultUtil.createSuccesDto(dto);
	}

	/**
	 * 物品放行根据个人账号查询企业信息
	 */
	@RequestMapping(value = "/getVirByUser")
	@ResponseBody
	public AppResultDto getVirByUser(@RequestParam Integer userId) {

		log.info("WxGoodsReleaseController getVirByUser userId:" + userId);
		AppUser             user = appUserManager.get(userId);
		Map<String, Object> map  = new HashMap<String, Object>();
		if (user != null &&null!=user.getVirtualArchId()) {
			VirtualArchitecture v = virtualArchService.findById(user.getVirtualArchId());
			
			//Department dept = deptManager.get(Long.parseLong(user.getDepartmentId().toString()));
			
			
			if (v != null) {
				map.put("compId", v.getId());
				map.put("compName", v.getName());
				map.put("rank", "");
				//if(StringUtils.isNotBlank(dept.getRank())){
				//	map.put("rank", dept.getRank());
				//}
				
			} else {
				return AppResultUtil.createErrorDto(AppConstants.ERROR_CODE, "白名单没有绑定企业,请联系系统管理员!");
			}
		}else{
			return AppResultUtil.createErrorDto(AppConstants.ERROR_CODE, "白名单没有绑定企业,请联系系统管理员!");
		}
		log.info("WxGoodsReleaseController getVirByUser map:" + map.toString());
		return AppResultUtil.createSuccesDto(map);
	}

	//

	/**
	 * 装修 根据项目查询企业信息
	 */
	@RequestMapping(value = "/getVirInfoDtoByOrgId")
	@ResponseBody
	public AppResultDto getVirInfoDtoByOrgId(@RequestParam String orgId, String virName) {

		log.info("WxGoodsReleaseController getVirInfoDtoByOrgId orgId:" + orgId);
		log.info("WxGoodsReleaseController getVirInfoDtoByOrgId virName:" + virName);

		try {
			List<VirInfoDto> listVirInfoDtos = virtualArchManager.getVirInfoDtoByOrgId(orgId, virName);
			return AppResultUtil.createSuccesDto(listVirInfoDtos);

		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询失败");
		}


	}
	//


	/**
	 * 当前微信用户是企业负责人
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/indexCompanyHead")
	@ResponseBody
	public AppResultDto indexAudit(HttpServletRequest request) {

		log.info("WxGoodsReleaseController indexAudit======");
		try {
			//如果是这个企业的负责人
			Map    returnMap = new HashMap();
			String openId    = request.getParameter("openId");
			//查询当前用户 是那个企业的负责人 且有物品放行审批权限
			List<Map<String, Object>> ListVirIds = goodsReleaseManager.getVirIdsByOpenId(openId, 1);
			if (null != ListVirIds) {
				returnMap.put("isCompanyHead", "1");
			} else {
				returnMap.put("isCompanyHead", "0");
			}
			return AppResultUtil.createSuccesDto(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询失败");
		}
	}

	/**
	 * 物品放行首页脚标数量
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/indexTotal")
	@ResponseBody
	public AppResultDto indexTotal(HttpServletRequest request) {

		log.info("WxGoodsReleaseController indexTotal======");

		try {
			//如果是这个企业的负责人
			Map<String, Object> params = new HashMap<String, Object>();
			//params.put("releaseType", "280");//用数据字典
			
	      /*if(StringUtils.isNotEmpty(String.valueOf(dto.getCompanyid()))){
				List<VirtUserInfo> list= virtUserDao.getVirUserByVirId(Integer.valueOf(String.valueOf(dto.getCompanyid())));
				if(list!=null&&list.size()>0){
					for(int k=0;k<list.size();k++){
						VirtUserInfo virUserInfo=list.get(k);
						int userId=virUserInfo.getUserId();//这个是tb_user表中的id
						WxUser wxUser=scWxuserManager.getScWxuserByUserId(userId);
					}
			  }
			}	*/

			String openId = request.getParameter("openId");
			//查询当前用户 是那个企业的负责人 且有物品放行审批权限
			List<Map<String, Object>> ListVirIds = goodsReleaseManager.getVirIdsByOpenId(openId, 1);
			String                    virIds     = "'#'";
			if (null != ListVirIds) {
				for (Map<String, Object> obj : ListVirIds) {
					if (obj.containsKey("virId")) {
						virIds = virIds + ",'" + obj.get("virId") + "'";
					}
				}
			}


			//不为空 代表处理过 就不查
			//当前企业id
			//找当前的公司id 和记录里匹配
			params.put("companyHeadUserIsNull", "companyHeadUserIsNull");//为空的表不没有处理过的
			params.put("virIds", virIds);
			Integer num       = goodsReleaseService.findCountBy(params);
			Map     returnMap = new HashMap();
			returnMap.put("auditNum", num + "");
			return AppResultUtil.createSuccesDto(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询失败");
		}
	}


	/**
	 * 物品放行首页脚标数量
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/indexPage")
	@ResponseBody
	public AppResultDto indexPage(HttpServletRequest request) {

		log.info("WxGoodsReleaseController indexTotal======");

		try {


			//Map<String, Object> params = new HashMap<String, Object>();
			//String openId = request.getParameter("openId");
			String virAccount    = request.getParameter("virAccountId");
			String userId        = request.getParameter("userId");
			String releaseStatus = request.getParameter("releaseStatus");

//			Long virAccountId = null;
//
//
//			System.out.println("virAccountId:"+virAccount);
//			System.out.println("userId:"+userId);
//
//			if(StringUtils.isNotEmpty(virAccount)) {
//				virAccountId = Long.parseLong(virAccount);
//			}
//			if(virAccountId != null && StringUtils.isEmpty(userId)) {
//				VirAccountDto dto = virtualArchService.getVirAccountBy(virAccountId);
//				Integer virId = dto.getCompId();
//				Map<String,Object> map = propertyManager.getVirtualArchitectureMap(virId.toString());
//				if(map != null){
//					String relation_id = null;
//					if(map.get("relation_id") != null){
//						relation_id = map.get("relation_id").toString();
//						if(StringUtils.isNotEmpty(relation_id)){
//							relation_id += "," + virId;
//						}else{
//							relation_id = virId.toString();
//						}
//						params.put("virIds", relation_id);
//					}else{
//						params.put("virIds", virId);
//					}
//				}else{
//					params.put("virIds", virId);
//				}
//			}
//			if(StringUtils.isNotEmpty(userId)) {
//				params.put("userId", Integer.parseInt(userId));
//				params.put("virAccountId", null);
//			}
//			params.put("releaseStatus", releaseStatus);	
//			params.put("applyType", 1);				
//			
//			Page page = goodsReleaseService.findPageBy(params, 1, 1);	

			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", userId);
			//params.put("virtualArchId", userInfo.getVirtualArchId());
			params.put("releaseStatus", "1,10");
			params.put("applyType", 1);
			List<GoodsRelease> listGoodsRelease = goodsReleaseManager.getGoodsReleaseByParams(params);
			Map                returnMap        = new HashMap();

			returnMap.put("releaseId", 0);
			if (listGoodsRelease.size() >= 1) {
				//dto.setPassRecordStatus(listGoodsRelease.get(0).getReleaseStatus());
				String gen_id = URLEncoder.encode(GenPass.Encrypt(listGoodsRelease.get(0).getId().toString()), "UTF-8");
				returnMap.put("releaseId", gen_id);
				returnMap.put("releaseStatus", listGoodsRelease.get(0).getReleaseStatus());
			}


			returnMap.put("url", "https://www.baidu.com/");

			return AppResultUtil.createSuccesDto(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询失败");
		}
	}


	/**
	 * 物品放行
	 * 选择公司
	 *
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectCompany")
	@ResponseBody
	public AppResultDto selectCompany(HttpServletRequest request) throws Exception {

		String organizationId    = request.getParameter("organizationId");
		int    organizationIntId = 10;
		if (StringUtils.isNotEmpty(organizationId)) {
			organizationIntId = Integer.valueOf(organizationId);
		}

		Map<String, Set<returnCompanyDto>> returnMap = virtualArchManager.getFloorCompany(organizationIntId);

		return AppResultUtil.createSuccesDto(returnMap);
	}

	/**
	 * @param request
	 * @return 物品放行列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public WebPageResultDto list(HttpServletRequest request) {

		log.info("WxGoodsReleaseController list======");

		try {
			int                 pageSize = Integer.parseInt(request.getParameter("pageSize"));
			int                 pageNo   = Integer.parseInt(request.getParameter("pageNo"));
			Map<String, Object> params   = new HashMap<String, Object>();
			//String openId = request.getParameter("openId");
			String virAccount = request.getParameter("virAccountId");
			String companyId  = request.getParameter("companyId");//公司Id
			String isAuditPage    = request.getParameter("isAuditPage");//是否审核页面
			if (org.apache.commons.lang3.StringUtils.isEmpty(companyId)) {
				return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "公司不能为空", "");
			}
			String userId        = request.getParameter("userId");
			String releaseStatus = request.getParameter("releaseStatus");
			String applyType     = request.getParameter("applyType");

			//String roleType=request.getParameter("roleType");//1放行记录 5马上审批
			//WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
			Long virAccountId = null;


			System.out.println("virAccountId:" + virAccount);
			System.out.println("userId:" + userId);
			AppUser   user = null;
			if(StringUtils.isNotEmpty(userId)){
				user = appUserManager.get(Integer.valueOf(userId));
			}

			Long  virPermId  = null;
			if(null != user && null != user.getVirPermId()){
				virPermId = user.getVirPermId();
			}

			boolean isVirPermId = false;
			boolean isDepPermId = false;
			String  userPermId= ""; //用户权限
			if(null != virPermId){
				VirPermConfig config = enterprisePermissionManager.get(virPermId);
				userPermId = config.getId().toString();
				
				if(null != config && StringUtils.isNotEmpty(config.getCode())){
					if(config.getCode().equals("QYGLY")){//企业管理员
						isVirPermId =  true;
					}else if(config.getCode().equals("BMGLY")){//部门管理员
						isDepPermId = true;
					}
				}
			}
			
			log.info("ApiGoodsReleaseController list isVirPermId======{}",isVirPermId);
			log.info("ApiGoodsReleaseController list isDepPermId======{}",isDepPermId);

			if (StringUtils.isNotEmpty(virAccount)) {
				//virAccountId = Long.parseLong(virAccount);
				//params.put("virAccountId", virAccount);
			}
			/*if (StringUtils.isEmpty(userId)) {  //virAccountId != null
				//VirAccountDto dto = virtualArchService.getVirAccountBy(virAccountId);
				//Integer virId = dto.getCompId();
				Integer             virId = Integer.parseInt(companyId);
				Map<String, Object> map   = propertyManager.getVirtualArchitectureMap(virId.toString());
				if (map != null) {
					String relation_id = null;
					if (map.get("relation_id") != null) {
						relation_id = map.get("relation_id").toString();
						if (StringUtils.isNotEmpty(relation_id)) {
							relation_id += "," + virId;
						} else {
							relation_id = virId.toString();
						}
						params.put("virIds", relation_id);
					} else {
						params.put("virIds", virId);
					}
				} else {
					params.put("virIds", virId);
				}
			}*/
			
			if(StringUtils.isNotEmpty(isAuditPage)  &&  "1".equals(isAuditPage)){ //审核界面
				
				if(releaseStatus.equals("10")){ //审核中才区分 角色 
					params.put("userPermId", userPermId);
				}
				
				
				if (StringUtils.isNotEmpty(userId)) {
					//params.put("userId", Integer.parseInt(userId));
					//params.put("virAccountId", null);
					
					if(isVirPermId){
						params.put("virtualArchId", companyId);//公司Id
					}else if(isDepPermId){
						//AppUser   user = appUserManager.get(Integer.valueOf(userId));					
						params.put("rank", "");
						if(null != user && null != user.getDepartmentId()){
							Department dept = deptManager.get(Long.parseLong(user.getDepartmentId().toString()));				
							if(StringUtils.isNotBlank(dept.getRank())){
								params.put("rank", dept.getRank());
							}
						}	
					}else{
						params.put("virtualArchId", 0);//如果其他角色不给看单
					}
									
				}
			}else{//个人界面
				if (StringUtils.isNotEmpty(userId)) {
					params.put("userId", Integer.parseInt(userId));
				}
			}
			

			params.put("releaseStatus", releaseStatus);
			params.put("applyType", applyType);
			
			/*params.put("billStartDate", request.getParameter("billStartDate"));//发起-开始时间
			params.put("billEndDate", request.getParameter("billEndDate"));
			params.put("releaseStartDate", request.getParameter("releaseStartDate"));//放行-开始时间
			params.put("releaseEndDate", request.getParameter("releaseEndDate"));
			params.put("releaseType", request.getParameter("releaseType"));
			params.put("releaseUser", request.getParameter("releaseUser"));*/
			//if(StringUtils.isNotEmpty(roleType) && "5".equals(roleType)){//马上审批

			//params.put("companyHeadUserIsNull", "companyHeadUserIsNull");//为空的表不没有处理过的
				
				/*List<Map<String, Object>> ListVirIds=goodsReleaseManager.getVirIdsByOpenId(openId,1);
				String virIds="'#'";
				if(null!=ListVirIds){
					for (Map<String,Object> obj:ListVirIds){
						if(obj.containsKey("virId")){
							virIds=virIds+",'"+obj.get("virId")+"'";
						}
					}					
				}	
				params.put("virIds", virIds);
			}else{
				params.put("creatorBillId", wxUser.getId());//马上审批 不需要查自己的，查公司的就行
			}*/

			Page             page = goodsReleaseService.findPageBy(params, pageSize, pageNo);
			WebPageResultDto dto  = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", page.getItems());
			dto.setCount(page.getTotalCount());
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败", LogExceptionStackUtil.LogExceptionStack(e));
		}
	}

	/**
	 * @return 查找电子放行条详情
	 * @throws UnsupportedEncodingException
	 * @throws NumberFormatException
	 */
	@RequestMapping(value = "/getDetail")
	@ResponseBody
	public AppResultDto getDetail(@RequestParam("id") String id) throws NumberFormatException, UnsupportedEncodingException {

		log.info("WxGoodsReleaseController getDetail======");

		//Long decode_id=Long.decode(GenPass.Decrypt(URLDecoder.decode(id, "UTF-8")));

		return AppResultUtil.createSuccesDto(goodsReleaseService.findById(id));
	}

	/**
	 * 同意放行
	 */
	@RequestMapping(value = "/agree")
	@ResponseBody
	public AppResultDto agree(@ModelAttribute("goodsReleaseBillInputDto") GoodsReleaseBillInputDto dto, MultipartHttpServletRequest request) {
		log.info("WxGoodsReleaseController agree======");

		dto.setBillFrom(CodeConstants.WECHAT);

		AppResultDto rdto = goodsReleaseService.agree(dto, request);
		try {
			GoodsReleaseOutDto gDto = (GoodsReleaseOutDto) ((AppCommonResultDto) rdto).getData();
			goodsReleaseService.push(gDto.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rdto;

	}

	/**
	 * 拒绝放行
	 */
	@RequestMapping(value = "/refuse")
	@ResponseBody
	public AppResultDto refuse(@ModelAttribute("goodsReleaseBillInputDto") GoodsReleaseBillInputDto dto, HttpServletRequest request) {

		log.info("WxGoodsReleaseController refuse======");
		dto.setBillFrom(CodeConstants.WECHAT);
		return goodsReleaseService.refuse(dto, request);
	}

	/**
	 * @return 获取审核流程
	 */
	@RequestMapping(value = "/getGoodsConfig")
	@ResponseBody
	public AppResultDto getGoodsConfig(@RequestParam("orgId") Long orgId, HttpServletRequest request) {

		log.info("ApiGoodsReleaseController getGoodsConfig======");

		String applyType = request.getParameter("applyType");

		Integer type = 0;
		if (StringUtils.isNotEmpty(applyType)) {
			type = 1;
		}

		boolean flag = goodsReleaseService.getGoodsConfig(orgId, type);

		Map<String, Object> map = new HashMap<>();
		if (flag) {
			map.put("config", 1);
			//return AppResultUtil.createSuccesDto(map);
		}else{
			map.put("config", 0);
		}

		List<Map<String, Object>> list =  goodsReleaseService.getGoodsConfigByTypes(orgId, "1");
		if(list.size()>=1){
			map.put("permId",list.get(0).get("roleId"));
		}else{
			map.put("permId","");
		}


		return AppResultUtil.createSuccesDto("成功", map);
	}

	/**
	 * 判断该企业有没有审核放行员
	 *
	 * @param virtualArchId
	 * @return
	 */
	@RequestMapping(value = "/check")
	@ResponseBody
	public AppResultDto check(@RequestParam("virtualArchId") Integer virtualArchId) {
		log.info("ApiGoodsReleaseController check======");
		return AppResultUtil.createSuccesDto(goodsReleaseService.check(virtualArchId));
	}
	
	
	/**
	 * 获取审核流程
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "/getPermByCheckRole")
	@ResponseBody
	public AppResultDto getPermByCheckRole(@RequestParam Long orgId,@RequestParam Integer pageType,HttpServletRequest request) {

		/*User user = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> organizationList = appUserService.getUserOrgs(user.getId());
		if (organizationList.size() >= 1) {
			orgId = Long.valueOf(organizationList.get(0).getId().toString());
		}*/
		if(null == pageType){//pageType 1-物品放行 2-空调加时 
			pageType = 1;//
		}
		return AppResultUtil.createSuccesDto(enterprisePermissionManager.getPermByCheckRole(orgId));
	}
	
	/**
	 * 根据 SysCode 和 OrgId 查询角色列表
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "/getRoleByOrgAndSysCode")
	@ResponseBody
	public AppResultDto getRoleByOrgAndSysCode(@RequestParam Integer orgId,@RequestParam Integer pageType,HttpServletRequest request) {

		try {
			/*User user = (User) request.getSession().getAttribute("UserCache");
			List<OrgDto> organizationList = appUserService.getUserOrgs(user.getId());
			if (organizationList.size() >= 1) {
				orgId = organizationList.get(0).getId();
			}*/
			if(null == pageType){//pageType 1-物品放行 2-空调加时 
				pageType = 1;//
			}
			return AppResultUtil.createSuccesDto(roleManager.getRoleByOrgAndSysCode(orgId,pageType));
		} catch (Exception e) {
			log.error("查询角色列表失败 getRoleByOrgAndSysCode ===>" + e.getMessage(), e);
		}

		return AppResultUtil.createErrorDto("100", "查询角色列表失败");
	}
	
	/**
	 * 获取审核流程
	 *
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "/getWxGoodsConfig")
	@ResponseBody
	public AppResultDto getWxGoodsConfig(@RequestParam Long orgId,HttpServletRequest request) {
		
		/*User user = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> organizationList = appUserService.getUserOrgs(user.getId());
		if (organizationList.size() >= 1) {
			orgId = Long.valueOf(organizationList.get(0).getId().toString());
		}*/
		return AppResultUtil.createSuccesDto(enterprisePermissionManager.getGoodsConfig(orgId));
	}
	
	

}
