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.google.gson.reflect.TypeToken;
import com.qmth.sop.business.bean.result.FlowFormWidgetResult;
import com.qmth.sop.business.bean.result.FlowTablePropResult;
import com.qmth.sop.business.bean.result.FlowTdFormWidgetResult;
import com.qmth.sop.business.cache.CommonCacheService;
import com.qmth.sop.business.entity.TDFormWidget;
import com.qmth.sop.business.entity.TDTableProp;
import com.qmth.sop.business.mapper.TDFormWidgetMapper;
import com.qmth.sop.business.service.TDFormWidgetService;
import com.qmth.sop.common.enums.TFCustomTypeEnum;
import com.qmth.sop.common.enums.WidgetCodeEnum;
import com.qmth.sop.common.enums.WidgetTypeEnum;
import com.qmth.sop.common.util.GsonUtil;
import com.qmth.sop.common.util.JacksonUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 动态表单控件表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-07-17
 */
@Service
public class TDFormWidgetServiceImpl extends ServiceImpl<TDFormWidgetMapper, TDFormWidget> implements TDFormWidgetService {

    @Resource
    CommonCacheService commonCacheService;

    /**
     * 流程控件查询
     *
     * @param flowType
     * @param formSetup
     * @return
     */
    @Override
    public List<FlowFormWidgetResult> flowWidgetQuery(TFCustomTypeEnum flowType, Integer formSetup) {
        QueryWrapper<TDFormWidget> tdFormWidgetQueryWrapper = new QueryWrapper<>();
        tdFormWidgetQueryWrapper.lambda().eq(TDFormWidget::getFlowType, flowType).eq(TDFormWidget::getType, WidgetTypeEnum.FORM).orderByAsc(TDFormWidget::getCreateTime);
        if (Objects.nonNull(formSetup)) {
            tdFormWidgetQueryWrapper.lambda().eq(TDFormWidget::getFormSetup, formSetup);
        }
        List<TDFormWidget> tdFormWidgetList = this.list(tdFormWidgetQueryWrapper);
        List<FlowFormWidgetResult> flowFormWidgetResultList = null;
        if (!CollectionUtils.isEmpty(tdFormWidgetList)) {
            flowFormWidgetResultList = GsonUtil.fromJson(JacksonUtil.parseJson(tdFormWidgetList), new TypeToken<List<FlowFormWidgetResult>>() {
            }.getType());
            Set<FlowFormWidgetResult> flowFormWidgetResultSet = flowFormWidgetResultList.stream().filter(s -> s.getCode() == WidgetCodeEnum.TABLE).collect(Collectors.toSet());
            if (!CollectionUtils.isEmpty(flowFormWidgetResultSet)) {
                for (FlowFormWidgetResult f : flowFormWidgetResultSet) {
                    List<TDTableProp> tdTablePropList = commonCacheService.tablePropCache(f.getId());
                    if (!CollectionUtils.isEmpty(tdTablePropList)) {
                        List<FlowTablePropResult> flowTablePropResultList = GsonUtil.fromJson(JacksonUtil.parseJson(tdTablePropList), new TypeToken<List<FlowTablePropResult>>() {
                        }.getType());
                        for (FlowTablePropResult tf : flowTablePropResultList) {
                            TDFormWidget tdFormWidget = commonCacheService.formWidgetCache(tf.getEditWidgetId());
                            FlowTdFormWidgetResult flowTdFormWidgetResult = GsonUtil.fromJson(GsonUtil.toJson(tdFormWidget), FlowTdFormWidgetResult.class);
                            tf.setTdFormWidget(flowTdFormWidgetResult);
                        }
                        f.setTablePropList(flowTablePropResultList);
                    }
                }
            }
        }
        return flowFormWidgetResultList;
    }
}
