package cn.axzo.workflow.core.service.impl;

import cn.axzo.workflow.common.model.dto.BpmnFormRelationCreateDTO;
import cn.axzo.workflow.common.model.dto.BpmnFormRelationSearchDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.core.repository.entity.ExtAxBpmnFormRelation;
import cn.axzo.workflow.core.repository.mapper.ExtAxBpmnFormRelationMapper;
import cn.axzo.workflow.core.service.ExtAxBpmnFormRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

import static cn.axzo.workflow.common.constant.BpmnConstants.NO_TENANT_ID;

/**
 * bpmn 模型与表单模型的关联关系
 *
 * @author wangli
 * @since 2024-11-04 15:39
 */
@Slf4j
@Service
public class ExtAxBpmnFormRelationServiceImpl implements ExtAxBpmnFormRelationService {
    @Resource
    private ExtAxBpmnFormRelationMapper bpmnFormRelationMapper;

    @Override
    public Long insert(BpmnFormRelationCreateDTO dto) {
        ExtAxBpmnFormRelation relation = new ExtAxBpmnFormRelation();
        relation.setKey(dto.getKey());
        relation.setBpmnDefinitionId(dto.getBpmnDefinitionId());
        relation.setFormDeploymentId(dto.getFormDeploymentId());
        relation.setTenantId(dto.getTenantId());
        bpmnFormRelationMapper.insert(relation);
        return relation.getId();
    }

    @Override
    public ExtAxBpmnFormRelation queryByBpmnDefinitionId(String bpmnDefinitionId) {
        BpmnFormRelationSearchDTO searchDTO = new BpmnFormRelationSearchDTO();
        searchDTO.setBpmnDefinitionId(bpmnDefinitionId);
        return bpmnFormRelationMapper.selectOne(buildQueryWrapper(searchDTO));
    }

    @Override
    public List<ExtAxBpmnFormRelation> keyQuery(BpmnFormRelationSearchDTO dto) {
        LambdaQueryWrapper<ExtAxBpmnFormRelation> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(dto.getTenantId())) {
            queryWrapper.eq(ExtAxBpmnFormRelation::getTenantId, dto.getTenantId()).or().eq(ExtAxBpmnFormRelation::getTenantId, NO_TENANT_ID);
        }

        return bpmnFormRelationMapper.selectList(queryWrapper);
    }

    @Override
    public List<ExtAxBpmnFormRelation> genericQuery(BpmnFormRelationSearchDTO dto) {
        return bpmnFormRelationMapper.selectList(buildQueryWrapper(dto));
    }

    public BpmPageResult<ExtAxBpmnFormRelation> genericPageQuery(BpmnFormRelationSearchDTO dto) {
        Page<ExtAxBpmnFormRelation> page = bpmnFormRelationMapper.selectPage(new Page<>(dto.getPageNo(), dto.getPageSize()), buildQueryWrapper(dto));
        return new BpmPageResult<>(page.getRecords(), page.getTotal());
    }

    LambdaQueryWrapper<ExtAxBpmnFormRelation> buildQueryWrapper(BpmnFormRelationSearchDTO dto) {
        return new LambdaQueryWrapper<ExtAxBpmnFormRelation>()
                .eq(StringUtils.hasText(dto.getKey()), ExtAxBpmnFormRelation::getKey, dto.getKey())
                .eq(StringUtils.hasText(dto.getBpmnDefinitionId()), ExtAxBpmnFormRelation::getBpmnDefinitionId, dto.getBpmnDefinitionId())
                .eq(StringUtils.hasText(dto.getFormDeploymentId()), ExtAxBpmnFormRelation::getFormDeploymentId, dto.getFormDeploymentId())
//                .eq(!StringUtils.hasText(dto.getTenantId()), ExtAxBpmnFormRelation::getTenantId, NO_TENANT_ID)
                .eq(StringUtils.hasText(dto.getTenantId()), ExtAxBpmnFormRelation::getTenantId, dto.getTenantId())
                .eq(ExtAxBpmnFormRelation::getIsDelete, 0)
                ;
    }
}
