package com.sifang.modules.platform.fittingManager.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.fittingManager.service.FacFittingRefundService;
import com.sifang.modules.platform.utils.PlantformUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lzp
 * @date 2019/10/11 14:28
 * 平台备件返厂controller
 */
@Controller
@RequestMapping("${adminPath}/factory/fittingRefund")
public class FacFittingRefundController extends BaseController {

	@Autowired
	private FacFittingRefundService refundService;

	//跳转到平台备件返厂页面
	@RequestMapping("toFitRefundPage")
	public String toFittingRefundPage(HttpServletRequest request,Model model){
		model.addAttribute("siteList", PlantformUtils.getAllSiteList());
		return "modules/factory/fittingManager/fittingRefund/fittingRefundPage";
	}

	/**
	 * 获取所有服务商申请的备件
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAllSiteRefundData")
	public String getAllSiteRefundData(HttpServletRequest request){
		LayuiPage page = new LayuiPage(request);
		Map<String, String> map = getParams(request);
		return page.renderJson();
	}

	/**
	 * 备件详情页面
	 * @param request
	 * @return
	 */
	@RequestMapping("goFitRefundDetail")
	public String goFitRefundDetail(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		Record re = refundService.getFitRefundDetail(id);
		String imgs = re.getStr("fitting_img");
		model.addAttribute("refundRecord", re);
		model.addAttribute("fitImgs",imgs.split(","));
		return "modules/yangguang/fittingManager/refundManager/fitRefundDetailPageNo";
	}


	/**
	 *	确认到厂操作
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("confToTheFac")
	public String confToTheFac(HttpServletRequest request){
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			return "201";
		}
		try {
			return refundService.cofToTheFac(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return "202";
		}
	}


}
