package org.jeecg.modules.adweb.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.jeecg.modules.adweb.workflow.entity.ExecuteNode;
import org.jeecg.modules.adweb.workflow.mapper.ExecuteNodeMapper;
import org.jeecg.modules.adweb.workflow.service.IExecuteNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: adweb_execute_node @Author: jeecg-boot @Date: 2021-11-12 @Version: V1.0
 */
@Service
public class ExecuteNodeServiceImpl extends ServiceImpl<ExecuteNodeMapper, ExecuteNode>
    implements IExecuteNodeService {

  @Autowired private IAdwebSiteService adwebSiteService;

  @Autowired private ExecuteNodeMapper executeNodeMapper;

  /** 建站流程模板对应的hostCode */
  private final String SITE_BUILD_CODE = "SITE_BUILD_CODE";

  /** SEO优化流程模板对应的hostCode */
  private final String SEO_OPTIMIZE_CODE = "SEO_OPTIMIZE_CODE";

  private final String ENQUIRY_VERIFY_CODE = "ENQUIRY_VERIFY_CODE";

  private final Integer FLOW_IS_DELETE = 0;

  private final Integer FLOW_NOT_DELETE = 0;

  private final Integer Flow_TYPE_TEMPLATE = 1;

  private final Integer Flow_TYPE_PROCESS = 2;

  private final Integer Flow_NODE_TYPE_SITE_BUILD = 1;

  private final Integer Flow_NODE_TYPE_SEO_OPTIMIZE = 2;

  private final Integer Flow_NODE_TYPE_TEMPLATE = 3;

  private final Integer Flow_NODE_TYPE_ENQUIRY_VERIFY = 4;

  /** 判断站点是否具有建站流程 @Author: luxiaoxiao @Date: 2021/11/15 */
  @Override
  public boolean haveSiteBuildFlow(String siteCode) {
    QueryWrapper<AdwebSite> siteQueryWrapper = new QueryWrapper<>();
    siteQueryWrapper.eq("code", siteCode);
    //        siteQueryWrapper.eq("status", 1);
    List<AdwebSite> siteList = adwebSiteService.list(siteQueryWrapper);
    if (siteList.size() == 0) {
      return false;
    }
    Integer siteFlowStatus = siteList.get(0).getSiteFlowStatus();
    if (siteFlowStatus == null) {
      return false;
    }
    if (siteFlowStatus == 1 || siteFlowStatus == 0) {
      return true;
    }
    return false;
  }

  /** 获取建站流程 @Author: luxiaoxiao @Date: 2021/11/15 */
  @Override
  public List<ExecuteNode> getSiteBuildFlow(String siteCode) {
    if (!haveSiteBuildFlow(siteCode)) {
      return null;
    }

    QueryWrapper<ExecuteNode> nodeQueryWrapper = new QueryWrapper<>();
    nodeQueryWrapper.eq("type", Flow_TYPE_PROCESS);
    nodeQueryWrapper.eq("host_id", siteCode);
    nodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_SITE_BUILD);
    nodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    nodeQueryWrapper.orderByAsc("sort");
    return list(nodeQueryWrapper);
  }

  public List<ExecuteNode> getEnquiryVerifyFlow(String enquiryId) {
    QueryWrapper<ExecuteNode> nodeQueryWrapper = new QueryWrapper<>();
    nodeQueryWrapper.eq("type", Flow_TYPE_PROCESS);
    nodeQueryWrapper.eq("host_id", enquiryId);
    nodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_ENQUIRY_VERIFY);
    nodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    nodeQueryWrapper.orderByAsc("sort");
    return list(nodeQueryWrapper);
  }

  /** 获取建站模板流程 @Author: luxiaoxiao @Date: 2021/11/15 */
  @Override
  public List<ExecuteNode> getSiteBuildTemplateFlow() {
    QueryWrapper<ExecuteNode> nodeQueryWrapper = new QueryWrapper<>();
    nodeQueryWrapper.eq("type", Flow_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("host_id", SITE_BUILD_CODE);
    nodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    nodeQueryWrapper.orderByAsc("sort");
    return list(nodeQueryWrapper);
  }

  @Override
  public List<ExecuteNode> getEnquiryVerifyTemplateFlow() {
    QueryWrapper<ExecuteNode> nodeQueryWrapper = new QueryWrapper<>();
    nodeQueryWrapper.eq("type", Flow_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("host_id", ENQUIRY_VERIFY_CODE);
    nodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    nodeQueryWrapper.orderByAsc("sort");
    return list(nodeQueryWrapper);
  }

  @Override
  public Long getEnquiryVerifyTemplateFlowCount() {
    QueryWrapper<ExecuteNode> nodeQueryWrapper = new QueryWrapper<>();
    nodeQueryWrapper.eq("type", Flow_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("host_id", ENQUIRY_VERIFY_CODE);
    nodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    return count(nodeQueryWrapper);
  }

  /** 获取SEO优化模板流程 @Author: luxiaoxiao @Date: 2021/11/15 */
  @Override
  public List<ExecuteNode> getSEOTemplateFlow() {
    QueryWrapper<ExecuteNode> nodeQueryWrapper = new QueryWrapper<>();
    nodeQueryWrapper.eq("type", Flow_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("host_id", SEO_OPTIMIZE_CODE);
    nodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_TEMPLATE);
    nodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    nodeQueryWrapper.orderByAsc("sort");
    return list(nodeQueryWrapper);
  }

  /** 保存流程模板 @Author: luxiaoxiao @Date: 2021/11/15 */
  @Override
  public boolean saveTemplate(List<ExecuteNode> nodeList) {
    if (CollectionUtils.isEmpty(nodeList)) {
      return false;
    }

    int totalProportion = 0;
    for (ExecuteNode node : nodeList) {
      if (node.getProportion() != null) {
        totalProportion += node.getProportion();
      }
    }
    if (totalProportion != 100) {
      throw new JeecgBootException("权重相加不为100！");
    }

    ExecuteNode node = nodeList.get(0);
    String hostId = node.getHostId();
    if (StringUtils.isEmpty(hostId)) {
      throw new JeecgBootException("缺少hostId");
    }
    UpdateWrapper<ExecuteNode> updateWrapper = new UpdateWrapper<>();
    updateWrapper.eq("host_id", hostId);
    updateWrapper.eq("del_flag", FLOW_NOT_DELETE);
    updateWrapper.set("del_flag", 1);
    update(updateWrapper);

    Date now = new Date();
    for (int i = 0; i < nodeList.size(); i++) {
      ExecuteNode executeNode = nodeList.get(i);
      executeNode.setId(null);
      executeNode.setType(1);
      executeNode.setHostId(hostId);
      executeNode.setNodeType(3);
      executeNode.setStatus(null);
      executeNode.setFinTime(null);
      executeNode.setFinUid(null);
      executeNode.setDelFlag(0);
      executeNode.setCreateTime(now);
      executeNode.setSort(Double.parseDouble(i + ""));
    }
    return saveBatch(nodeList);
  }

  /** 获取未完成的SEO节点 */
  @Override
  public List<ExecuteNode> getSeoUnfinishedNode(String historyId) {
    QueryWrapper<ExecuteNode> executeNodeQueryWrapper = new QueryWrapper<>();
    executeNodeQueryWrapper.eq("history_id", historyId);
    executeNodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_SEO_OPTIMIZE);
    executeNodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    executeNodeQueryWrapper.eq("status", 0);
    executeNodeQueryWrapper.orderByAsc("sort");
    return this.list(executeNodeQueryWrapper);
  }

  /** 获取未完成的建站节点 */
  @Override
  public List<ExecuteNode> getSiteUnfinishedNode(String siteCode) {
    QueryWrapper<ExecuteNode> executeNodeQueryWrapper = new QueryWrapper<>();
    executeNodeQueryWrapper.eq("host_id", siteCode);
    executeNodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_SITE_BUILD);
    executeNodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    executeNodeQueryWrapper.eq("status", 0);
    executeNodeQueryWrapper.orderByAsc("sort");
    return list(executeNodeQueryWrapper);
  }

  @Override
  public List<ExecuteNode> getEnquiryVerifyUnfinishedNode(String enquiryId) {
    QueryWrapper<ExecuteNode> executeNodeQueryWrapper = new QueryWrapper<>();
    executeNodeQueryWrapper.eq("host_id", enquiryId);
    executeNodeQueryWrapper.eq("node_type", Flow_NODE_TYPE_ENQUIRY_VERIFY);
    executeNodeQueryWrapper.eq("del_flag", FLOW_NOT_DELETE);
    executeNodeQueryWrapper.eq("status", 0);
    executeNodeQueryWrapper.orderByAsc("sort");
    return list(executeNodeQueryWrapper);
  }

  /** 给站点创建建站流程 @Author: luxiaoxiao @Date: 2021/11/16 */
  @Override
  public boolean copySiteFlow(String siteCode) {
    executeNodeMapper.copySiteFlow(siteCode, SITE_BUILD_CODE);
    ExecuteNode node = getFirstUnCompleteNode(siteCode, Flow_NODE_TYPE_SITE_BUILD);
    if (node == null) {
      return false;
    }
    UpdateWrapper<AdwebSite> updateWrapper = new UpdateWrapper<>();
    updateWrapper.eq("code", siteCode);
    updateWrapper.set("site_flow_status", 0);
    updateWrapper.set("site_current_step", node.getName());
    return adwebSiteService.update(updateWrapper);
  }

  @Override
  public void copyEnquiryVerifyFlow(String enquiryId) {
    executeNodeMapper.copyEnquiryVerifyFlow(enquiryId, ENQUIRY_VERIFY_CODE);
    //    ExecuteNode node = getFirstUnCompleteNode(enquiryId, Flow_NODE_TYPE_ENQUIRY_VERIFY);
    //    return node != null;
  }

  private ExecuteNode getFirstUnCompleteNode(String host_id, Integer nodeType) {
    QueryWrapper<ExecuteNode> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("host_id", host_id);
    queryWrapper.eq("type", Flow_TYPE_PROCESS);
    queryWrapper.eq("node_type", nodeType);
    queryWrapper.eq("status", 0);
    queryWrapper.eq("del_flag", 0);
    queryWrapper.orderByAsc("sort");
    List<ExecuteNode> list = list(queryWrapper);
    if (CollectionUtils.isNotEmpty(list)) {
      return list.get(0);
    }
    return null;
  }
}
