package com.palmnest.webapp.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.output.org.AboutOrgOutDto;
import com.palmnest.service.org.AboutOrgService;
import com.palmnest.service.redis.BaseRedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/wechat/aboutOrg")
public class WechatAboutOrgController {

	@Autowired
	private AboutOrgService  aboutOrgService;
	@Autowired
	private BaseRedisService baseRedisService;

	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public AppResultDto list(Integer orgId) {
		//服务项放入缓存
		String serviceJson = baseRedisService.get(CodeConstants.ABOUT_ORG_LIST_KEY + orgId);
		if (StringUtils.isNotEmpty(serviceJson)) {
			List<AboutOrgOutDto> list2 = JSONObject.parseArray(serviceJson, AboutOrgOutDto.class);
			if (list2 != null) {
				return AppResultUtil.createSuccesDto(list2);
			}
		}
		List<AboutOrgOutDto> dtos = aboutOrgService.findByOrg(orgId);
		baseRedisService.set(CodeConstants.ABOUT_ORG_LIST_KEY + orgId, JSONObject.toJSONString(dtos));
		return AppResultUtil.createSuccesDto(dtos);
	}

	@ResponseBody
	@RequestMapping(value = "/getDetail", method = {RequestMethod.GET, RequestMethod.POST})
	public AppResultDto getDetail(Long id) {
		AboutOrgOutDto dto = aboutOrgService.findDtoBy(id);
		return AppResultUtil.createSuccesDto(dto);
	}
}
