package com.huigou.topsun.sap.costReimbursement.controller;


import com.huigou.context.Operator;
import com.huigou.context.OrgUnit;
import com.huigou.topsun.sap.costReimbursement.application.SapCostReimbursementApplication;
import com.huigou.topsun.sap.costReimbursement.application.SapCostReimbursementDetailApplication;
import com.huigou.topsun.sap.costReimbursement.domain.SapCostReimbursement;
import com.huigou.topsun.sap.costReimbursement.domain.query.SapCostReimbursementQueryRequest;
import com.huigou.topsun.sap.costReimbursement.domain.vo.PaymentReceiptVo;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.annotation.SkipAuth;
import com.huigou.uasp.bmp.common.BizBillStatus;
import com.huigou.uasp.client.CommonController;
import com.huigou.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@ControllerMapping("/sapCostReimbursement")
public class SapCostReimbursementController extends CommonController {

    private final static Logger LOG = LoggerFactory.getLogger(SapCostReimbursementController.class);

    protected String getPagePath() {
        return "/biz/topsun/sap/costReimbursement/";
    }

    @Autowired
    SapCostReimbursementApplication costReimbursementApplication;
    @Autowired
    SapCostReimbursementDetailApplication costReimbursementDetailApplication;


    public String forwardCostReimbursementList(){
        SDO sdo = this.getSDO();
        return forward("costReimbursementList");
    }
    public String forwardPayApplyNoPOList(){
        return forward("payApplyNoPOList");
    }

    public String forwardAdvancePaymentList(){
        return forward("advancePaymentList");
    }

    public String forwardEmployeeLoanList(){
        return forward("employeeLoanList");
    }

    public String forwardPayApplyList(){
        return forward("payApplyList");
    }

    public String forwardRefundApplyList(){
        return forward("refundApplyList");
    }

    public String forwardCostReimbursement(){
        //定义流程
        this.putAttribute("processDefinitionKey", SapCostReimbursementApplication.PROCESS_DEFINITION_KEY);
        this.putAttribute("procUnitId", "Apply");//定义流程环节的id
        SDO sdo = this.getSDO();
        String type=  sdo.getProperty("type").toString();
        SapCostReimbursement sapCostReimbursement = new SapCostReimbursement();
        sapCostReimbursement.setStatusId(BizBillStatus.APPLYING.getId());
        Operator operator = getOperator();//获取当前操作员的详情信息
        sapCostReimbursement.setDefaultValues(new OrgUnit(operator.getFullId(), operator.getFullName()));
        sapCostReimbursement.setZepNo(sapCostReimbursement.getBillCode());
        sapCostReimbursement.setZepDepart(operator.getDeptCode());
        sapCostReimbursement.setZepDepartname(operator.getDeptName());
        sapCostReimbursement.setZepAppli(operator.getLoginName());
        sapCostReimbursement.setZepAppliname(operator.getName());
        sapCostReimbursement.setZepFcrdate(new Date());
        switch (type) {
            case "cost":
                sapCostReimbursement.setZepFtype("A");
                sapCostReimbursement.setZepFtypename("费用报销流程");
                return forward("costReimbursement",sapCostReimbursement);
            case "payNoPo":
                sapCostReimbursement.setZepFtype("H");
                sapCostReimbursement.setZepFtypename("付款申请(无PO)流程");
                return forward("payApplyNoPO",sapCostReimbursement);
            case "borrow":
                sapCostReimbursement.setZepFtype("B");
                sapCostReimbursement.setZepFtypename("员工借款流程");
                return forward("employeeLoan",sapCostReimbursement);
            case "advance":
                sapCostReimbursement.setZepFtype("D");
                sapCostReimbursement.setZepFtypename("预付款申请流程");
                return forward("advancePayment",sapCostReimbursement);
            case "pay":
                sapCostReimbursement.setZepFtype("C");
                sapCostReimbursement.setZepFtypename("付款申请流程");
                return forward("payApply",sapCostReimbursement);
            case "refund":
                sapCostReimbursement.setZepFtype("E");
                sapCostReimbursement.setZepFtypename("客户退款申请流程");
                return forward("refundApply",sapCostReimbursement);
        }
        return null;
    }


    public String slicedSapCostReimbursementList(){
        SDO sdo = this.getSDO();
        SapCostReimbursementQueryRequest queryRequest = sdo.toQueryRequest(SapCostReimbursementQueryRequest.class);
        Map<String, Object> map = costReimbursementApplication.slicedSapCostReimbursementQuery(queryRequest);
        return toResult(map);
    }


    public String slicedSapCostReimbursementDetailList(){
        SDO sdo = this.getSDO();
        SapCostReimbursementQueryRequest queryRequest = sdo.toQueryRequest(SapCostReimbursementQueryRequest.class);
        Map<String, Object> map = new HashMap<>();
        if (StringUtil.isNotBlank(queryRequest.getReimbursementBaseInfoId())){
            map = costReimbursementDetailApplication.slicedSapCostReimbursementDetail(queryRequest);
        }else {
            map.put("Rows",new ArrayList<>());
        }
        return toResult(map);
    }

    /**
     * 工作流审核跳转页面
     * @return
     */
    public String showSapCostReimbursement() {
        SDO sdo = this.getSDO();
        String id = sdo.getBizId();
        String isDetailPage = sdo.getString("isDetailPage");
        if("true".equals(isDetailPage))this.putAttribute("isReadOnly","true");
        SapCostReimbursement sapCostReimbursement = costReimbursementApplication.loadSapCostReimbursementById(id);
        String zepFtype = sapCostReimbursement.getZepFtype();
        switch (zepFtype) {
            case "A":
                return forward("costReimbursement",sapCostReimbursement);
            case "H":
                return forward("payApplyNoPO",sapCostReimbursement);
            case "B":
                return forward("employeeLoan",sapCostReimbursement);
            case "D":
                return forward("advancePayment",sapCostReimbursement);
            case "C":
                return forward("payApply",sapCostReimbursement);
            case "E":
                return forward("refundApply",sapCostReimbursement);
        }
        return null;
    }

    public String deleteSapCostReimbursementDetails(){
        SDO sdo = this.getSDO();
        List<String> ids = sdo.getIds();
        costReimbursementDetailApplication.deleteSapCostReimbursementDetails(ids);
        return success();
    }
//
//    /**
//     * 手动发送数据到sap
//     * @return
//     */
//    public String againSendSapDate(){
//        SDO sdo = this.getSDO();
//        String id = sdo.getBizId();
//        sapStatsIndicatorsApplication.sendSapDate(id);
//        return success();
//    }


    @SkipAuth
    @RequestMapping( value = "/paymentReceipt")
    @ResponseBody
    public HashMap<String, Object> paymentReceipt(@RequestBody PaymentReceiptVo paymentReceiptVo){
        HashMap<String, Object> messageMap = new HashMap<>();
        messageMap.put("Message_Status","S");
        messageMap.put("Message_Text","成功");
        messageMap.put("ZEP_NO",paymentReceiptVo.getBillCode());
        LOG.info("SAP财务付款凭证数据=={}", JSONUtil.toString(paymentReceiptVo));
        try {
            this.costReimbursementApplication.savePaymentReceiptVo(paymentReceiptVo);
        }catch (RuntimeException e){
            messageMap.put("Message_Status","E");
            messageMap.put("Message_Text",e.getMessage());
            messageMap.put("ZEP_NO",paymentReceiptVo.getBillCode());
        }
        return messageMap;
    }

    /**
     * 根据发票号校验是否已经提交过该发票号
     * @return
     */
    public String checkzyl06(){
        SDO sdo = this.getSDO();
        String zyl06 = sdo.getString("zyl06");
        Integer count = costReimbursementApplication.checkzyl06(zyl06);
        return success(count);
    }
}