package com.yc.ship.module.resource.service.driver;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.resource.controller.admin.driver.vo.*;
import com.yc.ship.module.resource.dal.dataobject.driver.DriverDO;
import com.yc.ship.module.resource.dal.mysql.driver.DriverMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import com.yc.ship.module.resource.enums.UseStatusEnum;
import com.yc.ship.module.system.api.user.AdminUserApi;
import com.yc.ship.module.system.api.user.dto.AdminUserRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 驾驶员管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
@Slf4j
public class DriverServiceImpl implements DriverService {

    @Resource
    private DriverMapper driverMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createDriver(DriverSaveReqVO createReqVO) {
        // 创建系统用户
        AdminUserRespDTO adminDto = new AdminUserRespDTO();
        adminDto.setNickname(createReqVO.getName());
        adminDto.setUsername(PinyinUtil.getPinyin(createReqVO.getName(), ""));
        adminDto.setMobile(createReqVO.getPhone());
        adminDto.setPassword(createReqVO.getPhone());
        adminDto.setSex(createReqVO.getSex());
        Long userId = adminUserApi.createUser(adminDto);
        if (userId != null && userId > 0) {
            // 插入
            DriverDO driver = BeanUtils.toBean(createReqVO, DriverDO.class);
            driver.setId(String.valueOf(userId));
            driver.setDeleted(false);
            driverMapper.insert(driver);
            return driver.getId();
        } else {
            throw exception(ErrorCodeConstants.DRIVER_CREATE_FAILED);
        }
    }

    @Override
    public void updateDriver(DriverSaveReqVO updateReqVO) {
        // 校验存在
        validateDriverExists(updateReqVO.getId());
        // 更新
        DriverDO updateObj = BeanUtils.toBean(updateReqVO, DriverDO.class);

        driverMapper.updateById(updateObj);
    }

    @Override
    @Transactional
    public void deleteDriver(String id) {
        // 校验存在
        validateDriverExists(id);
        // 删除系统用户信息
        adminUserApi.deleteUser(Long.valueOf(id));
        // 删除
        driverMapper.deleteById(id);
    }

    private void validateDriverExists(String id) {
        if (driverMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.DRIVER_NOT_EXISTS);
        }
    }

    @Override
    public DriverDO getDriver(String id) {
        return driverMapper.selectById(id);
    }

    @Override
    public PageResult<DriverDO> getDriverPage(DriverPageReqVO pageReqVO) {

        return driverMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DriverDO> getDriverList() {
        return this.getDriverList(new DriverSimpleRespVO());
    }

    @Override
    public List<DriverDO> getDriverList(DriverSimpleRespVO respVO) {
        if (null == respVO.getUseStatus()) {
            respVO.setUseStatus(UseStatusEnum.ENABLE.getStatus());
        }
        List<DriverDO> list = driverMapper.selectList(respVO);
        list.sort(Comparator.comparing(DriverDO::getUpdateTime));
        return list;
    }

    @Override
    public List<DriverDO> getSimpleCarList() {
        return driverMapper.selectSimpleList();
    }

    @Override
    public List<DriverRespVO> getDriverDefaultCarList(DriverReqVO reqVO) {
        reqVO.setTenantId(String.valueOf(TenantContextHolder.getTenantId()));
        return driverMapper.getDriverDefaultCarList(reqVO);
    }

    @Override
    public List<DriverDO> getSimpleDriverListByCarType(String carType) {
        return driverMapper.getSimpleDriverListByCarType(carType);
    }

}