package com.engine.htsc.jjhyy.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.jjhyy.dao.JjhyybaseDao;
import com.engine.htsc.jjhyy.enitry.Uf_jjhhtbh;
import com.engine.htsc.jjhyy.service.JjhyybaseService;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.DeptUtils;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName JjhyybaseService.java
 * @createTime 2022年03月18日 10:39:00
 */
public class JjhyybaseServiceImpl extends Service implements JjhyybaseService {
    private PropBean pb=new PropBean();

    private String date = DateUtil.format(new Date(), "yyyy-MM-dd");

    @Override
    public boolean getwkyygldoneuser(User user,Map<String, Object> map) {
        String nodenumbber=Util.null2String(pb.getPropName("jjhyy.nodenumber.jjhyy_yyglfh"));
        String requestid=Util.null2String(map.get("requestid"));
        Set<String> useridSet = new HashSet<>();
        String sql = "select operator,agentorbyagentid from workflow_requestlog where requestid = '"+requestid+"'  " +
                "  and nodeid in ( select id from workflow_nodebase where unnumber='"+nodenumbber+"')   order by  to_date((operatedate || ' ' || OPERATETIME),'yyyy-MM-dd hh24:mi:ss')  desc";
        boolean falg=false;
        List list =JAVATools.GetTableToListMap(sql,null);
        if(list!=null && list.size()>0){
            falg=true;
        }
        return falg;
    }

    @Override
    public Map<String, Object> getsealname(User user, Map<String, Object> map) {
       String unnumber=Util.null2String(map.get("unnumber"));
        String sealname=  Util.null2String(map.get("sealname"));
        Map<String,Object> resultMap = new HashMap<>();
        if(StringUtils.isBlank(unnumber) || StringUtils.isBlank(sealname)){
            return null;
        }
        Arrays.stream(sealname.split(",")).forEach((value)->{
           Map<String,Object> mapvalue= getsealmap(unnumber,value);
           if(mapvalue!=null && mapvalue.size()>0){
               resultMap.put(value,mapvalue);
           }
        });
        return resultMap;
    }


    private Map<String,Object> getsealmap(String lcxx,String eselname){
         if(StringUtils.isBlank(lcxx) || StringUtils.isBlank(eselname)){
             return null;
         }
        String sql = "select t1.id,t1.yzmc,t1.yzlx,t1.pxzd from uf_yinzhang" +
                " t1,uf_tsld t2 where t2.yz = t1.id ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("t2.lcxx",lcxx);
        wheremap.put("t2.qt",eselname);
       return JAVATools.GetTableToMap(sql,wheremap);
    }


    @Override
    public void createdetailnumber(User user, Map<String,Object> map) {
        String requestid= Util.null2String(map.get("requestid"));
        String tablename= Util.null2String(map.get("tablename"));
        if(StringUtils.isBlank(requestid) || StringUtils.isBlank(tablename) || user==null){
            return ;
        }
        List<Map<String,Object>> strmap= JjhyybaseDao.getdetailnumber(map);
        strmap.forEach(item->{
            String id=Util.null2String(item.get("id"));
            String wjmc=Util.null2String(item.get("wjmc"));
            int wh=createwh(requestid,wjmc,user);
            Map<String,String> wheremap=new HashMap<>();
            wheremap.put("id",id);
            Map<String,String> updatemap=new HashMap<>();
            String newunnumber= pb.getPropName("jjhyy.unnumbername")+date.replace("-","")+reloadwh(wh,4);
            updatemap.put("htbh",newunnumber);
            JAVATools.Updatesql(updatemap,tablename+"_dt7",wheremap);
        });
    }

    public String reloadwh(int wh,int length){
        if(length<=0 && wh<=0){
            return "0001";
        }
        if(wh>=10000){
            return Util.null2String(wh);
        }
        String formatstr="%"+length+"d";
        String whStr = String.format(formatstr,wh).replace(" ","0");
        return whStr;
    }

    private int createwh(String requestid,String wjcm,User user){
        int unnumber = 1;
        try {

            //去询了
            String sql = "select max(wh) wh from  uf_jjhhtbh where 1=1";
            Map<String, String> retmap = new HashMap<>();
            retmap.put("nf", date);
            Map<String, Object> whcount = JAVATools.GetTableToMap(sql, retmap);
            if (whcount == null || whcount.size() <= 0) {
                return unnumber;
            }
            unnumber = Util.getIntValue(Util.null2String(whcount.get("wh")))+1;
            if (unnumber <= 0) {
                unnumber = 1;
            }
            Uf_jjhhtbh uf_jjhhtbh = new Uf_jjhhtbh();
            uf_jjhhtbh.setLcid(requestid);
            uf_jjhhtbh.setBm(user.getUserDepartment());
            uf_jjhhtbh.setNf(date);
            uf_jjhhtbh.setWh(unnumber);
            uf_jjhhtbh.setNgr(user.getUID());
            uf_jjhhtbh.setWjmc(wjcm);
            uf_jjhhtbh.setCjsj(date);
            uf_jjhhtbh.create();
        }catch (Exception e){
            return 1;
        }
        return unnumber;
    }

    @Override
    public JSONObject getOtherReadUser(User user, Map<String, Object> map) {
        String wjlb=Util.null2String(map.get("wjlb"));
        JSONObject jsonObject=new JSONObject();
        JSONArray jsonArray=new JSONArray();
        List<String> userstr=new ArrayList<>();
        if(StringUtils.isBlank(wjlb) ){
            return null;
        }
        try {
            if ("1".equals(wjlb) || "2".equals(wjlb)) {
                //获取对应的人
                String jscode = pb.getPropName("jjhyy.jjhjscode");
                List<Map<String, Object>> userlist = DeptUtils.getDepartmentCodeToUserids(jscode);
                if (userlist != null && userlist.size() > 0) {
                    userlist.forEach((value) -> {
                        String id = Util.null2String(value.get("id"));
                        String lastname = Util.null2String(value.get("lastname"));
                        if (!StringUtils.isBlank(id) && !StringUtils.isBlank(lastname)) {
                            JSONObject userjson = new JSONObject();
                            userjson.put("id", id);
                            userjson.put("name", lastname);
                            jsonArray.add(userjson);
                            userstr.add(id);
                        }
                    });

                }
            }
        }catch (Exception e){
            return null;
        }
        jsonObject.put("value",userstr.stream().collect(Collectors.joining(",")));
        jsonObject.put("specialobj",jsonArray);

        return jsonObject;
    }


}
