package com.zcloud.primeport.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zcloud.primeport.domain.gateway.VehicleApplyGateway;
import com.zcloud.primeport.domain.gateway.VehicleAuditGateway;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.persistence.dataobject.VehicleApplyDO;
import com.zcloud.primeport.persistence.dataobject.VehicleAuditDO;
import com.zcloud.primeport.persistence.repository.VehicleApplyRepository;
import com.zcloud.primeport.persistence.repository.VehicleAuditRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * web-infrastructure
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:23
 */
@Service
@AllArgsConstructor
public class VehicleApplyGatewayImpl implements VehicleApplyGateway {
    private final VehicleApplyRepository vehicleApplyRepository;
    private final VehicleAuditRepository VehicleAuditRepository;

    @Override
    public VehicleApplyE add(VehicleApplyE vehicleApplyE) {
        VehicleApplyDO d = new VehicleApplyDO();
        BeanUtils.copyProperties(vehicleApplyE, d);
        vehicleApplyRepository.save(d);
        vehicleApplyE.setId(d.getId());
        return vehicleApplyE;
    }

    @Override
    public Boolean update(VehicleApplyE vehicleApplyE) {
        VehicleApplyDO d = new VehicleApplyDO();
        BeanUtils.copyProperties(vehicleApplyE, d);
        vehicleApplyRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedVehicleApplyById(Long id) {
        return vehicleApplyRepository.removeById(id);
    }

    @Override
    public Boolean deletedVehicleApplyByIds(Long[] ids) {
        return vehicleApplyRepository.removeByIds(Collections.singletonList(ids));
    }

    @Override
    public VehicleApplyE getById(Long id) {
        VehicleApplyDO vehicleApplyDO = vehicleApplyRepository.getById(id);
        if (vehicleApplyDO != null) {
            VehicleApplyE vehicleApplyE = new VehicleApplyE();
            BeanUtils.copyProperties(vehicleApplyDO, vehicleApplyE);
            // 上一步的审批人
            QueryWrapper<VehicleAuditDO> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("vehicle_apply_id",id);
            objectQueryWrapper.orderByDesc("audit_time");
            List<VehicleAuditDO> list = VehicleAuditRepository.list(objectQueryWrapper);
            if (!list.isEmpty()) {
                VehicleAuditDO vehicleAuditDO = list.get(0);
                vehicleApplyE.setAuditCorpId(vehicleAuditDO.getAuditCorpId());
                vehicleApplyE.setAuditCorpName(vehicleAuditDO.getAuditCorpName());
                vehicleApplyE.setAuditDeptId(vehicleAuditDO.getAuditDeptId());
                vehicleApplyE.setAuditDeptName(vehicleAuditDO.getAuditDeptName());
                vehicleApplyE.setAuditUserId(vehicleAuditDO.getAuditUserId());
                vehicleApplyE.setAuditUserName(vehicleAuditDO.getAuditUserName());
            }
            return vehicleApplyE;
        }
        return null;
    }

    @Override
    public void updateEvehicleBlockedByLicenceNo(String licenceNo, Integer blockedFlag) {
        UpdateWrapper<VehicleApplyDO> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.set("blocked_flag", blockedFlag);
        objectUpdateWrapper.eq("licence_no", licenceNo);
        vehicleApplyRepository.update(objectUpdateWrapper);
    }

    @Override
    public void updateBlackFlag(VehicleApplyE vehicleApplyE) {
        UpdateWrapper<VehicleApplyDO> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.set("blocked_flag", vehicleApplyE.getBlockedFlag());
        objectUpdateWrapper.eq("licence_no", vehicleApplyE.getLicenceNo());
        vehicleApplyRepository.update(objectUpdateWrapper);
    }
}

