package com.njzscloud.dispose.cst.truck.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.dispose.cst.truck.mapper.TruckMapper;
import com.njzscloud.dispose.cst.truck.pojo.entity.TruckEntity;
import com.njzscloud.dispose.cst.truck.service.TruckService;
import com.njzscloud.dispose.sys.auth.pojo.result.MyResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆信息
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TruckServiceImpl extends ServiceImpl<TruckMapper, TruckEntity> implements TruckService {

    @Override
    public void add(TruckEntity truckEntity) {
        MyResult userDetail = SecurityUtil.loginUser();
        Long customerId = userDetail.getCurrentCustomerId();
        truckEntity.setCustomerId(customerId);
        truckEntity.setOrgId(userDetail.getCurrentOrgId());
        truckEntity.setTareWeight(0);
        this.save(truckEntity);
    }

    @Override
    public void modify(TruckEntity truckEntity) {
        if (!SecurityUtil.isAdmin()) {
            TruckEntity old = this.getById(truckEntity.getId());
            MyResult userDetail = SecurityUtil.loginUser();
            Long customerId = userDetail.getCurrentCustomerId();
            if (customerId.equals(old.getCustomerId())) {
                // 传参customerId与旧数据customerId一致，说明是修改自己名下的车辆，允许修改
                this.updateById(truckEntity);
            } else {
                throw Exceptions.clierr("无修改权限");
            }
        } else {
            this.updateById(truckEntity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids, Boolean manager) {
        if (!SecurityUtil.isAdmin()) {
            MyResult userDetail = SecurityUtil.loginUser();
            Long customerId = userDetail.getCurrentCustomerId();
            // id和customerId必传
            if (manager == null) {
                throw Exceptions.clierr("manager必传");
            }
            // 只能删除自己名下车辆
            List<TruckEntity> trucks = this.listByIds(ids);
            if (!trucks.stream().allMatch(truck -> truck.getCustomerId().equals(customerId))) {
                throw Exceptions.clierr("无删除权限");
            }
            if (!manager) {
                // 非管理员直接删除即可
                this.removeBatchByIds(ids);
            } else {
                // 管理员则是把orgId置空，解绑
                this.update(Wrappers.<TruckEntity>lambdaUpdate().set(TruckEntity::getOrgId, null).in(TruckEntity::getId, ids));
            }
        } else {
            this.removeBatchByIds(ids);
        }
    }

    @Override
    public TruckEntity detail(Long id) {
        return this.getById(id);
    }

    @Override
    public PageResult<TruckEntity> paging(PageParam pageParam, TruckEntity truckEntity) {
        String licensePlate = truckEntity.getLicensePlate();
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<TruckEntity>lambdaQuery()
            .like(StrUtil.isNotBlank(licensePlate), TruckEntity::getLicensePlate, licensePlate)
        ));
    }
}


