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

import com.huigou.topsun.common.BillStatus;
import com.huigou.topsun.sap.purchaseOrder.application.SapPurchaseOrderApplication;
import com.huigou.topsun.sap.purchaseOrder.application.SapPurchaseOrderItemApplication;
import com.huigou.topsun.sap.purchaseOrder.domain.SapPurchaseOrder;
import com.huigou.topsun.sap.purchaseOrder.domain.query.SapPurchaseOrderItemQueryRequest;
import com.huigou.topsun.sap.purchaseOrder.domain.query.SapPurchaseOrderQueryRequest;
import com.huigou.topsun.sap.purchaseOrder.domain.vo.SapPurchaseOrderDto;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.annotation.SkipAuth;
import com.huigou.uasp.client.CommonController;
import com.huigou.util.JSONUtil;
import com.huigou.util.SDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: xin.lu
 * @Date: 2024/01/09/18:58
 * @Description:
 */
@Controller
@ControllerMapping("/sapPurchaseOrder")
public class SapPurchaseOrderController extends CommonController {
    private final static Logger LOG = LoggerFactory.getLogger(SapPurchaseOrderController.class);
    @Resource
    private SapPurchaseOrderItemApplication sapPurchaseOrderItemApplication;
    @Resource
    private SapPurchaseOrderApplication sapPurchaseOrderApplication;
    @Override
    protected String getPagePath() {
        return "/biz/topsun/sap/purchaseOrder/";
    }

    @SkipAuth
    @RequestMapping( value = "/getSapPurchaseOrder",consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public HashMap<String, Object> getSapPurchaseOrder(@RequestBody SapPurchaseOrderDto sapPurchaseOrderDto){
        LOG.info("SAP采购订单审批数据=={}", JSONUtil.toString(sapPurchaseOrderDto));
        //System.out.println(sapPurchaseOrderDto);
        HashMap<String, Object> messageMap = new HashMap<>();
        messageMap.put("Message_Status","S");
        messageMap.put("Message_Text","成功");
        messageMap.put("NUMBER",sapPurchaseOrderDto.getLsHeader().getEbeln());
        try {
            this.sapPurchaseOrderApplication.saveSapPurchaseOrderDto(sapPurchaseOrderDto);
        }catch (RuntimeException e){
            messageMap.put("Message_Status","E");
            messageMap.put("Message_Text",e.getMessage());
            messageMap.put("NUMBER",sapPurchaseOrderDto.getLsHeader().getEbeln());
        }
        return messageMap;
    }


    public String forwardSapPurchaseOrderList(){
        this.putAttribute("statusKindsList", BillStatus.getValidMap());
        return forward("sapPurchaseOrderList");
    }

    public String slicedSapPurchaseOrderList(){
        SDO sdo = this.getSDO();
        SapPurchaseOrderQueryRequest queryRequest = sdo.toQueryRequest(SapPurchaseOrderQueryRequest.class);
        Map<String, Object> map = sapPurchaseOrderApplication.slicedSapPurchaseOrderList(queryRequest);
        return toResult(map);
    }

    public String showSapPurchaseOrderDetail(){
        SDO sdo = this.getSDO();
        String id = sdo.getBizId();
        SapPurchaseOrder sapPurchaseOrder = sapPurchaseOrderApplication.loadSapPurchaseOrder(id);
        return forward("sapPurchaseOrderDetail",sapPurchaseOrder);
    }

    public String querySapPurchaseOrderItems(){
        SDO sdo = this.getSDO();
        SapPurchaseOrderItemQueryRequest queryRequest = sdo.toQueryRequest(SapPurchaseOrderItemQueryRequest.class);
        Map<String, Object> map = sapPurchaseOrderItemApplication.querySapPurchaseOrderItems(queryRequest);
        return toResult(map);
    }

    //撤销审批
    public String revokeApproval(){
        SDO sdo = this.getSDO();
        String id=sdo.getString("id");
        String newId=sapPurchaseOrderApplication.revokeApproval(id);
        return success();
    }

    /**
     * 打印
     * @return
     */
    public String print() {
        SDO sdo = this.getSDO();
        String id = sdo.getId();
        Map<String,Object> map = this.sapPurchaseOrderApplication.print(id);
        String pintPage = String.format("/print/topsun/processOuter.ftl");
        return outputAndProcUnitHandlerPDF(pintPage, id, map);
    }
}
