package com.seeyon.apps.src_hxinvoice.quartz;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.apps.ext.quartz.AbstractQuartzTask;
import com.seeyon.apps.src_hxinvoice.constants.HxinvoiceConstants;
import com.seeyon.apps.src_hxinvoice.dao.IHxinvoiceDao;
import com.seeyon.apps.src_hxinvoice.service.IInvoiceService;
import com.seeyon.apps.src_hxinvoice.util.ProtUtil;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.organization.manager.OrgManager;
import java.util.List;
import java.util.Map;

public class QueryFastRedInvoiceResultQuartz extends AbstractQuartzTask {

    private static Log log = Log.get(QueryFastRedInvoiceResultQuartz.class);

    protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");
    public String getPluginId() {return HxinvoiceConstants.getPluginId();}
    public ConfigVo getHxinvoiceConfig() {return cstConfigApi.getConfig(getPluginId());}

    private IHxinvoiceDao hxinvoiceDao;
    public void setHxinvoiceDao(IHxinvoiceDao hxinvoiceDao) {this.hxinvoiceDao = hxinvoiceDao;}
    public IHxinvoiceDao getHxinvoiceDao() {
        if (this.hxinvoiceDao == null) {this.hxinvoiceDao = ((IHxinvoiceDao) AppContext.getBean("hxinvoiceDao"));}return hxinvoiceDao;
    }

    private IInvoiceService invoiceService;
    public void setInvoiceService(IInvoiceService invoiceService){this.invoiceService = invoiceService;}
    public IInvoiceService getInvoiceService() {
        if (this.invoiceService == null) {
            this.invoiceService = ((IInvoiceService) AppContext.getBean("invoiceService"));
        }
        return invoiceService;
    }

    private OrgManager orgManager;
    public void setOrgManager(OrgManager orgManager) {this.orgManager = orgManager; }
    public OrgManager getOrgManager() {
        if (orgManager == null) {orgManager = (OrgManager) AppContext.getBean("orgManager");}return orgManager;
    }

    @Override
    public String taskRun(String p) throws Exception {
        try{
            System.out.println("进入定时任务方法"+p);
            ConfigVo configVo = getHxinvoiceConfig();
            ProtUtil protUtil = new ProtUtil();
            String methodName = "/sims-api/invoice/api/v6/QueryFastRedInvoiceResult";
            String ipport = configVo.getParamVal(HxinvoiceConstants.ipport.name());
            String url = ipport+methodName;
//            System.out.println("调用接口路径"+url);
            log.info("调用接口路径"+url);

//          查询冲红流程所有信息
            List<Map<String,String>> queryFastRedInvoices = hxinvoiceDao.queryFastRedInvoice();
//            System.out.println("查询符合要求的数据量"+queryFastRedInvoices.size());
            log.info("查询符合要求的数据量"+queryFastRedInvoices.size());
//          遍历查询到的所有冲红信息
            for(Map<String ,String> queryFastRedInvoice:queryFastRedInvoices){
//              调用接口查询当前冲红订单是否开票成功。
                JSONObject jsonObject =invoiceService.queryFastRedInvoiceResult(methodName,queryFastRedInvoice);
                log.info("请求参数为"+jsonObject);
//              调用接口查询冲红结果
                JSONObject res = protUtil.doPost(jsonObject.toString(),"http://"+url);
                String contentStr =invoiceService.getInvoiceContentRes(res);
                JSONObject js = JSONObject.parseObject(contentStr);
//                log.info("转json"+js);
                if("000000".equals(js.getString("ZTDM"))){
//              附件查询成功调用接口通过超级节点
                    JSONObject params = new JSONObject();
                    params.put("message", "开票完成！！！");
                    params.put("returnCode", 1);
                    String rem = protUtil.sendPostNotification(params.toString(), configVo.getParamVal(HxinvoiceConstants.nodeTokenUrl.name()), queryFastRedInvoice.get("token"),configVo);
                }
        }
        }catch (Exception e){
//          异常处理，记录定时任务日志
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getName() {
        return "冲红状态查询定时任务";
    }
}
