package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maintain.common.constant.UserConstants;
import com.maintain.common.core.domain.entity.SysRole;
import com.maintain.common.helper.LoginHelper;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpCarBo;
import com.maintain.business.domain.vo.ErpCarVo;
import com.maintain.business.domain.ErpCar;
import com.maintain.business.mapper.ErpCarMapper;
import com.maintain.business.service.IErpCarService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户信息-车辆Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-25
 */
@RequiredArgsConstructor
@Service
public class ErpCarServiceImpl implements IErpCarService {

    private final ErpCarMapper baseMapper;

    /**
     * 查询客户信息-车辆
     */
    @Override
    public ErpCarVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车辆列表（自定义分页查询）
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<ErpCarVo> selectPageCarList(ErpCarBo bo, PageQuery pageQuery) {
        Page<ErpCarVo> carVoPage = baseMapper.selectPageCarList(pageQuery.build(), this.buildCustomQueryWrapper(bo));
        return TableDataInfo.build(carVoPage);
    }

    /**
     * 查询车辆列表（自定义查询）
     *
     * @param bo
     */
    @Override
    public List<ErpCarVo> selectCarList(ErpCarBo bo) {
        return baseMapper.selectCarList(this.buildCustomQueryWrapper(bo));
    }

    /**
     * 查询客户信息-车辆列表
     */
    @Override
    public TableDataInfo<ErpCarVo> queryPageList(ErpCarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCar> lqw = buildQueryWrapper(bo);
        Page<ErpCarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户信息-车辆列表
     */
    @Override
    public List<ErpCarVo> queryList(ErpCarBo bo) {
        LambdaQueryWrapper<ErpCar> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCar> buildQueryWrapper(ErpCarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCar> lqw = new LambdaQueryWrapper<>();
        return lqw;
    }


    private QueryWrapper<ErpCar> buildCustomQueryWrapper(ErpCarBo bo) {
        QueryWrapper<ErpCar> wrapper = Wrappers.query();
        wrapper
            .eq(ObjectUtil.isNotNull(bo.getClientType()), "t1.type", bo.getClientType())
            .like(StringUtils.isNotBlank(bo.getPhone()), "t.phone", bo.getPhone())
            .like(StringUtils.isNotBlank(bo.getClientName()), "t.client_name", bo.getClientName())
            .like(StringUtils.isNotBlank(bo.getPlateNumber()), "t.plate_number", bo.getPlateNumber());
        wrapper.orderByDesc("t.create_time");
        return wrapper;
    }

    /**
     * 新增客户信息-车辆
     */
    @Override
    public Boolean insertByBo(ErpCarBo bo) {
        ErpCar add = BeanUtil.toBean(bo, ErpCar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息-车辆
     */
    @Override
    public Boolean updateByBo(ErpCarBo bo) {
        ErpCar update = BeanUtil.toBean(bo, ErpCar.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCar entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户信息-车辆
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
