package com.palmnest.webapp.controller.washcar;


import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.CustomerInputDto;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.washcar.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
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 CustomerController {


	@Autowired
	private CustomerService customerService;
	@Autowired
	private AppUserService  appUserService;

	/**
	 * 跳转到用户信息界面
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "jumpCustomer")
	public ModelAndView jumpCustomer(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/personInfo", model.asMap());
	}

	/**
	 * 导入用户信息
	 *
	 * @param excelFile
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "importUserInfo")
	@ResponseBody
	public AppResultDto importUserInfo(@RequestParam("excelFile") MultipartFile excelFile, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User   user   = (User) request.getSession().getAttribute("UserCache");
		String userId = "";
		if (user != null) {
			userId = String.valueOf(user.getId());
		}
		AppResultDto dto = customerService.importUserInfo(excelFile.getInputStream(), userId);
		return dto;
	}

	/**
	 * 修改用户入口
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getCustomer")
	@ResponseBody
	public AppResultDto getCustomer(Long id) {
		CustomerInputDto dto = customerService.findById(id);
		return AppResultUtil.createSuccesDto(dto);
	}


	/**
	 * 保存用户信息
	 *
	 * @param customerInputDto
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "saveCustomer")
	@ResponseBody
	public AppResultDto saveCustomer(CustomerInputDto customerInputDto, HttpServletRequest request, HttpServletResponse response) throws Exception {

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

		String userId = "";

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

		if (StringUtils.isEmpty(customerInputDto.getOrgId())) {
			return AppResultUtil.createErrorDto("100", "项目不能为空");
		}

		String result = "";
		try {

			result = customerService.saveCustomer(customerInputDto, userId);

			if ("save".equals(result)) {
				return AppResultUtil.createCommonDto(AppConstants.SUCCESS_CODE, "保存用户信息成功", null);
			} else if ("update".equals(result)) {
				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 custId
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteCustomer")
	@ResponseBody
	public AppResultDto deleteCustomer(String custId, HttpServletRequest request, HttpServletResponse response) throws Exception {

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

		String userId = "";

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

		String result = "";

		try {
			result = customerService.deleteCustomer(Long.parseLong(custId), userId);
			if ("0".equals(result)) {
				return AppResultUtil.createCommonDto("100", "删除失败", null);
			} else {
				return AppResultUtil.createCommonDto(AppConstants.SUCCESS_CODE, "删除成功", null);
			}

		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "操作用户信息失败:" + e.getMessage());
		}
	}

	/**
	 * 获取用户列表
	 *
	 * @param userName
	 * @param mobile
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "getCustmoerList")
	@ResponseBody
	public WebPageResultDto getCustmoerList(String userName, String mobile, String carNumber, String searchStr, int page, int limit, HttpServletRequest request, HttpServletResponse response) {

		Page custList = new Page();

		String orgIds = request.getParameter("orgId");
		User   user   = (User) request.getSession().getAttribute("UserCache");

		List<OrgDto> dtos = appUserService.getUserOrgs(user.getId());

		try {
			if (StringUtils.isEmpty(orgIds) || "0".equals(orgIds)) {
				if (dtos != null && dtos.size() > 0) {
					orgIds = "";
					for (int i = 0; i < dtos.size(); i++) {
						OrgDto dto = dtos.get(i);
						orgIds = orgIds + dto.getId() + ";";
					}
				}
			}
			custList = customerService.getCustList(orgIds, mobile, userName, carNumber, searchStr, page, limit);
			WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", custList.getItems());
			dto.setCount(custList.getTotalCount());
			return dto;
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new WebPageResultDto("100", "查询列表失败:" + e.getMessage(), null);
		}
	}
}
