package cn.iocoder.yudao.module.rescue.service.impl;

import cn.iocoder.yudao.module.rescue.domain.DriverInfo;
import cn.iocoder.yudao.module.rescue.domain.RescueDeptDriver;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.rescue.service.IDriverInfoService;
import cn.iocoder.yudao.module.rescue.service.IRescueDeptDriverService;
import cn.iocoder.yudao.module.rescue.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.module.rescue.mapper.RescueDeptDriverMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

/**
 * 救援客户司机分配Service业务层处理
 *
 * @author zcy
 * @date 2023-09-21
 */
@Service
public class RescueDeptDriverServiceImpl extends ServiceImpl<RescueDeptDriverMapper, RescueDeptDriver> implements IRescueDeptDriverService {
    @Autowired
    private DeptApi deptService;
    @Autowired
    private AdminUserApi userService;


    @Autowired
    private IDriverInfoService driverInfoService;

    /**
     * 查询救援客户司机分配
     *
     * @param id 救援客户司机分配主键
     * @return 救援客户司机分配
     */
    @Override
    public RescueDeptDriver selectRescueDeptDriverById(Long id) {
        return baseMapper.selectRescueDeptDriverById(id);
    }

    /**
     * 查询救援客户司机分配列表
     *
     * @param rescueDeptDriver 救援客户司机分配
     * @return 救援客户司机分配
     */
    @Override
    public List<RescueDeptDriver> selectRescueDeptDriverList(RescueDeptDriver rescueDeptDriver) {
        List<RescueDeptDriver> rescueDeptDrivers = baseMapper.selectRescueDeptDriverList(rescueDeptDriver);
        for (RescueDeptDriver deptDriver : rescueDeptDrivers) {
            DeptRespDTO dept = deptService.getDept(deptDriver.getDeptId());
            deptDriver.setDeptName(dept.getName());
            String firstDriverIds = deptDriver.getFirstDriverIds();
            if (StringUtils.isNotEmpty(firstDriverIds)) {
                String[] driverIds = firstDriverIds.split(",");
                String firstDriverName = "";
                for (String driverId : driverIds) {
                    DriverInfo driverInfo = driverInfoService.getById(driverId);
                    AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
                    firstDriverName = firstDriverName + sysUser.getNickname() + "|";
                }
                deptDriver.setFirstDriverName(firstDriverName);


            }
            String secondDriverIds = deptDriver.getSecondDriverIds();
            if (StringUtils.isNotEmpty(secondDriverIds)) {
                String[] driverIds = secondDriverIds.split(",");
                String name = "";
                for (String driverId : driverIds) {
                    DriverInfo driverInfo = driverInfoService.getById(driverId);
                    AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
                    name = name + sysUser.getNickname() + "|";
                }
                deptDriver.setSecondDriverName(name);


            }
            String thirdDriverIds = deptDriver.getThirdDriverIds();
            if (StringUtils.isNotEmpty(thirdDriverIds)) {
                String[] driverIds = thirdDriverIds.split(",");
                String name = "";
                for (String driverId : driverIds) {
                    DriverInfo driverInfo = driverInfoService.getById(driverId);
                    AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
                    name = name + sysUser.getNickname() + "|";
                }
                deptDriver.setThirdDriverIds(name);

            }

        }
        return rescueDeptDrivers;
    }

    /**
     * 分页查
     * @author 小李
     * @date 8:59 2024/8/20
     * @param rescueDeptDriver
     **/
    @Override
    public IPage<RescueDeptDriver> selectRescueDeptDriverPage(RescueDeptDriver rescueDeptDriver, Page<RescueDeptDriver> page) {
        IPage<RescueDeptDriver> driverIPage = baseMapper.selectRescueDeptDriverList(rescueDeptDriver, page);
        for (RescueDeptDriver deptDriver : driverIPage.getRecords()) {
            DeptRespDTO dept = deptService.getDept(deptDriver.getDeptId());
            deptDriver.setDeptName(dept.getName());

            String firstDriverIds = deptDriver.getFirstDriverIds();
            if (StringUtils.isNotEmpty(firstDriverIds)) {
                StringJoiner tempStr = new StringJoiner("|");
                String[] driverIds = firstDriverIds.split(",");
                for (String driverId : driverIds) {
                    DriverInfo driverInfo = driverInfoService.getById(driverId);
                    if (driverInfo != null) {
                        AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
                        if (sysUser != null) {
                            tempStr.add(sysUser.getNickname());
                        } else {
                            tempStr.add("未知用户");
                        }
                    }
                }
                deptDriver.setFirstDriverName(tempStr.toString());
            }

            // 对 secondDriverIds 和 thirdDriverIds 进行类似的处理
            String secondDriverIds = deptDriver.getSecondDriverIds();
            if (StringUtils.isNotEmpty(secondDriverIds)) {
                StringJoiner tempStr = new StringJoiner("|");
                String[] driverIds = secondDriverIds.split(",");
                for (String driverId : driverIds) {
                    DriverInfo driverInfo = driverInfoService.getById(driverId);
                    if (driverInfo != null) {
                        AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
                        if (sysUser != null) {
                            tempStr.add(sysUser.getNickname());
                        } else {
                            tempStr.add("未知用户");
                        }
                    }
                }
                deptDriver.setSecondDriverName(tempStr.toString());
            }

            // 对 thirdDriverIds 进行类似的处理
            String thirdDriverIds = deptDriver.getThirdDriverIds();
            if (StringUtils.isNotEmpty(thirdDriverIds)) {
                StringJoiner tempStr = new StringJoiner("|");
                String[] driverIds = thirdDriverIds.split(",");
                for (String driverId : driverIds) {
                    DriverInfo driverInfo = driverInfoService.getById(driverId);
                    if (driverInfo != null) {
                        AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
                        if (sysUser != null) {
                            tempStr.add(sysUser.getNickname());
                        } else {
                            tempStr.add("未知用户");
                        }
                    }
                }
                deptDriver.setThirdDriverName(tempStr.toString());
            }
        }
        return driverIPage;
    }


    /**
     * 新增救援客户司机分配
     *
     * @param rescueDeptDriver 救援客户司机分配
     * @return 结果
     */
    @Override
    public int insertRescueDeptDriver(RescueDeptDriver rescueDeptDriver) {
        return baseMapper.insert(rescueDeptDriver);
    }

    /**
     * 修改救援客户司机分配
     *
     * @param rescueDeptDriver 救援客户司机分配
     * @return 结果
     */
    @Override
    public int updateRescueDeptDriver(RescueDeptDriver rescueDeptDriver) {
        return baseMapper.updateById(rescueDeptDriver);
    }

    /**
     * 批量删除救援客户司机分配
     *
     * @param ids 需要删除的救援客户司机分配主键
     * @return 结果
     */
    @Override
    public int deleteRescueDeptDriverByIds(Long[] ids) {
        return baseMapper.deleteByIds(Arrays.asList(ids));
    }

    /**
     * 删除救援客户司机分配信息
     *
     * @param id 救援客户司机分配主键
     * @return 结果
     */
    @Override
    public int deleteRescueDeptDriverById(Long id) {
        return baseMapper.deleteRescueDeptDriverById(id);
    }
}
