package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.sop.business.bean.result.FormWidgetMetadataResult;
import com.qmth.sop.business.entity.TDFormWidgetMetadata;
import com.qmth.sop.business.entity.TFCustomFlow;
import com.qmth.sop.business.mapper.TDFormWidgetMetadataMapper;
import com.qmth.sop.business.service.TDFormWidgetMetadataService;
import com.qmth.sop.business.service.TFCustomFlowService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import com.qmth.sop.common.enums.TFCustomTypeEnum;
import com.qmth.sop.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * <p>
 * 动态表单元数据表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-08-29
 */
@Service
public class TDFormWidgetMetadataServiceImpl extends ServiceImpl<TDFormWidgetMetadataMapper, TDFormWidgetMetadata> implements TDFormWidgetMetadataService {
    private final static Logger log = LoggerFactory.getLogger(TDFormWidgetMetadataServiceImpl.class);

    @Resource
    TDFormWidgetMetadataService tdFormWidgetMetadataService;

    @Resource
    TFCustomFlowService tfCustomFlowService;

    /**
     * 根据流程类型、版本号查询元数据
     *
     * @param type
     * @param version
     * @param tdFormWidgetMetadataSet
     * @return
     */
    @Override
    @Transactional
    public Boolean selectMetadataExist(TFCustomTypeEnum type, Integer version, Set<TDFormWidgetMetadata> tdFormWidgetMetadataSet) {
        try {
            int count = tdFormWidgetMetadataService.count(new QueryWrapper<TDFormWidgetMetadata>().lambda().eq(TDFormWidgetMetadata::getType, type).eq(TDFormWidgetMetadata::getVersion, version));
            if (count == 0) {
                tdFormWidgetMetadataService.saveBatch(tdFormWidgetMetadataSet);
            } else if (!CollectionUtils.isEmpty(tdFormWidgetMetadataSet) && count != tdFormWidgetMetadataSet.size()) {
                tdFormWidgetMetadataService.remove(new QueryWrapper<TDFormWidgetMetadata>().lambda().eq(TDFormWidgetMetadata::getType, type).eq(TDFormWidgetMetadata::getVersion, version));
                tdFormWidgetMetadataService.saveBatch(tdFormWidgetMetadataSet);
            }
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        }
        return true;
//        return this.baseMapper.deleteAllData(Objects.nonNull(type) ? type.name() : null);
    }

    /**
     * 查询所有sop元数据
     *
     * @param type
     * @return
     */
    @Override
    public List<FormWidgetMetadataResult> selectAll(TFCustomTypeEnum type) {
        if (type != TFCustomTypeEnum.OFFICE_SOP_FLOW && type != TFCustomTypeEnum.CLOUD_MARK_SOP_FLOW) {
            throw ExceptionResultEnum.ERROR.exception("流程类型只能为教务处或研究生");
        }
        TFCustomFlow maxTfCustomFlow = tfCustomFlowService.findMaxVersion(null, null, type);
        Optional.ofNullable(maxTfCustomFlow).orElseThrow(() -> ExceptionResultEnum.FLOW_CUSTOM_NO_DATA.exception());
        return this.baseMapper.getFormWidgetMetadataResult(type.name(), maxTfCustomFlow.getVersion());
    }

    /**
     * 保存sop元数据
     *
     * @param tdFormWidgetMetadataList
     * @return
     */
    @Override
    @Transactional
    public Boolean save(List<TDFormWidgetMetadata> tdFormWidgetMetadataList) {
        return this.saveOrUpdateBatch(tdFormWidgetMetadataList);
    }
}
