package com.hotwj.platform.resourceManagement.vehicleInfo.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.domain.vo.HotGovEnterpriseUnitVo;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.service.IHotGovEnterpriseUnitService;
import com.hotwj.platform.resourceManagement.vehicleInfo.domain.bo.VehicleInfoBo;
import com.hotwj.platform.resourceManagement.vehicleInfo.domain.vo.VehicleInfoVo;
import com.hotwj.platform.resourceManagement.vehicleInfo.mapper.VehicleInfoMapper;
import com.hotwj.platform.resourceManagement.vehicleInfo.service.IVehicleInfoService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class VehicleInfoServiceImpl implements IVehicleInfoService {

    private final VehicleInfoMapper vehicleInfoMapper;
    private final IHotGovEnterpriseUnitService hotGovEnterpriseUnitService;

    @Override
    public TableDataInfo<VehicleInfoVo> queryPageByGovDistrict(PageQuery pageQuery, VehicleInfoBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        Page<VehicleInfoVo> page = pageQuery.build();
        if (page.orders() != null) {
            for (OrderItem order : page.orders()) {
                if ("create_time".equals(order.getColumn())) {
                    order.setColumn("v.create_time");
                }
            }
        }
        page.setRecords(vehicleInfoMapper.selectVehicleInfoPage(page, unitVo.getDistrictCode(), bo));
        return TableDataInfo.build(page);
    }
}
