package com.njzscloud.supervisory.biz.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.support.UserDetail;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.biz.contant.BizObj;
import com.njzscloud.supervisory.biz.mapper.BizDriverMapper;
import com.njzscloud.supervisory.biz.pojo.entity.BizDriverEntity;
import com.njzscloud.supervisory.biz.pojo.param.AddDriverParam;
import com.njzscloud.supervisory.biz.pojo.result.SearchCompanyResult;
import com.njzscloud.supervisory.sys.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 司机信息
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BizDriverService extends ServiceImpl<BizDriverMapper, BizDriverEntity> implements IService<BizDriverEntity> {
    private final UserService sysUserService;

    /**
     * 新增
     *
     * @param addDriverParam 数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AddDriverParam addDriverParam) {
        UserDetail userDetail = SecurityUtil.loginUser();
        SearchCompanyResult searchCompanyResult = baseMapper.selectCompanyByUserId(userDetail.getUserId());

        Long driverUserId = sysUserService.add(addDriverParam.getUser()
                .setBizObj(BizObj.SiJi)
        );
        BizDriverEntity bizDriverEntity = BeanUtil.copyProperties(addDriverParam, BizDriverEntity.class)
                .setUserId(driverUserId)
                .setCompanyId(searchCompanyResult.getId());
        this.save(bizDriverEntity);
    }

    /**
     * 修改
     *
     * @param bizDriverEntity 数据
     */
    public void modify(BizDriverEntity bizDriverEntity) {
        this.updateById(bizDriverEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return BizDriverEntity 结果
     */
    public BizDriverEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param bizDriverEntity 筛选条件
     * @param pageParam       分页参数
     * @return PageResult&lt;BizDriverEntity&gt; 分页结果
     */
    public PageResult<BizDriverEntity> paging(PageParam pageParam, BizDriverEntity bizDriverEntity) {
        String driverName = bizDriverEntity.getDriverName();
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<BizDriverEntity>lambdaQuery()
                .like(StrUtil.isNotBlank(driverName), BizDriverEntity::getDriverName, driverName)));
    }

    public PageResult<BizDriverEntity> dispensabilityList(PageParam pageParam, String keywords) {
        UserDetail userDetail = SecurityUtil.loginUser();
        SearchCompanyResult searchCompanyResult = baseMapper.selectCompanyByUserId(userDetail.getUserId());

        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<BizDriverEntity>lambdaQuery()
                // .eq(BizDriverEntity::getBusy, false)
                .eq(BizDriverEntity::getCompanyId, searchCompanyResult.getId())
                .and(StrUtil.isNotBlank(keywords), it ->
                        it.like(BizDriverEntity::getDriverName, keywords)
                                .or().like(BizDriverEntity::getPhone, keywords))
        ));
    }
}
