package com.palmnest.webapp.controller.api;

import cn.superdesk.libs.common.json.JsonUtils;

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.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.input.RenovationManageAuditParam;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.estateserve.release.RenovationManageService;
import com.palmnest.service.redis.BaseRedisService;
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.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.Map;


/**
 * app 装修
 */
@Slf4j
@Controller
@RequestMapping(value = "/api/renovation")
public class ApiRenovationManageController extends BaseFormController {


	@Autowired
	private RenovationManageService renovationManageService;
	@Autowired
	private AppUserService          appUserService;
	@Autowired
	private BaseRedisService        baseRedisService;


	/**
	 * @param request
	 * @return 物品放行列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public AppResultDto list(HttpServletRequest request) {
		log.info("RenovationManageController list======");
		try {
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			int pageNo   = Integer.parseInt(request.getParameter("pageNo"));

			Map<String, Object> params = new HashMap<String, Object>();

			//String openId = request.getParameter("openId");
			String status      = request.getParameter("status");
			String companyName = request.getParameter("companyName");

			params.put("companyName", companyName);
			params.put("status", status);

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

			String orgIds = getAppUserManageOrgIds(request);//例如:1,25,2
			if (StringUtil.isNotEmpty(orgIds)) {
                String[] orgArr = orgIds.split(",");
                params.put("orgId", orgArr[0]);
            }
			params.put("orgIds", orgIds);
			log.info("RenovationManageController list======params:"+JsonUtils.toJson(params));
			Page page = renovationManageService.findPageBy(params, pageSize, pageNo);
			return AppResultUtil.createSuccesDto(page);
		} catch (Exception e) {
			e.printStackTrace();
			return AppResultUtil.createErrorDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败" + LogExceptionStackUtil.LogExceptionStack(e));
		}
	}


	/**
	 * 提交物品放行申请
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/audit")
	@ResponseBody
	public AppResultDto audit(@ModelAttribute("RenovationManageAuditParam") RenovationManageAuditParam dto, HttpServletRequest request) {

		log.info("ApiRenovationManageController audit======");

		dto.setBillFrom(CodeConstants.ANDROID);
		AppResultDto rdto = renovationManageService.audit(dto, request);

		return rdto;
	}


	/**
	 * @return 查找电子放行条详情
	 */
	@RequestMapping(value = "/getDetail")
	@ResponseBody
	public AppResultDto getDetail(@RequestParam("id") Long id) {

		log.info("ApiRenovationManageController getDetail======");

		return AppResultUtil.createSuccesDto(renovationManageService.findById(id));
	}

}
