package com.tofly.xrtygis.handle.plan;

import com.tofly.common.core.util.DateUtil;
import com.tofly.xrtygis.callCenterMapper.PltFlowMapper;
import com.tofly.xrtygis.callCenterMapper.PltFlowTransnoMapper;
import com.tofly.xrtygis.service.DutyrosterService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.tofly.common.core.util.DateUtil.DATE_DEFAULT_FORMAT;

/**
 * 定时更改呼叫中心夜班人员为转接号码
 *
 * @author JF
 */
@Component
public class CallChangeNightNumHandler {

    @Autowired
    private PltFlowTransnoMapper pltFlowTransnoMapper;
    @Autowired
    private PltFlowMapper pltFlowMapper;
    @Autowired
    private DutyrosterService dutyrosterService;

    @XxlJob("callChangeNightNumHandler")
    public ReturnT<String> automaticallySignBackHandler(String param) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        if(sdf.format(new Date()).compareTo("07:50:00") > 0 && sdf.format(new Date()).compareTo("07:59:59") < 0){
            updateTransferNum("1");
        }else if(sdf.format(new Date()).compareTo("11:50:00") > 0 && sdf.format(new Date()).compareTo("11:59:59") < 0){
            updateTransferNum("4");
        }else if(sdf.format(new Date()).compareTo("17:50:00") > 0 && sdf.format(new Date()).compareTo("17:59:59") < 0){
            updateTransferNum("2");
        }else if(sdf.format(new Date()).compareTo("00:00:00") > 0 && sdf.format(new Date()).compareTo("00:30:00") < 0){
            updateTransferNum("2");
        }
        return ReturnT.SUCCESS;
    }

    //更改排版人员号码为转接号码
    private void updateTransferNum(String workTime){
        List<String> personList = dutyrosterService.getNightPersonByDay(DateUtil.parseDateToStr(DATE_DEFAULT_FORMAT,new Date()), workTime);
        StringBuilder phones = new StringBuilder();
        if(personList.size() == 0){
            return;
        }
        pltFlowTransnoMapper.deletePltFlowTransno();
        for(int i = 0;i < personList.size();i++){
            if(StringUtils.isNotBlank(personList.get(i)) && !personList.get(i).equals("null")){
                phones.append(personList.get(i));
                if(i != personList.size() - 1){
                    phones.append("$");
                }
                pltFlowTransnoMapper.insertPltFlowTransno(personList.get(i));
            }
        }
        pltFlowMapper.updatePltFlow(phones.toString());
    }
}
