package com.engine.attendance.attendanceplan.cmd;

import com.engine.attendance.component.persongroup.commonutil.PersongroupCommonUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.DbTools;
import com.engine.common.util.Utils;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;

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

@Slf4j
public class GetAttendanceItemsByPersonDataCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetAttendanceItemsByPersonDataCmd(Map<String,Object> params){
        this.params=params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String resourceId = Util.null2String(params.get("resourceId"));
        String startDate = Util.null2String(params.get("startDate"));
        String endDate = Util.null2String(params.get("endDate"));
        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_kqfa");
        Map<String,Object> resultMap = Maps.newHashMap();

        String sql = "select dxlx,dataid,dx from uf_jcl_syzz where modeid=?";
        List<Map<String,Object>> organizationList = DbTools.getSqlToList(sql,modeId);
        sql = "select id,departmentid,subcompanyid1 from hrmresource where id =?";
        Map<String,Object> departMentMap = DbTools.getSqlToMap(sql,resourceId);
        Set<String> dataIds = Sets.newHashSet();
        Map<String, List<Map<String,Object>>> organizationListGroupBydxlx =  organizationList.stream().collect(Collectors.groupingBy(e ->e.get("dxlx").toString()));
        //对象类型为人员
        List<Map<String,Object>> personOrganizationList = organizationListGroupBydxlx.get("0");
        //对象类型为人员组织
        List<Map<String,Object>> personGroupOrganizationList = organizationListGroupBydxlx.get("1");
        //对象类型为部门
        List<Map<String,Object>> departmentOrganizationList = organizationListGroupBydxlx.get("2");
        //对象类型为分部
        List<Map<String,Object>> subCompanyOrganizationList = organizationListGroupBydxlx.get("3");
        if (personOrganizationList != null){
            for (Map<String,Object> personOrganization :personOrganizationList){
                String dx = Util.null2String(personOrganization.get("dx"));
                String ids = dx.split("-")[0];
                if (ids.equals(resourceId)){
                    dataIds.add(Util.null2String(personOrganization.get("dataid")));
                }
            }
        }
        try {
            if (departmentOrganizationList != null){
                String deptid = Util.null2String(departMentMap.get("departmentid"));
                String pdeptids = "";
                pdeptids = new DepartmentComInfo().getAllParentDepartId(Util.null2String(departMentMap.get("departmentid")), pdeptids);
                pdeptids = deptid + pdeptids;
                log.info("pdeptids : [{}]",pdeptids);
                for (Map<String,Object> departmentOrganization :departmentOrganizationList){
                    String dx = Util.null2String(departmentOrganization.get("dx"));
                    String ids = dx.split("-")[0];
                    for (String pdeptid : pdeptids.split(",")){
                        if (pdeptid.equals(ids)){
                            dataIds.add(Util.null2String(departmentOrganization.get("dataid")));
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("catch error :{}",e);
        }
        if (subCompanyOrganizationList != null){
            String subCompanyId = Util.null2String(departMentMap.get("subcompanyid1"));
            for (Map<String,Object> subCompanyOrganization :subCompanyOrganizationList){
                String dx = Util.null2String(subCompanyOrganization.get("dx"));
                String ids = dx.split("-")[0];
                if (ids.equals(subCompanyId)){
                    dataIds.add(Util.null2String(subCompanyOrganization.get("dataid")));
                }
            }
        }
        if (personGroupOrganizationList != null){
            Set<String> personGroupIds = personGroupOrganizationList.stream().map(e -> Util.null2String(e.get("dx")).split("-")[0]).collect(Collectors.toSet());
            sql = "select mainid,empid,filters,bdate,edate from uf_ryqz_dt1  where  mainid in ("+String.join(",",personGroupIds)+")";
            log.info("query personGroupData : {}",sql);
            List<Map<String,Object>> personGroupData = DbTools.getSqlToList(sql);
            Set<String> personnelGroupIds = PersongroupCommonUtil.getPersonnelGroupingByPerson(personGroupData,resourceId,startDate,endDate);
            log.info("personnelGroupIds : {}",personnelGroupIds);
            for (Map<String,Object> personGroupOrganization :personGroupOrganizationList){
                String personnelGroupId = Util.null2String(personGroupOrganization.get("dx")).split("-")[0];
                if (personnelGroupIds.contains(personnelGroupId)){
                    dataIds.add(Util.null2String(personGroupOrganization.get("dataid")));
                }
            }
        }

        sql = "select id keyid,mc kqxm,a.* from uf_jcl_kq_kqxm a where tyxm=1";
        List<Map<String,Object>> attendanceItems  = DbTools.getSqlToList(sql);

        log.info("dataIds : {}",dataIds);
        sql = "select b.id keyid,b.mc kqxm,c.mc famc,b.*  from uf_jcl_kq_kqfa_dt1 a left join uf_jcl_kq_kqxm b   on a.kqxm=b.id  left join uf_jcl_kq_kqfa c on a.mainid=c.id where mainid in ("+String.join(",",dataIds)+")";
        if (dataIds.size() > 0){
            log.info("query attendanceItems sql : {}",sql);
            attendanceItems.addAll(DbTools.getSqlToList(sql));
        }
        resultMap.put("data",attendanceItems);
        resultMap.put("total",attendanceItems.size());

        return resultMap;
    }
}
