package com.cpxt.biz.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cpxt.biz.domain.BizCustomerWarehouse;
import com.cpxt.common.core.domain.entity.SysDept;
import com.cpxt.common.core.domain.entity.SysUser;
import com.cpxt.common.utils.DateUtils;
import com.cpxt.system.mapper.SysDeptMapper;
import com.cpxt.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpxt.biz.mapper.BizDriverMapper;
import com.cpxt.biz.domain.BizDriver;
import com.cpxt.biz.service.IBizDriverService;

/**
 * 司机Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizDriverServiceImpl implements IBizDriverService 
{
    @Autowired
    private BizDriverMapper bizDriverMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询司机
     * 
     * @param id 司机主键
     * @return 司机
     */
    @Override
    public BizDriver selectBizDriverById(Long id)
    {
        return bizDriverMapper.selectById(id);
    }

    /**
     * 查询司机列表
     * 
     * @param bizDriver 司机
     * @return 司机
     */
    @Override
    public List<BizDriver> selectBizDriverList(BizDriver bizDriver)
    {
        LambdaQueryWrapper<BizDriver> queryWrapper = buildQueryWrapper(bizDriver);
        return bizDriverMapper.selectList(queryWrapper);
    }

    private LambdaQueryWrapper<BizDriver> buildQueryWrapper(BizDriver bizDriver) {
        LambdaQueryWrapper<BizDriver> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizDriver.getDeptId())){
            queryWrapper.in(BizDriver::getDeptId,sysDeptMapper.selectChildrenDeptById(bizDriver.getDeptId()).stream().map(SysDept::getDeptId).collect(Collectors.toList()));
        }
        if (ObjectUtil.isNotEmpty(bizDriver.getName())){
            queryWrapper.like(BizDriver::getName, bizDriver.getName());
        }
        if (ObjectUtil.isNotEmpty(bizDriver.getIdcard())){
            queryWrapper.like(BizDriver::getIdcard, bizDriver.getIdcard());
        }
        if (ObjectUtil.isNotEmpty(bizDriver.getGender())){
            queryWrapper.eq(BizDriver::getGender, bizDriver.getGender());
        }
        if (ObjectUtil.isNotEmpty(bizDriver.getPhone())){
            queryWrapper.like(BizDriver::getPhone, bizDriver.getPhone());
        }
        if (ObjectUtil.isNotEmpty(bizDriver.getDefaultCarNo())){
            queryWrapper.like(BizDriver::getDefaultCarNo, bizDriver.getDefaultCarNo());
        }
        return queryWrapper;
    }

    /**
     * 新增司机
     * 
     * @param bizDriver 司机
     * @return 结果
     */
    @Override
    public int insertBizDriver(BizDriver bizDriver)
    {
        SysUser sysUser = new SysUser();
        setSysUser(bizDriver,sysUser);
        sysUserMapper.insertUser(sysUser);

        bizDriver.setDeptName(sysDeptMapper.selectDeptById(bizDriver.getDeptId()).getDeptName());
        bizDriver.setUserId(sysUser.getUserId());
        return bizDriverMapper.insert(bizDriver);
    }

    private void setSysUser(BizDriver bizDriver, SysUser sysUser) {
        sysUser.setDeptId(bizDriver.getDeptId());
        sysUser.setUserName(bizDriver.getPhone());
        sysUser.setNickName(bizDriver.getName());
        sysUser.setUserType("1"); // 司机
        sysUser.setPhonenumber(bizDriver.getPhone());
        sysUser.setSex(bizDriver.getGender());
        sysUser.setAvatar(bizDriver.getPhoto());
        sysUser.setPassword(bizDriver.getIdcard().substring(12,18)); // 340123199906222873
    }

    /**
     * 修改司机
     * 
     * @param bizDriver 司机
     * @return 结果
     */
    @Override
    public int updateBizDriver(BizDriver bizDriver)
    {
        return bizDriverMapper.updateById(bizDriver);
    }

    /**
     * 批量删除司机
     * 
     * @param ids 需要删除的司机主键
     * @return 结果
     */
    @Override
    public int deleteBizDriverByIds(Long[] ids)
    {
        UpdateWrapper<BizDriver> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", (Object) ids);
        updateWrapper.set("status","2"); //状态改为删除
        return bizDriverMapper.update(null,updateWrapper);
    }

    /**
     * 删除司机信息
     * 
     * @param id 司机主键
     * @return 结果
     */
    @Override
    public int deleteBizDriverById(Long id)
    {
        UpdateWrapper<BizDriver> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status","2"); //状态改为删除
        return bizDriverMapper.update(null,updateWrapper);
    }
}
