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

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.fittingManager.entity.PlatformFitting;
import com.sifang.modules.platform.fittingManager.service.PlatFormFittingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlantformFittingApply;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 com.sifang.modules.sunshine.fittingManager.service.FittingApplyService;
import com.sifang.modules.platform.utils.PlantformUtils;


/**
 * @author lzp
 * @date 2019/10/11 14:28
 */
@Controller
@RequestMapping(value = "${adminPath}/platform/fitting")
public class FittingApplyController extends BaseController {

	@Autowired
	private FittingApplyService fittingApplyService;
	@Autowired
	private PlatFormFittingService platformFittingService;
	private Logger logger= LoggerFactory.getLogger(getClass());

	/**
	 * 备件申请-待审核
	 * @return
	 */
	@RequestMapping("toPendingReviewPage")
	public String toPendingReviewPage(Model model) {
		model.addAttribute("siteList", PlantformUtils.getAllSiteList());
		return "modules/yangguang/fittingManager/applyManager/beDelivered";
	}

	/**
	 * 备件申请-待审核数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAllPendingReviewData")
	public Object getAllPendingReviewData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        fittingApplyService.getAllApplyList(page, map,"1");
        return page.renderJson();
	}


	/**
	 * 备件详情页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("toFittingDetailNo")
	public String toFittingDetailNo(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		Record re = fittingApplyService.getApplyDetail(id);
		model.addAttribute("applyRecord", re);
		return "modules/yangguang/fittingManager/applyManager/fittingDetailPageNo";
	}

	/**
	 * 平台审核
	 * @param request
	 *
	*/
	@RequestMapping(value = "applyToExamineForm")
	public String  applyToExamineForm(HttpServletRequest request,Model model){
		String applyId = request.getParameter("applyId");
		CrmPlantformFittingApply apply = fittingApplyService.getById(applyId);
		model.addAttribute("apply",apply);
		//获取备件基本信息
		PlatformFitting fitting = platformFittingService.getById(apply.getFittingId());
		model.addAttribute("fitting",fitting);
		return "modules/yangguang/fittingManager/applyManager/applyToExamine";
	}

	/**
	 * 审核通过
	*/
	@ResponseBody
	@RequestMapping(value = "adoptApply")
	public Result adoptApply(HttpServletRequest request){
		String id = request.getParameter("id");
		String fittingAuditNum = request.getParameter("fittingSuditNum");
		String oldFittingFlag = request.getParameter("oldFittingFlag");
		String factoryMarks = request.getParameter("factoryMarks");
		if(StringUtil.isBlank(id)){
			return Result.fail("202","id数据有误");
		}
		try {
			return fittingApplyService.adoptApply(id,fittingAuditNum,oldFittingFlag,factoryMarks);
		} catch (IllegalArgumentException i) {
			return Result.fail("205",i.getMessage());
		} catch (Exception e ){
			e.printStackTrace();
			return Result.fail("205","审核失败");
		}
	}

	/**
	 * 审核拒绝
	 * @param request
	*/
	@ResponseBody
	@RequestMapping(value = "refuseFitting")
	public Result refuseFitting(HttpServletRequest request){
		String id = request.getParameter("id");
		String oldFittingFlag = request.getParameter("oldFittingFlag");
		String factoryMarks = request.getParameter("factoryMarks");
		String refuseReason = request.getParameter("refuseReason");
		if(StringUtil.isBlank(id)){
			return Result.fail("202","id数据有误");
		}
		try {
			return fittingApplyService.refuseFitting(id,refuseReason,oldFittingFlag,factoryMarks);
		}catch (Exception e ){
			e.printStackTrace();
			return Result.fail("205","审核失败");
		}
	}

	/**
	 * 平台出库页面
	 * @param request
	 *
	 */
	@RequestMapping(value = "outStockFittingApply")
	public String  outStockFittingApply(HttpServletRequest request,Model model){
		String applyId = request.getParameter("applyId");
		CrmPlantformFittingApply apply = fittingApplyService.getById(applyId);
		model.addAttribute("apply",apply);
		//获取备件基本信息
		PlatformFitting fitting = platformFittingService.getById(apply.getFittingId());
		model.addAttribute("fitting",fitting);
		return "modules/yangguang/fittingManager/applyManager/outStockFittingApply";
	}

	/**
	 * 平台审核备件申请- 出库
	*/
	@ResponseBody
	@RequestMapping(value = "fittingOutStock")
	public Result fittingOutStock(HttpServletRequest request) {
		String id = request.getParameter("id");
		String outMark = request.getParameter("outMark");
			try {
			return 	fittingApplyService.fittingOutStock(id,outMark);
			}catch (Exception e){
				e.printStackTrace();
              logger.error(">>>>>>>>>>>>>>>>>>>fittingOutStock :exectption:{}",e);
				return Result.fail("202","出库失败");
			}
	}

	/**
	 * 平台驳回
	*/
	@ResponseBody
	@RequestMapping(value = "RejectFitting")
	public Result RejectFitting(HttpServletRequest request){
		String id = request.getParameter("id");
		try {
			return fittingApplyService.RejectFitting(id);
		}catch (Exception e){
			e.printStackTrace();
			return Result.fail("202","驳回失败");
		}
	}

	/**
	 * 备件申请-待出库
	 * @return
	 */
	@RequestMapping("fittingOnTheWay")
	public String fittingOnTheWay(Model model) {
		model.addAttribute("siteList", PlantformUtils.getAllSiteList());
		return "modules/yangguang/fittingManager/applyManager/fittingOnTheWay";
	}

	/**
	 * 备件申请-待出库
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getAllOnTheWayFitData")
	public Object getAllOnTheWayFitData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        fittingApplyService.getAllApplyList(page, map,"2");
        return page.renderJson();
	}

	/**
	 * 备件申请-全部申请
	 * @return
	 */
	@RequestMapping("finishedFitApplyPage")
	public String finishedFitApplyPage(Model model) {
		model.addAttribute("siteList", PlantformUtils.getAllSiteList());
		return "modules/yangguang/fittingManager/applyManager/finishedFittingApply";
	}

	/**
	 * 备件申请-全部申请数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("finishedFitApplyData")
	public Object finishedFitApplyData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        fittingApplyService.getAllApplyList(page, map,"");
        return page.renderJson();
	}


	/**
	 * 导出数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("exportsData")
	public Object exportsData(HttpServletRequest request){
		String type = request.getParameter("type");
		LayuiPage page = new LayuiPage(request);
		page.setPage(1);
		page.setLimit(10000);
		if ("1".equals(type)) {
			return fittingApplyService.getAllApplyListForExport(page, null, "1");
		} else if ("2".equals(type)) {
			return fittingApplyService.getAllApplyListForExportTw(page, null, "2");
		} else if ("3".equals(type)) {
			return fittingApplyService.getAllApplyListForExportTh(page, null, "");
		}else{
			return "201";
		}
	}

}
