package com.palmnest.webapp.controller.wechat;

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.core.util.LogExceptionStackUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.RenovationManageAuditParam;
import com.palmnest.application.dto.input.RenovationManageInputDto;
import com.palmnest.model.User;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.AppUserService;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.release.RenovationManageService;
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.List;
import java.util.Map;


//微信端
@Slf4j
@Controller
@RequestMapping(value = "/wechat/renovation")
public class WxRenovationManageController {


	@Autowired
	private RenovationManageService renovationManageService;
	@Autowired
	private AppUserService          appUserService;
	@Autowired
	private ScWxuserManager         scWxuserManager;


	/**
	 * @param request
	 * @return 物品放行列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public WebPageResultDto 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");

			WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);

			if (null != wxUser) {
				params.put("creatorBillId", wxUser.getId());
			} else {
				params.put("creatorBillId", "0");//没有查询为空
			}

			Page page = renovationManageService.findPageBy(params, pageSize, pageNo);

			WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", page.getItems());

			dto.setCount(page.getTotalCount());
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败", LogExceptionStackUtil.LogExceptionStack(e));
		}
	}

	/**
	 * web端获取当前登录用户所管辖的项目id串
	 *
	 * @return
	 */
	public String getUserManageOrgIds(User user) {
		String       orgStr = "";
		List<OrgDto> dtos   = appUserService.getUserOrgs(user.getId());
		//拼接项目id
		if (dtos != null && dtos.size() > 0) {
			for (OrgDto org : dtos) {
				orgStr += "," + org.getId();
			}
			orgStr = orgStr.substring(1);
		}
		return orgStr;
	}


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

		log.info("WxRenovationManageController apply======");
		AppResultDto rdto = renovationManageService.apply(dto, request);

		return rdto;
	}


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

		log.info("WxRenovationManageController audit======");
		AppResultDto rdto = renovationManageService.audit(dto, request);

		return rdto;
	}


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

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

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

}
