package com.fjhx.purchase.flow;

import cn.hutool.extra.spring.SpringUtil;
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.baomidou.mybatisplus.core.toolkit.Wrappers;
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.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.fjhx.purchase.util.code.CodeEnum;
import com.obs.services.internal.ServiceException;
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;

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


    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 申购数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        SubscribeService subscribeService = SpringUtil.getBean(SubscribeService.class);
        SubscribeDetailService subscribeDetailService = SpringUtil.getBean(SubscribeDetailService.class);
        Subscribe subscribe = submitData.toJavaObject(Subscribe.class);
//        subscribe.setCode(CodeEnum.SUBSCRIBE.getCode());
        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)){
            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) {
        SubscribeService subscribeService = SpringUtil.getBean(SubscribeService.class);
        SubscribeDetailService subscribeDetailService = SpringUtil.getBean(SubscribeDetailService.class);
        //通过业务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);
        //修改申购明细为待采购
        SubscribeDetail detail = new SubscribeDetail();
        detail.setStatus(SubscribeDetailStatusEnum.PAID_AMOUNT.getKey());
        subscribeDetailService.update(detail,Wrappers.<SubscribeDetail>query()
                .lambda().eq(SubscribeDetail::getSubscribeId,subscribe.getId()));
    }
}
