package com.engine.attendance.component.persongroup.service.impl;


import com.engine.attendance.component.persongroup.service.PersonGroupService;
import com.engine.common.util.DbTools;
import com.engine.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
public class PersonGroupServiceImpl extends Service implements PersonGroupService {
    @Override
    public Map<String, Object> getHrmCondition(Map<String, Object> params, User user) {
        //获取字段类型 0:日期类型,1:下拉选择类型,2:手工自由输入类型
        String type = Util.null2String(params.get("fieldType"));
        String condition = "";
        if ("0".equals(type)){
            condition = " and  fieldhtmltype='3' and type=2 ";
        }else if ("1".equals(type)){
            condition = " and fieldhtmltype = '5' ";
        }else if ("2".equals(type)){
            condition = " and fieldhtmltype <> '3' and fieldhtmltype <> '5' ";
        }

        Map<String, Object> data = Maps.newHashMap();
        String sql = "select * from (select t1.fieldid,t2.fieldname,t1.fieldlable,t1.ismand,t2.fielddbtype,t2.fieldhtmltype,t2.type, t1.dmlurl, t1.fieldorder, t1.isuse, t1.isModify,'1' as allowModify, t1.groupid, 1 as allowhide from cus_formfield t1, cus_formdict t2 where t1.fieldid=t2.id and t1.scope='HrmCustomFieldByInfoType' and (t1.scopeid='1' or t1.scopeid='-1' or t1.scopeid='3' ) " +
                " UNION all  " +
                " select fieldid, fieldname, fieldlabel ,ismand ,fielddbtype ,fieldhtmltype ,type , dmlurl,fieldorder ,isuse ,isModify ,allowModify, groupid, allowhide from hrm_formfield)  hrmallfield  where groupid in (select id from hrm_fieldgroup where (grouptype = '1' or grouptype = '-1' or grouptype = '3')) ";

        sql = sql +condition+" order by hrmallfield.groupid, hrmallfield.fieldorder ";
        log.info("sql :[{}]",sql);
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql);
        sql = "select fieldid,selectvalue,selectname from hrm_selectitem";
        List<Map<String,Object>> selectItemLists = DbTools.getSqlToList(sql);
        Map<String, List<Map<String,Object>>> selectItemMap = selectItemLists.stream().collect(Collectors.groupingBy(e -> e.get("fieldid").toString()));

        sql = "select fieldid,selectvalue,selectname from cus_selectitem";
        List<Map<String,Object>> custSelectItemLists = DbTools.getSqlToList(sql);
        Map<String, List<Map<String,Object>>> custSelectItemMap = custSelectItemLists.stream().collect(Collectors.groupingBy(e -> e.get("fieldid").toString()));


        log.info("selectItemMap : [{}]",selectItemMap);

        //sql = "select type,linkurl from wf_browser_config group by type";
        sql = "select type,linkurl from wf_browser_config ";
        List<Map<String,Object>> browserTypeLists = DbTools.getSqlToList(sql);
        Map<String,String> browserTypeMaps = Maps.newHashMap();
        for (Map<String,Object> browserType:browserTypeLists){
            browserTypeMaps.put(Util.null2String(browserType.get("type")),Util.null2String(browserType.get("linkurl")));
        }
        log.info("browserTypeMaps : [{}]",browserTypeMaps);

        for (Map<String,Object> map :dataList){
            Integer fieldlable = Integer.valueOf(map.get("fieldlable").toString());
            String fieldname = Util.null2String(map.get("fieldname"));
            String showName = SystemEnv.getHtmlLabelName(fieldlable, user.getLanguage());
            map.put("showName",showName);
            map.put("linkurl",browserTypeMaps.get(map.get("type").toString()));
            map.put("selectItems",selectItemMap.get(map.get("fieldid").toString()));
            if (fieldname.contains("field")){
                map.put("selectItems",custSelectItemMap.get(map.get("fieldid").toString()));
            }
        }

        data.put("data",dataList);

        return data;
    }


    @Override
    public Map<String, Object> saveApplicableOrganization(Map<String, Object> params) {
        String billId = Util.null2String(params.get("billid"));
        String tableName = Util.null2String(params.get("tableName"));
        String modeId = Util.null2String(params.get("modeId"));
        String data = Util.null2String(params.get("data"));
        Map<String,Object> resultMap = Maps.newHashMap();

        List<Map<String,String>> dataList = Utils.resolveList_Map(data);
        //清空原有数据
        String sql = "delete from uf_jcl_syzz where modeid=? and dataid =?";
        RecordSet rs = new RecordSet();
        boolean updateResult = DbTools.update(rs,sql,modeId,billId);
        List<List> addLists = new ArrayList<>();
        log.info("updateResult : {}",updateResult);
        String insertSql = "insert into uf_jcl_syzz (dxlx,dx,aqjb,modeid,dataid) values (?,?,?,?,?)";
        if (updateResult){
            dataList.forEach(e->{
                ArrayList<Object> list = new ArrayList<>();
                list.add(e.get("dxlx"));
                list.add(e.get("dx"));
                list.add(e.get("aqjb"));
                list.add(modeId);
                list.add(billId);
                addLists.add(list);
            });
        }
        log.info("insert data : [{}]",addLists);
        resultMap.put("result",rs.executeBatchSql(insertSql,addLists));
        return resultMap;
    }
}
