ZhongliVehicleService.java 3.84 KB
package cn.com.glsx.stdinterface.modules.service;

import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.entity.VehiclePermit;
import cn.com.glsx.stdinterface.modules.entity.ZhongliVehicle;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehiclePermitMapper;
import cn.com.glsx.stdinterface.modules.mapper.ZhongliAccountMapper;
import cn.com.glsx.stdinterface.modules.mapper.ZhongliVehicleMapper;
import cn.com.glsx.stdinterface.modules.model.ZhongliAccountModel;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ZhongliVehicleService {

    @Resource
    private ZhongliAccountMapper zhongliAccountMapper;

    @Resource
    private ZhongliVehicleMapper zhongliVehicleMapper;

    @Resource
    private VehicleMapper vehicleMapper;

    @Resource
    private VehiclePermitMapper vehiclePermitMapper;

    public void linkVehicle2Account() {
        List<ZhongliAccountModel> accountModelList = zhongliAccountMapper.selectByLevel(null);

        Map<String, List<ZhongliAccountModel>> accountMapByRoleName = accountModelList.stream().collect(Collectors.groupingBy(ZhongliAccountModel::getRoleName));

        Map<String, List<ZhongliAccountModel>> accountMapByLevel = accountModelList.stream()
                .filter(zam -> !zam.getRoleName().equals("仲利法务") && !zam.getRoleName().equals("仲利收车"))
                .sorted(Comparator.comparing(ZhongliAccountModel::getLevel))
                .collect(Collectors.groupingBy(ZhongliAccountModel::getLevel));

        //全部同步车辆
        List<Vehicle> vehicleList = vehicleMapper.selectAllByNoNullRefVehicleId();
        Map<Long, Vehicle> vehicleMap = vehicleList.stream().collect(Collectors.toMap(Vehicle::getRefVehicleId, v -> v));

        log.info("绝对授权方式");
        accountMapByRoleName.forEach((k, v) -> {
            v.forEach(account -> {
                List<ZhongliVehicle> zhongliVehicleList = zhongliVehicleMapper.selectByAccount(account.getAccount());
                log.info("账号{}(角色为:{})的车辆数:{}", account.getAccount(), k, zhongliVehicleList.size());
                if (k.equals("仲利法务") || k.equals("仲利收车")) {
                    List<VehiclePermit> permitList = Lists.newArrayList();
                    zhongliVehicleList.forEach(zv -> {
                        Vehicle vehicle = vehicleMap.get(zv.getVehicleId());
                        if (vehicle == null) {
                            return;
                        }
                        VehiclePermit vp = new VehiclePermit();
                        vp.setContentId(vehicle.getId());
                        vp.setAccorderId(0L);
                        vp.setReceiverId(1L);
                        vp.setContentType(0);
                        vp.setPermitType(1);
                        vp.setCreatedDate(new Date());
                        permitList.add(vp);
                    });
                    if (CollectionUtils.isNotEmpty(permitList)) {
                        //vehiclePermitMapper.insertList(permitList);
                    }
                }
            });
        });

        log.info("相对授权方式");
        accountMapByLevel.forEach((k, v) -> {
            v.forEach(account -> {
                List<ZhongliVehicle> zhongliVehicleList = zhongliVehicleMapper.selectByAccount(account.getAccount());
                log.info("{}账号{}(角色为:{})的车辆数:{}", k, account.getAccount(), k, zhongliVehicleList.size());

            });
        });

    }

}