package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.busiz.req.appoint.AppointReq;
import com.mortals.xhx.busiz.rsp.appoint.AppointmentSubmitDataInfo;
import com.mortals.xhx.busiz.rsp.register.AppointmentDataInfo;
import com.mortals.xhx.common.code.AppointmentStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.appointment.model.AppointmentRecordsEntity;
import com.mortals.xhx.module.appointment.model.AppointmentRecordsQuery;
import com.mortals.xhx.module.appointment.service.AppointmentPersonService;
import com.mortals.xhx.module.appointment.service.AppointmentRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * 同步提交预约任务
 */
@Slf4j
@Service("SyncSubmitAppointTask")
public class SyncSubmitAppointTaskImpl implements ITaskExcuteService {

    @Autowired
    private AppointmentRecordsService appointmentRecordsService;

    @Value("${php.host:''}")
    private String host;


    @Override
    public void excuteTask(ITask task) throws AppException {
        log.debug("同步提交预约任务");
        syncSubmitAppoint();
        log.debug("同步提交预约任务完成");

    }


    private void syncSubmitAppoint() {
        String registerPath = "/inter/Actuary/orderTakebusiness";
        String url = host + registerPath;
        AppointmentRecordsQuery appointmentRecordsQuery = new AppointmentRecordsQuery();
        appointmentRecordsQuery.setAppointmentStatus(AppointmentStatusEnum.未取号.getValue());
        appointmentRecordsQuery.setCreateTimeStart(DateUtil.today());
        appointmentRecordsQuery.setCreateTimeEnd(DateUtil.today());

        List<AppointmentRecordsEntity> appointmentRecordsList = appointmentRecordsService.find(appointmentRecordsQuery);
        for (AppointmentRecordsEntity appointmentRecordsEntity : appointmentRecordsList) {
            doTakeNum(url, appointmentRecordsEntity);
        }

    }

    private void doTakeNum(String url, AppointmentRecordsEntity appointmentRecordsEntity) {
        //请求取号
        AppointReq appointReq = new AppointReq();
        appointReq.setSiteid(appointmentRecordsEntity.getSiteId());
        appointReq.setNumber(appointmentRecordsEntity.getReservationNumber());
        log.info("appointment submit req=>{}",JSON.toJSONString(appointReq));
        String respJson = HttpUtil.post(url, JSON.toJSONString(appointReq));
        log.info("appointment submit respJson=>{}",respJson);
        Rest<AppointmentSubmitDataInfo> rest = JSON.parseObject(respJson, new TypeReference<Rest<AppointmentSubmitDataInfo>>() {
        });

        if(rest.getCode()== YesNoEnum.YES.getValue()){
            appointmentRecordsEntity.setFlowNum(rest.getData().getData().getFlownum());
            appointmentRecordsEntity.setAppointmentStatus(AppointmentStatusEnum.取号完成.getValue());
            appointmentRecordsEntity.setFlowTime(rest.getData().getData().getTime());
            appointmentRecordsEntity.setUpdateTime(new Date());
            appointmentRecordsEntity.setUpdateUserId(1L);
            appointmentRecordsEntity.setUpdateUserName("system");
            appointmentRecordsService.update(appointmentRecordsEntity);
        }
    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }

}
