package org.springblade.flow.activity.service.impl;

import cn.hutool.core.convert.Convert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.enums.AppConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.flow.activity.entity.ActivityEnum;
import org.springblade.flow.activity.entity.ProcessActivity;
import org.springblade.flow.activity.service.IAuditActivityService;
import org.springblade.flow.business.constant.AuditCandidate;
import org.springblade.flow.business.service.IFlowService;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.flow.core.utils.FlowUtil;
import org.springblade.flow.core.utils.TaskUtil;
import org.springblade.ldt.activity.entity.Activity;
import org.springblade.ldt.activity.service.IActivityService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description:
 * @since: 9/1/21 -- 3:29 PM
 */
@Service
@AllArgsConstructor
@Slf4j
public class AuditActivityService implements IAuditActivityService {

	private final IFlowService flowService;
	private final IActivityService activityService;
	private final IShopService shopService;


	/**
	 * 发起活动审核流程
	 * */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean startProcess(ProcessActivity processActivity) {
		Activity activity = processActivity.getActivity();
		String businessTable = FlowUtil.getBusinessTable(ProcessConstant.AUDIT_ACTIVITY);

		if (Func.isEmpty(activity.getId())) {

			//是否由商场发起
			boolean isMall = Objects.equals(activity.getLaunchType(), AppConstant.PLATFORM.MALL.name());
			boolean toPlatform = true;
			if(!isMall){
				Shop shop = shopService.getById(activity.getLaunchId());
				toPlatform = Objects.isNull(shop.getMallId());
			}
			activity.setAuditStatus(toPlatform? ActivityEnum.WAITING_PLATFORM.name():ActivityEnum.WAITING_MALL.name());
			// 保存活动
			activityService.save(activity);

			// 启动流程
			Kv variables = Kv.create()
				.set("businessId",activity.getId())
				.set("auditor", AuditCandidate.LDT_PLATFORM.name())
					.set("mallTask", TaskUtil.getTaskUserForApp(Convert.toStr(activity.getMallId()),TaskUtil.APP_MALL))
				.set("toPlatform", toPlatform)
				;

			BladeFlow flow = flowService.startProcessInstanceById(
				activity.getProcessDefinitionId(),
				FlowUtil.getBusinessKey(businessTable,
					String.valueOf(activity.getId())), variables,
				getStartUser(isMall,activity));

			if (Func.isNotEmpty(flow)) {
				log.debug("流程已启动,流程ID:" + flow.getProcessInstanceId());
				// 返回流程id写入leave
				activity.setProcessInstanceId(flow.getProcessInstanceId());
				activityService.updateById(activity);
			} else {
				throw new ServiceException("开启流程失败");
			}
		} else {
			activityService.updateById(activity);
		}
		return true;
	}


	private String getStartUser(boolean isMall, Activity activity) {
		if(isMall){
			return TaskUtil.getTaskUserForApp(Convert.toStr(activity.getLaunchId()),TaskUtil.APP_MALL);
		}else{
			 return TaskUtil.getTaskUserForApp(Convert.toStr(activity.getLaunchId()),TaskUtil.APP_SHOP);
		}
	}

}
