package weaver.interfaces.organization.cronjob;

import com.engine.organization.entity.map.JclOrgMap;
import com.engine.organization.entity.staff.po.StaffPO;
import com.engine.organization.mapper.jclorgmap.JclOrgMapper;
import com.engine.organization.mapper.staff.StaffMapper;
import com.engine.organization.util.OrganizationDateUtil;
import com.engine.organization.util.db.MapperProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.sql.Date;
import java.util.List;

/**
 * 定时任务刷新在岗数、编制数
 */
public class JobAndPlanCron extends BaseCronJob {

    private static final Logger LOGGER = LoggerFactory.getLogger("chartDataLog");

    char separator = Util.getSeparator();

    private JclOrgMapper getJclOrgMapMapper(){
        return MapperProxyFactory.getProxy(JclOrgMapper.class);
    }

    private StaffMapper getStaffMapper() {
        return MapperProxyFactory.getProxy(StaffMapper.class);
    }
    @Override
    public void execute() {
        // 计划任务需要处理的逻辑
        RecordSet recordSet = new RecordSet();
        String fType = "3";
        String currentDate = OrganizationDateUtil.getFormatLocalDate(new java.util.Date());
        List<JclOrgMap> jclOrgMaps = getJclOrgMapMapper().getJclOrgMapByType(fType,new java.sql.Date(OrganizationDateUtil.stringToDate(currentDate).getTime()));

        for (JclOrgMap jclOrgMap : jclOrgMaps){
            int id = jclOrgMap.getId();
            countJobAndPlans(fType,id,currentDate);
        }
    }

    /**
     * 刷新在岗、编制数
     */
    void countJobAndPlans(String type,int id,String currentDate) {
        Date date = new Date(OrganizationDateUtil.stringToDate(currentDate).getTime());
        RecordSet rs = new RecordSet();
        StaffPO staffPO = null;
        switch (type) {
            case "1":
                staffPO = getStaffMapper().getStaffsByParamId(Integer.parseInt(type), String.valueOf(id), null, null);
                break;
            case "2":
                staffPO = getStaffMapper().getStaffsByParamId(Integer.parseInt(type), null, String.valueOf(id), null);
                break;
            case "3":
                staffPO = getStaffMapper().getStaffsByParamId(Integer.parseInt(type), null, null, String.valueOf(id));

                rs.executeQuery("select count(1) as fonjob from jcl_org_map where ftype=4 and fparentid="+id+" and fdateend>"+currentDate);
                if (rs.next()) {
                    getJclOrgMapMapper().updateMapById(id, null, Integer.valueOf(rs.getString("fonjob")), date);
                }
                break;
            default:
                break;
        }
        if (staffPO != null) {
            // 处理自身
            getJclOrgMapMapper().updateMapById(id, staffPO.getStaffNum(), null, date);
        }
        //处理上级
        String sql = "select fparentid from jcl_org_map where ftype=? and id=? and fdateend>?";
        String typeSql = "select ftype from jcl_org_map where id=? and fdateend>?";
        rs.executeQuery(sql, type, id, currentDate);
        String fparentid = null;
        String ftype = null;
        if (rs.next()) {
            fparentid = rs.getString("fparentid");
            rs.executeQuery(typeSql, fparentid, currentDate);
            if (rs.next()) {
                ftype = rs.getString("ftype");
            }
        }
        JclOrgMap jclOrgMap = getJclOrgMapMapper().getSumPlanAndJobByFParentId(date, fparentid);
        getJclOrgMapMapper().updateMapById(Integer.parseInt(fparentid), jclOrgMap.getFPlan(), jclOrgMap.getFOnJob(), date);

        if (!"-1".equals(fparentid)) {
            countJobAndPlans(ftype, Integer.parseInt(fparentid), String.valueOf(currentDate));
        }
    }


}
