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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import com.hotwj.platform.resourceManagement.driverInfo.domain.bo.DriverPageBo;
import com.hotwj.platform.resourceManagement.driverInfo.domain.vo.DriverPageVo;
import com.hotwj.platform.resourceManagement.driverInfo.mapper.DriverPageMapper;
import com.hotwj.platform.resourceManagement.driverInfo.service.IDriverPageService;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.domain.vo.HotGovEnterpriseUnitVo;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.service.IHotGovEnterpriseUnitService;
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;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DriverPageServiceImpl implements IDriverPageService {

    private final DriverPageMapper driverPageMapper;
    private final IHotGovEnterpriseUnitService hotGovEnterpriseUnitService;

    @Override
    public TableDataInfo<DriverPageVo> queryPage(PageQuery pageQuery, DriverPageBo 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<DriverPageVo> page = pageQuery.build();
        List<DriverPageVo> list = driverPageMapper.selectDriverPage(page, unitVo.getDistrictCode(), bo);
        // 数据脱敏处理
    /*    for (DriverPageVo vo : list) {
            if (vo.getIdCard() != null) {
                vo.setIdCard(DesensitizedUtil.idCardNum(vo.getIdCard(), 4, 4));
            }
            if (vo.getPhone() != null) {
                vo.setPhone(DesensitizedUtil.mobilePhone(vo.getPhone()));
            }
            if (vo.getLicenseNumber() != null && vo.getLicenseNumber().length() > 8) {
                vo.setLicenseNumber(maskString(vo.getLicenseNumber(), 4, 4));
            }
        }*/
        page.setRecords(list);
        return TableDataInfo.build(page);
    }

    /**
     * 自定义脱敏方法
     *
     * @param str    原始字符串
     * @param prefix 保留前几位
     * @param suffix 保留后几位
     * @return 脱敏后的字符串
     */
    private String maskString(String str, int prefix, int suffix) {
        if (str == null || str.length() <= prefix + suffix) {
            return str;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(str.substring(0, prefix));
        for (int i = 0; i < str.length() - prefix - suffix; i++) {
            sb.append("*");
        }
        sb.append(str.substring(str.length() - suffix));
        return sb.toString();
    }
}
