package com.palmnest.webapp.controller.backstage;

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.input.AppUseVersionDto;
import com.palmnest.application.dto.output.AppUseVersionOutPutDto;
import com.palmnest.model.Element;
import com.palmnest.model.User;
import com.palmnest.service.appVersion.AppUseVersionService;
import com.palmnest.service.washcar.service.ElementGroupService;
import lombok.extern.slf4j.Slf4j;
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.ResponseBody;

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

/**
 * PC端app版本管理
 *
 * @author longmeizhen
 */
@Slf4j
@Controller
@RequestMapping(value = "/admin/appVersion/*")
public class AppUseVersionController {


	@Autowired
	private AppUseVersionService appUseVersionService;

	@Autowired
	private ElementGroupService elementGroupService;

	//保存版本记录

	/**
	 * 保存app版本管理记录
	 *
	 * @param versionDto
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "saveAppVersion")
	@ResponseBody
	public AppResultDto saveAppVersion(AppUseVersionDto versionDto, HttpServletRequest request, HttpServletResponse response) {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = user.getId().toString();
		} else {
			return AppResultUtil.createErrorDto("100", "登录用户不能为空");
		}
		try {
			String res = appUseVersionService.saveAppVersion(versionDto, userId);
			if ("SUCCESS".equals(res)) {
				return AppResultUtil.createSuccesDto();
			} else {
				return AppResultUtil.createErrorDto("100", "操作记录失败:" + res);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "保存记录失败：" + e.getMessage());
		}

	}
	//查询版本记录列表
	@RequestMapping(value = "getAppVersion")
	@ResponseBody
	public AppResultDto getAppVersion(HttpServletRequest request,HttpServletResponse response){
		try {
			String useCode = request.getParameter("useCode");
			String type = request.getParameter("type");
			List<AppUseVersionOutPutDto> appVersions = appUseVersionService.getByRecode(useCode, type);
			return AppResultUtil.createSuccesDto(appVersions);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "获取记录失败：" + e.getMessage());
		}
	}
	//获取版本列表详情

	//上传文件(上传成功后读取文件中的内容，返回给前端)

	//获取数据字典中的值

	/**
	 * 从数据字典中获取应用编码以及所属客户
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getDictionary")
	@ResponseBody
	public AppResultDto getDictionary(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map      = new HashMap<String, Object>();
		List<Element>       customer = elementGroupService.getElementsByGroupName("customer");
		map.put("customer", elementGroupService.convert(customer));//所属客户
		List<Element> applyCode = elementGroupService.getElementsByGroupName("applyCode");
		map.put("applyCode", elementGroupService.convert(applyCode));//应用编码
		return AppResultUtil.createSuccesDto(map);
	}
}
