package com.gci.boot.modules.basedata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gci.boot.common.vo.Result;
import com.gci.boot.constant.CommonConstant;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.basedata.mapper.TbVehicleMapper;
import com.gci.boot.modules.basedata.service.ITbVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-08-25
 */
@Service
public class TbVehicleServiceImpl extends ServiceImpl<TbVehicleMapper, TbVehicle> implements ITbVehicleService {

    @Override
    public Result<?> cancelValid(String id) {
        TbVehicle byId = getById(id);
        Result<Boolean> objectResult = new Result<>();
        if (StrUtil.isNotBlank(byId.getProjectId()) && !CommonConstant.INNER_PROJECT.equals(byId.getProjectId())) {
            objectResult.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            objectResult.setResult(false);
            objectResult.setMessage("当前车辆已有关联项目！请先解绑项目！");
        } else {
            objectResult.setCode(HttpStatus.OK.value());
            objectResult.setResult(true);
        }
        return objectResult;
    }

    @Override
    public void cancel(String id) {
        TbVehicle byId = getById(id);
        if (BeanUtil.isEmpty(byId)) {
            throw new GciBootException("车辆id错误！");
        } else {
            if (byId.getCancelFlag().equals(CommonConstant.DEL_FLAG_1)) {
                throw new GciBootException("当前车辆已经为作废状态！");
            }
        }
        update(new LambdaUpdateWrapper<TbVehicle>().eq(TbVehicle::getId,id).set(TbVehicle::getCancelFlag,CommonConstant.DEL_FLAG_1));
    }

    @Override
    public void unCancel(String id) {
        TbVehicle byId = getById(id);
        if (BeanUtil.isEmpty(byId)) {
            throw new GciBootException("车辆id错误！");
        } else {
            if (byId.getCancelFlag().equals(CommonConstant.DEL_FLAG_0)) {
                throw new GciBootException("当前车辆已经为恢复状态！");
            }
        }
        update(new LambdaUpdateWrapper<TbVehicle>().eq(TbVehicle::getId,id).set(TbVehicle::getCancelFlag,CommonConstant.DEL_FLAG_0));
    }
}
