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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.fittingManager.service.FacFittingRefundService;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlantformFittingRefundMsg;
import com.sifang.modules.sunshine.fittingManager.service.FittingRefundService;
import com.sifang.modules.platform.utils.PlantformUtils;
import com.sifang.modules.site.fittingManager.service.SiteFittingRefundService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.CrmUtils;
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}/platform/fittingRefund")
public class FittingRefundController extends BaseController {

	@Autowired
	private FittingRefundService refundService;

	@Autowired
	private SiteFittingRefundService siteRefundService;
	@Autowired
	private FacFittingRefundService facFittingRefundService;
	//跳转到平台备件返厂页面
	@RequestMapping("toFittingRefundPage")
	public String toFittingRefundPage(HttpServletRequest request,Model model){
		model.addAttribute("siteList", PlantformUtils.getAllSiteList());
		return "modules/yangguang/fittingManager/refundManager/fittingRefundPage";
	}

	/**
	 * 获取平台备件返厂页面的数据
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAllSiteRefundData")
	public String getAllSiteRefundData(HttpServletRequest request){
		LayuiPage page = new LayuiPage(request);
		User user = UserUtils.getUser();
		Map<String, String> mmap = getParams(request);
	/*	if(User.DEL_FLAG_AUDIT.equals(user.getUserType())){
			String siteId = CrmUtils.getSiteId(user);
		}*/
		refundService.getAllRefundFitList(page, mmap);
		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);
		model.addAttribute("refundRecord", re);
     String fittingImg = re.getStr("fitting_img");
        String[] fittingImgs = null;
        if(fittingImg !=null){
			fittingImgs = fittingImg.split(",");
		}
		model.addAttribute("fittingImgs",fittingImgs);

		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 facFittingRefundService.cofToTheFac(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return "202";
		}
	}
	//平台出入库明细页面
	@RequestMapping(value="toPlatformInAndOutWarehousingList")
	public String toInAndOutWarehousingList(HttpServletRequest request,Model model){
		return "modules/yangguang/fittingManager/refundManager/platformInAndOutWarehousingList";
	}

	//平台出入库明细页面数据
	@ResponseBody
	@RequestMapping(value="getPlatformInAndOutWarehousingData")
	public  String getPlatformInAndOutWarehousingData(HttpServletRequest request){
		LayuiPage page = new LayuiPage(request);
		Map<String, String> mmap = getParams(request);
		refundService.getPlatformInAndOutWarehousingData(page, mmap);
		return page.renderJson();
	}

}
