package com.fjhx.purchase.flow;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.flow.core.FlowThreadLocalUtil;
import com.fjhx.purchase.entity.subscribe.enums.SubscribeStatusEnum;
import com.fjhx.purchase.entity.subscribe.po.Subscribe;
import com.fjhx.purchase.entity.subscribe.po.SubscribeDetail;
import com.fjhx.purchase.service.subscribe.SubscribeDetailService;
import com.fjhx.purchase.service.subscribe.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author:yzc
 * @DATE:2023/4/20 19:16
 */
@DS(SourceConstant.PURCHASE)
@Component
public class SubscribeFlowByWdly extends FlowDelegate {

    @Autowired
    private CodingRuleService codingRuleService;

    @Autowired
    private SubscribeFlow subscribeFlow;
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private SubscribeDetailService subscribeDetailService;

    @Override
    public String getFlowKey() {
        return "wdly_apply_purchase";
    }


    /**
     * 发起流程
     *
     * @param flowId     流程ID
     * @param submitData 申购数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        Subscribe subscribe = submitData.toJavaObject(Subscribe.class);
//        subscribe.setCode(CodeEnum.SUBSCRIBE.getCode());
        subscribe.setCode(codingRuleService.createCode(CodingRuleEnum.SUBSCRIBE.getKey(), null));
        subscribe.setProcessInstanceId("wdly_apply_purchase");
        subscribe.setFlowId(flowId);
        subscribe.setSubcribeStatus(SubscribeStatusEnum.UNDER_REVIEW.getKey());
        subscribeService.save(subscribe);
        List<SubscribeDetail> SubscribeDetails = subscribe.getSubscribeDetailList();
        if (CollectionUtils.isNotEmpty(SubscribeDetails)) {
            for (SubscribeDetail s : SubscribeDetails) {
                s.setSubscribeId(subscribe.getId());
            }
            subscribeDetailService.saveBatch(SubscribeDetails);
        }
        return subscribe.getId();
    }

    /**
     * 结束流程
     *
     * @param flowId     流程ID
     * @param businessId 业务ID
     * @param submitData 数据
     */
    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        subscribeFlow.end(flowId, businessId, submitData);
    }

    /**
     * 重新发起编辑方法
     */
    public void reStart() {
        //获取提交的数据
        JSONObject submitData = FlowThreadLocalUtil.getCurrentData();
        //编辑申购信息
        Subscribe subscribe = submitData.toJavaObject(Subscribe.class);
        subscribe.setSubcribeStatus(SubscribeStatusEnum.UNDER_REVIEW.getKey());
        subscribeService.updateById(subscribe);
        //编辑申购明细
        List<SubscribeDetail> subscribeDetails = subscribe.getSubscribeDetailList();
        if (CollectionUtils.isNotEmpty(subscribeDetails)) {
            //更新明细列表信息
            subscribeDetails.forEach(item -> item.setSubscribeId(subscribe.getId()));
            subscribeDetailService.editLinked(subscribeDetails, SubscribeDetail::getSubscribeId, subscribe.getId());
        }
    }
}
