package com.sifang.modules.task;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.q7link.utils.OpenapiTemplate;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class AttachmentUrlTask {
    Logger log = LoggerFactory.getLogger(this.getClass());

    //@Scheduled(cron = "0 0 5 * * ? ")
    public void attachmentUrlTask() {
        log.info("开始同步修改附件的地址");
        StringBuilder updateSql=new StringBuilder();
        updateSql.append("  update crm_supply_material_deliver_order set in_stock_attachment_url=? where id=? ");
        StringBuilder querySql=new StringBuilder();
        querySql.append(" select  a.id,a.in_stock_attachment_key from crm_supply_material_deliver_order as a where  a.in_stock_attachment_key is not null and a.in_stock_attachment_key != '' ");
        List<Record> records = Db.find(querySql.toString());
        for(Record rd:records){
              String id=rd.getStr("id");
              String inStockAttachmentKey=rd.getStr("in_stock_attachment_key");
              String inStockAttachmentUrl="";
              if(StringUtils.isEmpty(inStockAttachmentKey)){
                  continue;
              }
              String[] arr=inStockAttachmentKey.split(",");
              for(int i=0;i<arr.length;i++){
                  inStockAttachmentUrl+= OpenapiTemplate.getAttachmentUrl(arr[0]);
                  if( i != arr.length-1){
                      inStockAttachmentUrl+=",";
                  }
              }
            int result=  Db.update(updateSql.toString(),inStockAttachmentUrl,id);
              log.info("修改附件URL  result------>>>>>>",result);

        }

    }
}
