package com.engine.recruit.service.impl;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.recruit.entity.workplan.WorkPlanCreateParams;
import com.engine.recruit.enums.InterviewOperateTypeEnum;
import com.engine.recruit.service.WorkPlanSerivice;
import com.engine.workplan.service.WorkPlanBaseService;
import com.engine.workplan.service.impl.WorkPlanBaseServiceImpl;
import weaver.conn.RecordSet;
import weaver.hrm.User;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author liang.cheng
 * @Date 2023/10/17 10:52 AM
 * @Description:
 * @Version 1.0
 */
public class WorkPlanServiceImpl extends Service implements WorkPlanSerivice {

    private WorkPlanBaseService getService(User user) {
        return ServiceUtil.getService(WorkPlanBaseServiceImpl.class, user);
    }

    @Override
    public void operate(WorkPlanCreateParams workPlanCreateParams) {
        RecordSet rs = new RecordSet();

        String operateType = workPlanCreateParams.getOperateType();
        String billId = workPlanCreateParams.getBillId();
        Map<String, Object> maps = new HashMap<>(16);

        if (InterviewOperateTypeEnum.ARRANGE.getOperateType().equals(operateType) || InterviewOperateTypeEnum.ADJUSTMENT.getOperateType().equals(operateType)){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            LocalDateTime dateTime = LocalDateTime.parse(workPlanCreateParams.getBeginDateTime(), formatter);
            LocalDateTime newDateTime = dateTime.plusHours(1);
            workPlanCreateParams.setEndDateTime(newDateTime.format(formatter));
            maps.put("workPlanType",workPlanCreateParams.getWorkPlanType());
            maps.put("planName",workPlanCreateParams.getPlanName());
            maps.put("memberIDs",workPlanCreateParams.getMemberIds());
            maps.put("urgentLevel",workPlanCreateParams.getUrgentLevel());
            maps.put("beginDateTime",workPlanCreateParams.getBeginDateTime());
            maps.put("endDateTime",workPlanCreateParams.getEndDateTime());
            maps.put("isEnableSecondAuth","0");
            //maps.put("from","workplan");
        }


        List<String> list = Arrays.stream(billId.split(","))
                .collect(Collectors.toList());
        list.forEach(id -> {
            // 取消面试
            if (InterviewOperateTypeEnum.CANCEL.getOperateType().equals(operateType)) {
                Map<String, Object> map = new HashMap<String, Object>(2){{
                    put("workid", workPlanCreateParams.getWorkId());
                }};
                getService(user).deleteWorkPlan(map);

            //完成面试
            }else if (InterviewOperateTypeEnum.EVALUATE.getOperateType().equals(operateType)) {
                Map<String, Object> map = new HashMap<String, Object>(2){{
                    put("workid", workPlanCreateParams.getWorkId());
                }};
                getService(user).finishWorkPlan(map);
                // 调整面试
            }else if (InterviewOperateTypeEnum.ADJUSTMENT.getOperateType().equals(operateType)){
                maps.put("workid",workPlanCreateParams.getWorkId());
                getService(user).addWorkPlan(maps);
                // 安排面试
            }else if (InterviewOperateTypeEnum.ARRANGE.getOperateType().equals(operateType)) {
                Map<String, Object> result = getService(user).addWorkPlan(maps);
                Integer workplanId = (Integer)result.get("workplanid");
                rs.executeUpdate("update uf_jcl_ms set glrc = ? where id = ?",workplanId,id);
            }
        });

    }
}
