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.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.enums.FlowStatusEnum;
import com.fjhx.purchase.entity.subscribe.enums.SubscribeDetailStatusEnum;
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 com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author:caozj
 * @DATE:2023/4/3 17:38
 */
@DS(SourceConstant.PURCHASE)
@Component
public class SubscribeFlow extends FlowDelegate {

    @Autowired
    private CodingRuleService codingRuleService;

    @Autowired
    private SubscribeService subscribeService;

    @Autowired
    private SubscribeDetailService subscribeDetailService;

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

    /**
     * 发起流程
     *
     * @param flowId     流程ID
     * @param submitData 申购数据
     * @return 业务id
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {

        Subscribe subscribe = submitData.toJavaObject(Subscribe.class);
        subscribe.setCode(codingRuleService.createCode(CodingRuleEnum.SUBSCRIBE.getKey(),null));
        subscribe.setSubcribeStatus(SubscribeStatusEnum.UNDER_REVIEW.getKey());
        subscribe.setFlowId(flowId);
        subscribeService.save(subscribe);

        List<SubscribeDetail> SubscribeDetails = subscribe.getSubscribeDetailList();
        if(CollectionUtils.isNotEmpty(SubscribeDetails)){
            SubscribeDetails.forEach(item -> item.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) {

        // 通过业务ID查询申购数据
        Subscribe subscribe = subscribeService.getById(businessId);
        if (ObjectUtils.isEmpty(subscribe)) {
            throw new ServiceException("申购单不存在");
        }

        // 修改申购状态为审批通过
        subscribe.setSubcribeStatus(SubscribeStatusEnum.PASS.getKey());
        subscribe.setApprovedDate(new Date());
        subscribeService.updateById(subscribe);

        // 修改申购明细为待采购
        subscribeDetailService.update(q -> q
                .eq(SubscribeDetail::getSubscribeId, subscribe.getId())
                .set(SubscribeDetail::getStatus, SubscribeDetailStatusEnum.PAID_AMOUNT.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
        );

    }

}
