package com.palmnest.webapp.controller.washcar;

import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.ExtraServiceDto;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.output.ExtraServiceOutputDto;
import com.palmnest.model.User;
import com.palmnest.model.washcar.ExtraService;
import com.palmnest.model.washcar.ProjectSet;
import com.palmnest.service.AppUserService;
import com.palmnest.service.washcar.service.ExtraServiceService;
import com.palmnest.service.washcar.service.ProjectSetService;
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;


/**
 * 设置中心controller
 *
 * @author longmeizhen
 */
@Slf4j
@Controller
@RequestMapping("/admin/washcar/*")
public class ExtraServiceController {


	@Autowired
	private ProjectSetService   projectSetService;
	@Autowired
	private ExtraServiceService extraServiceService;
	@Autowired
	private AppUserService      appUserService;

	/**
	 * 跳转到设置中心界面
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "jumpSetCenter")
	public ModelAndView jumpSetCenter(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/vehicleClean/setFocus", model.asMap());
	}

	/**
	 * 获取额外服务列表
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getExtraServiceList")
	@ResponseBody
	public AppResultDto getExtraServiceList(String projectId, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ExtraServiceOutputDto dto = new ExtraServiceOutputDto();
		try {
			dto = extraServiceService.getExtraList(projectId);
			if (dto != null) {
				return AppResultUtil.createSuccesDto(dto);
			} else {
				return AppResultUtil.createErrorDto("100", "该项目下没有任何服务信息");
			}

		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "获取失败:" + e.getMessage());
		}
	}

	//新增修改额外服务的入口
	@RequestMapping(value = "getExtraService")
	@ResponseBody
	public AppResultDto getExtraService(Long id, String proSetId, String orgId, HttpServletRequest request, HttpServletResponse response) {

		User user = (User) request.getSession().getAttribute("UserCache");

		String userId = "";
		if (user != null) {
			userId = String.valueOf(user.getId());
		}

		ExtraServiceDto dto = new ExtraServiceDto();

		try {
			dto = extraServiceService.findByIdAndProId(orgId, id, proSetId, userId);
			return AppResultUtil.createSuccesDto(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "获取失败:" + e.getMessage());
		}
	}

	/**
	 * 保存修改额外服务
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "saveOrUpdateExtraService")
	@ResponseBody
	public AppResultDto saveOrUpdateExtraService(ExtraServiceDto extraServiceDto, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = String.valueOf(user.getId());
		}
		ExtraService extra = new ExtraService();
		try {
			extra = extraServiceService.saveExtraService(extraServiceDto, userId);
			if (extra != null && extra.getId() != null) {
				return AppResultUtil.createCommonDto(AppConstants.SUCCESS_CODE, "成功", null);
			} else {
				return AppResultUtil.createErrorDto("100", "操作失败");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "操作失败:" + e.getMessage());
		}
	}

	/**
	 * 保存修改洗车名额
	 *
	 * @param request
	 * @param response
	 * @param quota    洗车名额
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "saveOrUpdateQuota")
	@ResponseBody
	public AppResultDto saveOrUpdateQuota(Long id, Integer quota, String orgId, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = String.valueOf(user.getId());
		}
		ProjectSet proSet = new ProjectSet();
		try {
			proSet = projectSetService.saveQuota(orgId, id, quota, userId);
			if (proSet != null && proSet.getId() != null) {
				return AppResultUtil.createCommonDto(AppConstants.SUCCESS_CODE, "成功", proSet);
			} else {
				return AppResultUtil.createErrorDto("100", "修改名额失败");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "修改名额失败:" + e.getMessage());
		}

	}

	/**
	 * 删除服务
	 *
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteExtraService")
	@ResponseBody
	public AppResultDto deleteExtraService(Long id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = String.valueOf(user.getId());
		}
		int count = 0;
		try {
			count = extraServiceService.deleteExtra(id, userId);
			if (count > 0) {
				return AppResultUtil.createCommonDto(AppConstants.SUCCESS_CODE, "删除成功", null);
			} else {
				return AppResultUtil.createErrorDto("100", "删除失败");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "删除服务失败:" + e.getMessage());
		}
	}

}
