package com.huigou.topsun.quotation.application.impl;

import com.huigou.data.query.model.QueryDescriptor;
import com.huigou.topsun.quotation.application.QuotationItemApplication;
import com.huigou.topsun.quotation.domain.model.Quotation;
import com.huigou.topsun.quotation.application.QuotationApplication;
import com.huigou.topsun.quotation.domain.model.QuotationItem;
import com.huigou.topsun.quotation.domain.query.QuotationQueryRequest;
import com.huigou.topsun.quotation.repository.QuotationRepository;
import com.huigou.uasp.bmp.common.BizBillStatus;
import com.huigou.uasp.bpm.FlowBroker;
import com.huigou.util.ClassHelper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* @author 16508
* @description 针对表【quotation(报价单)】的数据库操作Service实现
* @createDate 2023-11-28 13:59:10
*/
@Service("quotationApplication")
public class QuotationApplicationImpl extends FlowBroker implements QuotationApplication {
    @Resource
    private QuotationRepository quotationRepository;
    @Resource
    private QuotationItemApplication quotationItemApplication;

    @Override
    protected String saveBizAndApprovalData() {
        super.saveBizAndApprovalData();
        Quotation quotation = getBizEntity(Quotation.class);
        if (quotation.isNew()) {
            quotation.setStatusId(BizBillStatus.APPLYING.getId());
        } else {
            quotation = (Quotation) commonDomainService.loadAndFillinProperties(quotation);
        }
        quotation = this.saveQuotation(quotation);
        List<QuotationItem> quotationItems = this.getBizEntities(QuotationItem.class, "quotationItems");

        quotationItemApplication.saveQuotationItems(quotation.getId(),quotationItems);
        return quotation.getId();
    }



    @Override
    protected Map<String, Object> getProcessBizParams(String bizId) {
        // 返回业务数据给流程实例，
        return ClassHelper.toMap(quotationRepository.getOne(bizId));
    }

    @Override
    protected void onEnd(DelegateExecution delegateExecution) {
        super.onEnd(delegateExecution);
        String bizId = delegateExecution.getProcessBusinessKey();
        BizBillStatus status = approvePassed() ? BizBillStatus.COMPLETED : BizBillStatus.ABORTED;
        Quotation quotation = quotationRepository.findOne(bizId);
        quotation.setStatusId(status.getId());
        quotationRepository.save(quotation);
    }

    @Override
    protected void onAbortProcessInstance(DelegateExecution delegateExecution) {
        super.onAbortProcessInstance(delegateExecution);
        String bizId = delegateExecution.getProcessBusinessKey();
        Quotation quotation = quotationRepository.findOne(bizId);
        quotation.setStatusId(BizBillStatus.ABORTED.getId());
        quotationRepository.save(quotation);
    }

    /**
     * @param delegateTask
     * @param destActivityId 回退到的目标节点id
     */
    @Override
    protected void onBack(DelegateTask delegateTask, String destActivityId) {
        super.onBack(delegateTask, destActivityId);
        if ("Apply".equalsIgnoreCase(destActivityId)) {
            String bizId = delegateTask.getExecution().getProcessBusinessKey();
            Quotation quotation = quotationRepository.findOne(bizId);
            quotation.setStatusId(BizBillStatus.APPLYING.getId());
            quotationRepository.save(quotation);
        }
    }

    @Override
    public Quotation findById(String id) {
        return quotationRepository.findOne(id);
    }

    @Override
    public Quotation saveQuotation(Quotation quotation) {
        quotation = quotationRepository.save(quotation);
        quotation.setQuotationId(quotation.getId());
        quotationRepository.save(quotation);
        return quotation;
    }

    @Override
    public Map<String, Object> slicedQuotationList(QuotationQueryRequest queryRequest) {
        QueryDescriptor queryDescriptor = this.sqlExecutorDao.getQuery(QUERY_XML_FILE_PATH, "slicedQuotationList");
        return this.sqlExecutorDao.executeSlicedQuery(queryDescriptor,queryRequest);
    }
}




