package com.mortals.xhx.module.appointment.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.busiz.req.appoint.AppointReq;
import com.mortals.xhx.busiz.rsp.appoint.AppointmentSubmitDataInfo;
import com.mortals.xhx.common.code.AppointmentStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.appointment.model.AppointmentConstraintEntity;
import com.mortals.xhx.module.appointment.model.AppointmentConstraintQuery;
import com.mortals.xhx.module.appointment.service.AppointmentConfigService;
import com.mortals.xhx.module.appointment.service.AppointmentConstraintService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.appointment.dao.AppointmentRecordsDao;
import com.mortals.xhx.module.appointment.model.AppointmentRecordsEntity;
import com.mortals.xhx.module.appointment.service.AppointmentRecordsService;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * AppointmentRecordsService
 * 预约签到记录 service实现
 *
 * @author zxfei
 * @date 2023-04-17
 */
@Service("appointmentRecordsService")
@Slf4j
public class AppointmentRecordsServiceImpl extends AbstractCRUDServiceImpl<AppointmentRecordsDao, AppointmentRecordsEntity, Long> implements AppointmentRecordsService {

    @Value("${php.host:''}")
    private String host;
    @Autowired
    private AppointmentConstraintService constraintService;
    @Autowired
    private AppointmentConfigService appointmentConfigService;


    @Override
    public Rest<String> doTakeNum(AppointmentRecordsEntity appointmentRecordsEntity) {
        String registerPath = "/inter/Actuary/orderTakebusiness";
        String url = host + registerPath;

        AppointmentConstraintEntity constraintEntity = constraintService.selectOne(new AppointmentConstraintQuery().idCardNo(appointmentRecordsEntity.getIdNumber()));
        if (!ObjectUtils.isEmpty(constraintEntity)) {
            //查看约束情况
            if (constraintEntity.getConstraint() == YesNoEnum.YES.getValue()) {
                constraintEntity.setConstraintTime(new Date());
                constraintService.update(constraintEntity);
                return Rest.fail(String.format("当前号码:%s已经被约束，不做取号服务！", appointmentRecordsEntity.getIdNumber()));
            }
        }

        //请求取号
        AppointReq appointReq = new AppointReq();
        appointReq.setSiteid(appointmentRecordsEntity.getSiteId());
        appointReq.setNumber(appointmentRecordsEntity.getReservationNumber());

        Map<String, Object> params = new HashMap<>();
        params.put("siteid", appointmentRecordsEntity.getSiteId());
        params.put("number", appointmentRecordsEntity.getReservationNumber());

        log.info("appointment submit req=>{}", JSON.toJSONString(params));
        String respJson = HttpUtil.post(url, params);
        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");
            this.update(appointmentRecordsEntity);
        }
        return Rest.ok();
    }
}