package com.weaver.seconddev.njwebservice;

import com.alibaba.fastjson.JSON;
import com.weaver.common.hrm.util.Util;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.njwebservice.entity.DiscountedInvoiceInfoReq;
import com.weaver.seconddev.njwebservice.entity.ResponseInfo;
import com.weaver.seconddev.njwebservice.prop.ConfigProp;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liang.cheng
 * @Date 2025/1/21 14:09
 * @Description: ZWZX-YS05-直贷业务发票申请流程（电子报账系统触发）
 * @Version 1.0
 */
@Service("discountedInvoiceApplicationService")
public class DiscountedInvoiceApplicationService {

    private final static Logger log = LoggerFactory.getLogger(AutoCreateWorkflowService.class);


    @Autowired
    private AutoCreateWorkflowService autoCreateWorkflowService;

    @Autowired
    private ConfigProp configProp;  //注入

    public ResponseInfo discountedInvoiceApplicate(DiscountedInvoiceInfoReq param) {
        log.error("DiscountedInvoiceApplicationService:"+param.toString());
        ResponseInfo res = new ResponseInfo();
        String systemFlag = param.getSystemFlag();
        if(!"EAS".equals(systemFlag)){
            res.setISTAT("E");
            res.setRequestid("");
            res.setSRMSAGE("系统标识不符！");
            return res;
        }
        String creator = "";
        String companycode = param.getCompanyCode();

        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.ebuilder.getType();
        rs.executeSql("select easfqrgh from uf_applyUser where companycode='"+companycode+"' and lclx=1",poolname);
        if(rs.next()){
            creator = Util.null2String(rs.getString("easfqrgh"));
        }

        log.error("发票申请人："+creator);

        if("".equals(creator)){
            res.setISTAT("E");
            res.setRequestid("");
            res.setSRMSAGE(param.getCompanyName()+"在OA系统里没有维护应收折现发票申请人");
            return res;
        }

        if(param.getCompanyName() != null && !"".equals(param.getCompanyName())){
            rs.executeSql("select id from eteams.dbo.department where type = 'subcompany' and name = '"+param.getCompanyName()+"'",CONN_TYPE.hrm.getType());
            if(!rs.next()){
                res.setISTAT("E");
                res.setRequestid("");
                res.setSRMSAGE("申请公司在OA系统中不存在！");
                return res;
            }
        }

        if(param.getSh()==null||"".equals(param.getSh())){
            res.setISTAT("E");
            res.setRequestid("");
            res.setSRMSAGE("税号不能为空！");
            return res;
        }

        //String gscode = Util.null2String(getPropValue("htwsaccount", "ZWZDYW")).trim();
        //todo 配置文件未建立
        String gscode = configProp.getZWZDYW();

        List<String> list = Arrays.asList(gscode.split(","));
        String workflowId = "100003460000004207";//e10 100003460000004207  e9 4207
        String tablename = "formtable_main_2304";//e10 formtable_main_2304 e9 formtable_main_2304

        if(list.contains(param.getCompanyCode())){
            workflowId = "100003460000004574";//e10 100003460000004574  e9 4574
            tablename = "formtable_main_2438";//e10 formtable_main_2638   e9 formtable_main_2638
        }

        if(param.getBoeNum()!=null&&!"".equals(param.getBoeNum())){
            rs.executeSql("select boeNum from "+tablename+" where boeNum='"+param.getBoeNum()+"'",CONN_TYPE.workflow.getType());
            if(rs.next()){
                res.setISTAT("E");
                res.setRequestid("");
                res.setSRMSAGE("申请单号已存在，请不要重复申请！");
                log.error("DiscountedInvoiceApplicationImpl-Error:state="+res.getISTAT()+"||message="+res.getSRMSAGE());

                return res;
            }
        }

        String loginname = "EAS";
        String password = "123456";

        String requestLevel = "0";

        Map<String,String> map = new HashMap<>();
        map.put("hrm:sqr", creator);
        map.put("com:sqr:sqdw", "");
        map.put("dep:sqr:sqbm", "");
        map.put("boeNum", param.getBoeNum());
        map.put("boeHeaderId", param.getBoeHeaderId());
        map.put("companyName", param.getCompanyName());
        map.put("companyCode", param.getCompanyCode());
        map.put("boeDeptName", param.getBoeDeptName());
        map.put("boeDeptCode", param.getBoeDeptCode());
        map.put("vendorCode", param.getVendorCode());
        map.put("vendorName", param.getVendorName());
        map.put("applyDate", param.getApplyDate());
        map.put("applyAmount", param.getApplyAmount());
        map.put("sh", param.getSh());
        String mainTable = JSON.toJSONString(map);
        String[] detailTables = {};
        String result = autoCreateWorkflowService.createWorkflow(loginname, password, workflowId, creator, requestLevel, mainTable, detailTables);
        log.error("DiscountedInvoiceApplicationImpl:"+result);
        if(result.matches("^[1-9]+[0-9]*$")){
            res.setISTAT("S");
            res.setRequestid(result);
            res.setSRMSAGE("流程创建成功！");
        }else{
            res.setISTAT("E");
            res.setRequestid("");
            res.setSRMSAGE(result);
        }
        return res;
    }

}
