package com.fjhx.service.flow;

import com.fjhx.constants.StatusConstant;
import com.fjhx.entity.apply.ApplyPurchaseDetails;
import com.fjhx.enums.FlowStatusEnum;
import com.fjhx.params.JumpVo;
import com.fjhx.params.apply.ApplyPurchaseVo;
import com.fjhx.service.apply.ApplyPurchaseDetailsService;
import com.fjhx.service.apply.ApplyPurchaseService;
import com.fjhx.service.contract.ContractService;
import com.fjhx.utils.ExampleAbstract;
import com.fjhx.utils.FlowConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 发起申购
 */
@Service
public class InitiateApplyFlowService {

    @Autowired
    private ApplyPurchaseService applyPurchaseService;

    @Autowired
    private ApplyPurchaseDetailsService applyPurchaseDetailsService;

    @Autowired
    private ContractService contractService;

    private final FlowConstructor flowConstructor = FlowConstructor.init(new ExampleAbstract() {

        @Override
        public String getCode() {
            return "js_initiateApply";
        }

        @Override
        public void end() {
            ApplyPurchaseVo applyVo = getCacheData(ApplyPurchaseVo.class);
            applyVo.setFlowStatus(FlowStatusEnum.ADOPT.getType());
            applyPurchaseService.updateById(applyVo);

            // 生成合同
            contractService.create(applyVo);
        }

        @Override
        public void adopt() {
            ApplyPurchaseVo applyVo = getCacheData(ApplyPurchaseVo.class);
            applyVo.setFlowStatus(FlowStatusEnum.UNDER_WAY.getType());
            applyPurchaseService.updateById(applyVo);
        }

        @Override
        public void reject() {
            ApplyPurchaseVo applyVo = getCacheData(ApplyPurchaseVo.class);
            applyVo.setFlowStatus(FlowStatusEnum.REJECT.getType());
            applyPurchaseService.updateById(applyVo);
        }

    });


    @Transactional(rollbackFor = Exception.class)
    public void create(ApplyPurchaseVo applyPurchaseVo) {

        String code = applyPurchaseService.getCode();

        // 添加申购
        applyPurchaseVo.setFlowStatus(FlowStatusEnum.WAIT.getType());
        applyPurchaseVo.setSafetyStockCreate(StatusConstant.NO);
        applyPurchaseVo.setCode(code);
        applyPurchaseService.save(applyPurchaseVo);

        Long id = applyPurchaseVo.getId();

        // 添加申购明细
        List<ApplyPurchaseDetails> applyDetailsList = applyPurchaseVo.getApplyDetailsList();
        applyDetailsList.forEach(item -> {
            item.setApplyPurchaseId(id);
            item.setApplyPurchaseCode(code);
        });
        applyPurchaseDetailsService.saveBatch(applyDetailsList);

        flowConstructor.create(id, "发起申购", applyPurchaseVo.getRemark(), applyPurchaseVo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void jump(JumpVo jumpVo) {
        flowConstructor.jump(jumpVo);
    }

}
