package com.palmnest.webapp.controller.web;

import com.palmnest.application.AppConstants;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.output.org.AboutOrgOutDto;
import com.palmnest.model.User;
import com.palmnest.model.property.AboutOrg;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.org.AboutOrgService;
import com.palmnest.service.redis.BaseRedisService;
import com.palmnest.webapp.controller.BaseFormController;
import org.apache.commons.lang3.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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/aboutOrg*")
public class AboutOrgController extends BaseFormController {

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

	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model model = new ExtendedModelMap();
		User  user  = (User) request.getSession().getAttribute("UserCache");
		model.addAttribute("isAdmin", user.isAdmin());
		List<TbOrganization> organizationList = getUserRelationOrgList(request);
		model.addAttribute("organizationList", organizationList);
		if(null != user){
			model.addAttribute("userType", user.getUserType());
		}
		return new ModelAndView("admin/aboutUs/aboutUsList", model.asMap());
	}

	@RequestMapping(value = "/addOrUpdate")
	public ModelAndView addOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model                model            = new ExtendedModelMap();
		User                 user             = (User) request.getSession().getAttribute("UserCache");
		List<TbOrganization> organizationList = getUserRelationOrgList(request);
		model.addAttribute("organizationList", organizationList);
		model.addAttribute("isAdmin", user.isAdmin());
		if(null != user){
			model.addAttribute("userType", user.getUserType());
		}
		return new ModelAndView("admin/aboutUs/addUpdateAbout", model.asMap());
	}


	@RequestMapping(value = "/getList")
	@ResponseBody
	public WebPageResultDto getList(HttpServletRequest request) {
		String pageSize  = request.getParameter("pageSize");
		String pageNo    = request.getParameter("pageNo");
		String orgId     = request.getParameter("orgId");
		String startTime = request.getParameter("startTime");
		String endTime   = request.getParameter("endTime");
		String title     = request.getParameter("title");
		String type      = request.getParameter("type");

		User user = (User) request.getSession().getAttribute("UserCache");// 获取当前用户
		if (StringUtils.isBlank(pageNo)) {
			pageNo = "1";
		}
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "10";
		}

		if (StringUtils.isBlank(orgId)) {
			orgId = getUserManageOrgIds(user);
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("pageSize", pageSize);
		map.put("pageNo", pageNo);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("orgId", orgId);
		map.put("title", title);
		map.put("type", type);

		Page list = aboutOrgService.getList(map);

		WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", list.getItems());
		dto.setCount(list.getTotalCount());
		return dto;
	}

	@RequestMapping(value = "/addOrUpdateAboutOrg")
	@ResponseBody
	public AppResultDto addOrUpdateAboutOrg(HttpServletRequest request) {
		String title    = request.getParameter("title");
		String content  = request.getParameter("content");
		String orgIdStr = request.getParameter("orgId");

		String hotLineName = request.getParameter("hotLineName");
		String hotLine     = request.getParameter("hotLine");
		String type        = request.getParameter("type");
		String fileIds     = request.getParameter("fileIds");


		String idStr = request.getParameter("id");

		if (StringUtils.isEmpty(type)) {
			return AppResultUtil.createErrorDto("100", "type参数为空");
		}
		if (StringUtils.isEmpty(orgIdStr)) {
			return AppResultUtil.createErrorDto("100", "orgIdStr参数为空");
		}

		if (type.equals("1")) {
			if (StringUtils.isAnyBlank(hotLineName, hotLine)) {
				return AppResultUtil.createErrorDto("100", "参数为空");
			}
		} else if (type.equals("2")) {
			if (StringUtils.isAnyBlank(title, content)) {
				return AppResultUtil.createErrorDto("100", "参数为空");
			}
		}


		User    user  = (User) request.getSession().getAttribute("UserCache");// 获取当前用户
		Integer orgId = null;
		if (StringUtils.isNotEmpty(orgIdStr)) {
			orgId = Integer.parseInt(orgIdStr);
		}
		if (StringUtils.isNotEmpty(idStr)) {
			//更新
			Long     id    = Long.parseLong(idStr);
			AboutOrg about = aboutOrgService.findById(id);
			if (about != null) {
				about.setContent(content);
				about.setTitle(title);

				about.setHotLineName(hotLineName);
				about.setHotLine(hotLine);
				about.setType(Integer.decode(type));
				about.setFileIds(fileIds);

				about.setModifier(user.getFullName());
				about.setModifyTime(new Date());
				aboutOrgService.update(about);
			}

		} else {
			//新增
			AboutOrg about = new AboutOrg();
			about.setOrgId(orgId);
			about.setCreator(user.getFullName());
			about.setCreateTime(new Date());
			about.setContent(content);
			about.setTitle(title);

			about.setHotLineName(hotLineName);
			about.setHotLine(hotLine);
			about.setType(Integer.decode(type));
			about.setFileIds(fileIds);

			about.setModifier(user.getFullName());
			about.setModifyTime(new Date());
			about.setIsEnabled(1);
			aboutOrgService.save(about);
		}
		baseRedisService.dele(CodeConstants.ABOUT_ORG_LIST_KEY + orgId);
		return AppResultUtil.createSuccesDto();
	}

	@RequestMapping("deleteAboutOrg")
	@ResponseBody
	public AppResultDto deleteAboutOrg(Long id) {
		if (id == null) {
			return AppResultUtil.createErrorDto("100", "ID为空");
		}
		AboutOrg ab = aboutOrgService.findById(id);
		if (ab != null) {
			aboutOrgService.delete(ab);
			baseRedisService.dele(CodeConstants.ABOUT_ORG_LIST_KEY + ab.getOrgId());
		}
		return AppResultUtil.createSuccesDto();
	}

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