package com.engine.htxc.contract.service.impl;

import com.engine.core.impl.Service;
import com.engine.htxc.commonutil.base.BaseUtil;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.contract.service.ContractUpdateWfService;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractUpdateWfServiceImpl.java
 * @createTime 2022年08月12日 11:32:00
 */
public class ContractUpdateWfServiceImpl extends Service implements ContractUpdateWfService {

    private DbTools dbtools=new DbTools();
    private PropBean pb=new PropBean();
    private String wktablename="";
    private String modetablename="";
    @Override
    public void updateworkflow(Map<String, Object> map, User user) {
        this.modetablename = Util.null2String(map.get("tablename"));
        String billid = Util.null2String(map.get("billid"));
        String sql="select htbh,yz from "+modetablename+" where id=?";
        Map<String,Object> mapvalue= dbtools.getsqltomap(sql,billid);
        if(mapvalue==null || mapvalue.size()<=0){
            return ;
        }
        String htbh=Util.null2String(mapvalue.get("htbh"));
        String yz=Util.null2String(mapvalue.get("yz"));
        if(StringUtils.isBlank(htbh)){
            return ;
        }
        getwktablename();
        List<Map<String,Object>> maplist=getwktableall(htbh);
        if(maplist.size()<=0){
            return ;
        }
        maplist.forEach((item)->{
          String id=Util.null2String(item.get("id"));
          String xzhtlb=Util.null2String(item.get("xzhtlb"));
          String yzoldvalue=Util.null2String(item.get("yz"));
          String yznewvalue=yz;
              if(!StringUtils.isBlank(id) && !StringUtils.isBlank(xzhtlb)){
                if(xzhtlb.split(",").length>1){
                    yznewvalue=getmodeesealall(modetablename,xzhtlb);
                }
                if(!BaseUtil.stringcompare(yznewvalue,yzoldvalue)){
                    Map<String,String> setmap=new HashMap<>();

                    setmap.put("yz", Arrays.stream(yznewvalue.split(",")).distinct().collect(Collectors.joining(",")));

                    Map<String,String> wheremap=new HashMap<>();
                    wheremap.put("id",id);
                    dbtools.updateSql(this.wktablename,setmap,wheremap);
                };
              }
        });
    }


    private List<Map<String,Object>> getwktableall(String htbh){
        List<Map<String,Object>> listmap=new ArrayList<>();
        if(StringUtils.isBlank(htbh)){
            return listmap;
        }

        if(StringUtils.isBlank(this.wktablename)){
            return listmap;
        }
       String sql="select id,yz,xzhtlb from "+this.wktablename+" where  instr(','||xzhtlb||',',',"+htbh+",')>0";
        return dbtools.getSqlToList(sql);
    }

    private void getwktablename(){
        String number=pb.getPropName("contract.approval.unnumber");
         this.wktablename=dbtools.getUnnumberToTablename(number);
    }

    private String getmodeesealall(String tablename,String htbh){
        if(StringUtils.isBlank(tablename) || StringUtils.isBlank(htbh)){
            return "";
        }
        String sql="select wm_concat_old(yz) as yz from "+tablename+" where htbh in ("+
                  Arrays.stream(htbh.split(",")).distinct().filter(Objects::nonNull).map((item)->"\'"+item+"'").collect(Collectors.joining(","))
              +")";
         return dbtools.getsqlonevalue(sql);
      }

}
