package com.sundot.airport.item.service;

import com.sundot.airport.item.domain.ItemSeizureRecord;
import com.sundot.airport.system.domain.approval.ApprovalInstance;
import com.sundot.airport.system.enums.ProcessStatusEnum;
import com.sundot.airport.system.service.approval.BusinessApprovalHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class ItemSeizureRecordApprovalHandler implements BusinessApprovalHandler {

    private static final Logger log = LoggerFactory.getLogger(ItemSeizureRecordApprovalHandler.class);

    @Autowired
    private IItemSeizureRecordService itemSeizureRecordService;

    @Override
    public boolean supports(String businessType) {

        return "SEIZURE_REPORT".equals(businessType);
    }

    @Override
    public void handleApproved(ApprovalInstance instance) {
        log.info("查获上报审批完成 {}", instance);
        if ("COMPLETED".equals(instance.getStatus())) {
            itemSeizureRecordService.lambdaUpdate().set(ItemSeizureRecord::getProcessStatus, ProcessStatusEnum.ARCHIVED.getCode()).eq(ItemSeizureRecord::getId, instance.getBusinessId()).update();
        } else {
            itemSeizureRecordService.lambdaUpdate().set(ItemSeizureRecord::getProcessStatus, ProcessStatusEnum.AUDITING.getCode()).eq(ItemSeizureRecord::getId, instance.getBusinessId()).update();
        }

    }

    @Override
    public void handleRejected(ApprovalInstance instance) {
        log.info("查获上报审批驳回 {}", instance);
    }

    @Override
    public void handleCancelled(ApprovalInstance instance) {
        log.info("查获上报审批取消{}", instance);
        itemSeizureRecordService.lambdaUpdate().set(ItemSeizureRecord::getProcessStatus, ProcessStatusEnum.TERMINATED.getCode()).eq(ItemSeizureRecord::getId, instance.getBusinessId()).update();
    }
}
