package com.yh.saas.platform.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.winsea.svc.base.base.entity.CommonParameterCompany;
import com.winsea.svc.base.base.service.ICommonParameterCompanyService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.winsea.svc.base.workflow.entity.Workflow;
import com.winsea.svc.base.workflow.entity.WorkflowStep;
import com.winsea.svc.base.workflow.entity.WorkflowStepCondition;
import com.yh.saas.platform.service.CommonParameterCompanyService;
import com.yh.saas.toolkit.workflow.service.IWorkflowService;
import com.yh.saas.toolkit.workflow.service.IWorkflowStepConditionService;
import com.yh.saas.toolkit.workflow.service.IWorkflowStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公司常量 服务实现类
 * </p>
 *
 * @author Wangxiangshun
 * @since 2021-02-19
 */
@Service
public class CommonParameterCompanyServiceImpl implements CommonParameterCompanyService {

    @SofaReference
    private ICommonParameterCompanyService commonParameterCompanyService;
    @Autowired
    private IWorkflowService workflowService;
    @Autowired
    private IWorkflowStepService stepService;
    @Autowired
    private IWorkflowStepConditionService stepConditionService;

    /**
     * 查询基础数据，是否能删除标识
     * @param constCode 变量code
     * @param businessCode 工作流code
     * @return
     */
    @Override
    public List<CommonParameterCompany> selectListParam(String constCode,String businessCode) {
        Wrapper<CommonParameterCompany> wr = new EntityWrapper<>();
        // 根据公司，code，顺序号查询
        List<CommonParameterCompany> parameterCompanyList = commonParameterCompanyService.selectList(wr);

        if (CollectionUtils.isNotEmpty(parameterCompanyList)) {
            // 查询物料申请单审核流条件中用到的key
            List<String> conditionKey = getMatApplWorkflowStepConditionVal(businessCode);
            // 设置当前类型能否删除
            for (CommonParameterCompany commonParameterCompany : parameterCompanyList) {
                // 在审核流中用到的也不能删
                if (CollectionUtils.isNotEmpty(conditionKey) && conditionKey.contains(commonParameterCompany.getParameterKey())) {
                    commonParameterCompany.setConstCodes("1");
                }
            }
        }
        return parameterCompanyList;
    }

    /**
     * 查询审核流条件中用到的key
     *
     * @return
     */
    private List<String> getMatApplWorkflowStepConditionVal(String businessCode) {
        List<String> conditionKey = new ArrayList<>();
        // 查询审核流信息
        List<Workflow> workflowDefinitions1 = workflowService.getWorkflowDefinitions(businessCode,
                AuthSecurityUtils.getCurrentUserInfo().getCompId());

        if (CollectionUtils.isEmpty(workflowDefinitions1)) {
            return conditionKey;
        }
        // 查审核流步骤
        List<WorkflowStep> detailByWorkflowId = stepService.getDetailByWorkflowId(workflowDefinitions1.get(0).getId());
        if (CollectionUtils.isNotEmpty(detailByWorkflowId)) {
            // 查出所有步骤对应的条件
            List<WorkflowStepCondition> byStepIdList = stepConditionService.findByStepIdList(detailByWorkflowId.stream()
                    .map(WorkflowStep::getId).collect(Collectors.toList()));
            // 取出条件值
            if (CollectionUtils.isNotEmpty(byStepIdList)) {
                conditionKey = byStepIdList.stream().map(WorkflowStepCondition::getVarValue).collect(Collectors.toList());
            }
        }
        return conditionKey;
    }



}
