package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.business.domain.ErpTyrePosition;
import com.maintain.business.domain.bo.ErpTyrePositionBo;
import com.maintain.business.domain.vo.ErpTyrePositionVo;
import com.maintain.business.mapper.ErpTyrePositionMapper;
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.ErpVehicleModelBo;
import com.maintain.business.domain.vo.ErpVehicleModelVo;
import com.maintain.business.domain.ErpVehicleModel;
import com.maintain.business.mapper.ErpVehicleModelMapper;
import com.maintain.business.service.IErpVehicleModelService;

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

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

    private final ErpVehicleModelMapper baseMapper;
    private final ErpTyrePositionMapper tyrePositionMapper;

    /**
     * 查询客户信息-车型
     */
    @Override
    public ErpVehicleModelVo queryById(Long id){
        ErpVehicleModelVo erpVehicleModelVo = baseMapper.selectVoById(id);
        LambdaQueryWrapper<ErpTyrePosition> positionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        positionLambdaQueryWrapper.eq(ErpTyrePosition::getVehicleModelId, id);
        List<ErpTyrePositionVo> positionVos = tyrePositionMapper.selectVoList(positionLambdaQueryWrapper);
        erpVehicleModelVo.setTyrePositions(positionVos);
        return erpVehicleModelVo;
    }

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

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

    private LambdaQueryWrapper<ErpVehicleModel> buildQueryWrapper(ErpVehicleModelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpVehicleModel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCarTypeCode()), ErpVehicleModel::getCarTypeCode, bo.getCarTypeCode());
        lqw.like(StringUtils.isNotBlank(bo.getCarBrand()), ErpVehicleModel::getCarBrand, bo.getCarBrand());
        lqw.like(StringUtils.isNotBlank(bo.getCarModel()), ErpVehicleModel::getCarModel, bo.getCarModel());
        lqw.eq(StringUtils.isNotBlank(bo.getCarConfiguration()), ErpVehicleModel::getCarConfiguration, bo.getCarConfiguration());
        lqw.eq(StringUtils.isNotBlank(bo.getCarColour()), ErpVehicleModel::getCarColour, bo.getCarColour());
        lqw.eq(bo.getType() != null, ErpVehicleModel::getType, bo.getType());
        lqw.eq(bo.getExpendType() != null, ErpVehicleModel::getExpendType, bo.getExpendType());
        lqw.eq(StringUtils.isNotBlank(bo.getChassisNumber()), ErpVehicleModel::getChassisNumber, bo.getChassisNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getMotorNumber()), ErpVehicleModel::getMotorNumber, bo.getMotorNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineNumber()), ErpVehicleModel::getEngineNumber, bo.getEngineNumber());
        lqw.eq(bo.getAxleNumber() != null, ErpVehicleModel::getAxleNumber, bo.getAxleNumber());
        lqw.eq(bo.getCreateTime() != null, ErpVehicleModel::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增客户信息-车型
     */
    @Override
    public Boolean insertByBo(ErpVehicleModelBo bo) {
        ErpVehicleModel add = BeanUtil.toBean(bo, ErpVehicleModel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            List<ErpTyrePosition> save = bo.getTyrePositions().stream()
                .map(item -> {
                    ErpTyrePosition newItem = new ErpTyrePosition(); // 假设有拷贝构造函数
                    newItem.setCode(item.getCode());
                    newItem.setName(item.getName());
                    newItem.setVehicleModelId(add.getId());
                    return newItem;
                }).collect(Collectors.toList());
            // 批量保存胎位信息
            tyrePositionMapper.insertBatch(save);
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息-车型
     */
    @Override
    public Boolean updateByBo(ErpVehicleModelBo bo) {
        ErpVehicleModel update = BeanUtil.toBean(bo, ErpVehicleModel.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            LambdaQueryWrapper<ErpTyrePosition> deleteWrapper = new LambdaQueryWrapper<>();
            deleteWrapper.eq(ErpTyrePosition::getVehicleModelId, update.getId());
            tyrePositionMapper.delete(deleteWrapper);
            List<ErpTyrePosition> save = bo.getTyrePositions().stream()
                .map(item -> {
                    ErpTyrePosition newItem = new ErpTyrePosition(); // 假设有拷贝构造函数
                    newItem.setCode(item.getCode());
                    newItem.setName(item.getName());
                    newItem.setVehicleModelId(update.getId());
                    return newItem;
                }).collect(Collectors.toList());
            // 批量保存胎位信息
            tyrePositionMapper.insertBatch(save);
        }
        return flag;
    }

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

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