package com.engine.htsc.capitalbusinessPayment.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.capitalbusinessPayment.service.CapitalBusinessPaymentService;
import com.engine.htsc.trip.util.AjaxResult;
import com.engine.workflow.service.RequestMonitorListService;
import com.engine.workflow.service.impl.RequestMonitorListServiceImpl;
import org.apache.commons.lang3.StringUtils;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * 资金业务付款
 * 出纳支付明细操作Action
 */
public class CapitalBusinessPaymentAction {

    private BaseBean bb = new BaseBean();

    public CapitalBusinessPaymentAction() {
    }

    private CapitalBusinessPaymentService getService() {
        return ServiceUtil.getService(CapitalBusinessPaymentService.class);
    }

    private RequestMonitorListService getArchiveService (User user) {
        return (RequestMonitorListServiceImpl) ServiceUtil.getService(RequestMonitorListServiceImpl.class, user);
    }

    /**
     * 出纳点击-自动付款
     * @param request
     * @return
     */
    @POST
    @Path("/doAutoPay")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> doAutoPay(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String result;
        try {
            Map<String, Object> request2Map = ParamUtil.request2Map(request);
            String ids = Util.null2String(request2Map.get("ids"));//勾选明细ids
            String userId = String.valueOf(HrmUserVarify.getUser(request,response).getUID());//获取当前处理人
            if (StringUtils.isNotBlank(ids)) {
                result = getService().doAutoPay(ids,userId);
            } else {
                result = "请选择需要自动付款的流程！";
            }
            return AjaxResult.ok(result, "修改状态为自动付款成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 出纳点击-手动付款
     * @param request
     * @return
     */
    @POST
    @Path("/doManualPay")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> doManualPay(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String result;
        try {
            Map<String, Object> request2Map = ParamUtil.request2Map(request);
            String ids = Util.null2String(request2Map.get("ids")); //勾选明细ids
            String userId = String.valueOf(HrmUserVarify.getUser(request,response).getUID());//获取当前处理人
            if (StringUtils.isNotBlank(ids)) {
                result = getService().doManualPay(ids,userId);
            } else {
                result = "请选择需要付款的流程！";
            }
            return AjaxResult.ok(result, "修改状态为付款成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 资金业务付款-流程终止
     * @param request
     * @return
     */
    @POST
    @Path("/doEndProcess")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> doEndProcess(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String result;
        try {
            Map<String, Object> request2Map = ParamUtil.request2Map(request);
            String requestid = Util.null2String(request2Map.get("requestid")); //获取requestid
            String userId = String.valueOf(HrmUserVarify.getUser(request,response).getUID());//获取当前处理人
            if (StringUtils.isNotBlank(requestid)) {
                result = getService().doEndProcess(requestid,userId);
            } else {
                result = "fail";
            }
            return AjaxResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //TODO 强制归档
    public void forceArchiving(HttpServletRequest request, HttpServletResponse response, User user) {
        bb.writeLog("资金业务付款 强制归档请求 request"+request.toString());
        RequestMonitorListService archivingService = getArchiveService(user);
        bb.writeLog("资金业务付款获取泛微归档服务 archivingService:"+archivingService);
        //TODO 根据支付明细Ids获取requestId
        String multiRequestIds = request.getParameter("multiRequestIds");
        archivingService.archivingRequestById(request,response,user);
    }




}
