package com.njzscloud.dispose.cst.driver.service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.dispose.cst.customer.mapper.CustomerMapper;
import com.njzscloud.dispose.cst.customer.pojo.entity.CustomerEntity;
import com.njzscloud.dispose.cst.driver.mapper.DriverMapper;
import com.njzscloud.dispose.cst.driver.pojo.entity.DriverEntity;
import com.njzscloud.dispose.cst.driver.pojo.param.DriverQueryParam;
import com.njzscloud.dispose.cst.driver.pojo.param.QRCodeParam;
import com.njzscloud.dispose.cst.driver.pojo.result.DriverResult;
import com.njzscloud.dispose.cst.driver.service.DriverService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 司机
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DriverServiceImpl extends ServiceImpl<DriverMapper, DriverEntity> implements DriverService {

    private final CustomerMapper customerMapper;

    @Override
    public void add(DriverEntity driverEntity) {
        this.save(driverEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(DriverEntity driverEntity) {
        Long driverId = driverEntity.getId();
        // 查询司机当前信息
        DriverEntity currentDriver = this.getById(driverId);
        if (currentDriver == null) {
            throw Exceptions.clierr("司机信息不存在");
        }

        Long newOrgId = driverEntity.getOrgId();
        Long oldOrgId = currentDriver.getOrgId();
        Long customerId = currentDriver.getCustomerId();

        // 判断是否需要更新客户的orgId
        if (customerId != null && newOrgId != null && ObjectUtil.notEqual(newOrgId, oldOrgId)) {
            // 更新客户的orgId
            CustomerEntity customer = new CustomerEntity();
            customer.setId(customerId);
            customer.setOrgId(newOrgId);
            customerMapper.updateById(customer);
        }

        this.updateById(driverEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    @Override
    public DriverResult detail(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public DriverResult detailByUserId(Long userId) {
        return baseMapper.getByUserId(userId);
    }

    @Override
    public PageResult<DriverResult> paging(PageParam pageParam, DriverQueryParam queryParam) {
        String driverName = queryParam.getDriverName();
        String keywords = queryParam.getKeywords();

        QueryWrapper<DriverEntity> ew = Wrappers.<DriverEntity>query()
            .eq("cd.deleted", Boolean.FALSE)
            .eq(queryParam.getBusy() != null, "cd.busy", queryParam.getBusy())
            .like(StrUtil.isNotBlank(driverName), "cd.driver_name", driverName)
            .and(StrUtil.isNotBlank(keywords), it -> it
                .like("cd.driver_name", keywords)
                .or().like("cd.phone", keywords)
            );

        return PageResult.of(baseMapper.paging(pageParam.toPage(), ew));
    }

    @Override
    public String getCode(QRCodeParam codeParam) {
        Integer width = codeParam.getWidth();
        Integer height = codeParam.getHeight();
        if (width == null) {
            width = 300;
        }
        if (height == null) {
            height = 300;
        }
        String imgType = codeParam.getImgType();
        if (Strings.isNullOrEmpty(imgType)) {
            imgType = ImgUtil.IMAGE_TYPE_JPG;
        }

        return QrCodeUtil.generateAsBase64(codeParam.getContent(), new QrConfig(width, height), imgType);

    }
}
