package com.palmnest.webapp.controller.backstage;

import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.SMSSearchDto;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.estateserve.SmsAuthCodeManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Controller
@RequestMapping(value = "/admin/sms/info/*")
public class SMSInformationController extends BaseFormController {


	@Autowired
	private AppUserService     appUserService;
	@Autowired
	private SmsAuthCodeManager smsAuthCodeManager;

	/**
	 * 跳转到短信管理界面
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "jumpSMSList")
	public ModelAndView jumpSMSList(HttpServletRequest request,
									HttpServletResponse response) {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/orderDinner/smsInfoManage", model.asMap());
	}

	/**
	 * 短信管理界面列表查询
	 *
	 * @param dto
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getSmsInfoList")
	@ResponseBody
	public WebPageResultDto getSmsInfoList(SMSSearchDto dto, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("UserCache");
		Page page = smsAuthCodeManager.findList(dto);
		if (page != null) {
			WebPageResultDto webDto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", page.getItems());
			webDto.setCount(page.getTotalCount());
			return webDto;
		}
		return null;
	}
}
