package com.zhongshu.card.server.core.service.openAPI.visitor;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.visitor.VisitorMainModel;
import com.zhongshu.card.client.model.visitor.VisitorMainParam;
import com.zhongshu.card.client.model.visitor.VisitorMainSearch;
import com.zhongshu.card.client.model.visitor.VisitorReViewParam;
import com.zhongshu.card.client.openApi.base.BusIdParam;
import com.zhongshu.card.client.openApi.model.visitor.VisitorMainOpenParam;
import com.zhongshu.card.client.openApi.model.visitor.VisitorMainOpenSearch;
import com.zhongshu.card.client.type.visitor.VisitorState;
import com.zhongshu.card.server.core.dao.scene.SceneComponentDao;
import com.zhongshu.card.server.core.dao.visitor.VisitorMainDao;
import com.zhongshu.card.server.core.domain.scene.SceneComponent;
import com.zhongshu.card.server.core.domain.visitor.VisitorMain;
import com.zhongshu.card.server.core.service.base.CommonService;
import com.zhongshu.card.server.core.service.visitor.VisitorMainService;
import com.zhongshu.card.server.core.util.BeanUtils;
import com.zhongshu.card.server.core.util.TempUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.errors.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 访客功能openAPI
 * @author TRX
 * @date 2025/3/10
 */
@Slf4j
@Service
public class VisitorOpenService {

    @Autowired
    private VisitorMainService visitorMainService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private SceneComponentDao sceneComponentDao;

    @Autowired
    private VisitorMainDao visitorMainDao;

    /**
     * 添加访客信息
     * @return
     */
    @Transactional
    public ResultContent saveApplyVisitor(VisitorMainOpenParam visitorMainParam) {
        String visitorSceneComponentCode = "sign";
        SceneComponent sceneComponent = sceneComponentDao.findTopByCode(visitorSceneComponentCode);
        if (ObjectUtils.isEmpty(sceneComponent)) {
            return ResultContent.buildFail("反馈组件未配置");
        }

        // 检查项目信息
        ResultContent<String> resultContent = commonService.checkProjectCanUserByCode(visitorMainParam.getProjectCode());
        if (resultContent.isFailed()) {
            return ResultContent.buildFail(resultContent.getMsg());
        }
        String projectOid = resultContent.getContent();
        //
        if (StringUtils.isEmpty(visitorMainParam.getTempUserId())) {
            return ResultContent.buildFail("tempUserId不能为空");
        }
        if (!TempUserUtil.idIsVisitorId(visitorMainParam.getTempUserId())) {
            return ResultContent.buildFail("tempUserId必须包含visitor字符");
        }

        VisitorMainParam param = new VisitorMainParam();
        BeanUtils.copyProperties(visitorMainParam, param);
        param.setProjectOid(projectOid);
        param.setSceneComponentId(sceneComponent.getId());

        ResultContent<VisitorMain> applyResult = visitorMainService.saveInfo(param);
        if (applyResult.isFailed()) {
            return ResultContent.buildFail(applyResult.getMsg());
        }

        // 自动通过
        VisitorReViewParam reViewParam = new VisitorReViewParam();
        reViewParam.setId(applyResult.getContent().getId());
        reViewParam.setReviewRemark("自动通过");
        ResultContent passResult = visitorMainService.passVisitApply(reViewParam);
        if (passResult.isFailed()) {
            throw new ApiException(passResult.getMsg());
        }
        return ResultContent.buildSuccess();
    }

    /**
     * 分页列表
     *
     * @param param
     * @return
     */
    public ResultContent<Page<VisitorMainModel>> page(VisitorMainOpenSearch param) {
        if (param.getState() != null) {
            if (param.getState() == VisitorState.Leave) {
                param.setState(null);
                param.setIsLeave(Boolean.TRUE);
                param.setIsOverTimeLeave(null);
            } else if (param.getState() == VisitorState.LeaveOutTime) {
                param.setState(null);
                param.setIsLeave(null);
                param.setIsOverTimeLeave(Boolean.TRUE);
            }
        }
        Pageable pageable = PageRequest.of(param.getNumber(), param.getSize());
        ResultContent<String> resultContent = commonService.checkProjectCanUserByCode(param.getProjectCode());
        if (resultContent.isFailed()) {
            return ResultContent.buildFail(resultContent.getMsg());
        }
        String projectOid = resultContent.getContent();

        VisitorMainSearch search = new VisitorMainSearch();
        BeanUtils.copyProperties(param, search);
        search.setProjectOid(projectOid);

        Page<VisitorMain> page = visitorMainDao.page(pageable, search);
        if (param.getIsDesen() != null && param.getIsDesen()) {
            return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, visitorMainService::toDesenModel));
        }
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, visitorMainService::toModel));
    }

    public ResultContent<VisitorMainModel> getVisitorDetail(IDParam param) {
        VisitorMain entity = visitorMainDao.findTopById(param.getId());
        if (entity == null) {
            return ResultContent.buildFail("数据不存在");
        }
        VisitorMainModel model = visitorMainService.toDesenModel(entity);
        return ResultContent.buildSuccess(model);
    }

    public ResultContent<VisitorMainModel> detailByBusId(BusIdParam param) {
        VisitorMain entity = visitorMainDao.findTopByBusId(param.getBusId());
        if (entity == null) {
            return ResultContent.buildFail("数据不存在");
        }
        VisitorMainModel model = visitorMainService.toDesenModel(entity);
        return ResultContent.buildSuccess(model);
    }

    public ResultContent deleteByBusId(BusIdParam param) {
        VisitorMain entity = visitorMainDao.findTopByBusId(param.getBusId());
        if (entity == null) {
            return ResultContent.buildFail("数据不存在");
        }
        return visitorMainService.deleteInfo(entity.getId());
    }

}
