package com.ruoyi.bst.model.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.model.domain.ModelQuery;
import com.ruoyi.bst.model.domain.ModelVO;
import com.ruoyi.bst.model.mapper.ModelMapper;
import com.ruoyi.bst.model.service.ModelService;
import com.ruoyi.bst.model.service.ModelValidator;
import com.ruoyi.bst.modelSuit.domain.ModelSuit;
import com.ruoyi.bst.modelSuit.service.ModelSuitService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 车辆型号Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-18
 */
@Service
public class ModelServiceImpl implements ModelService
{

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ModelValidator modelValidator;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private ModelSuitService modelSuitService;

    /**
     * 查询车辆型号
     *
     * @param id 车辆型号主键
     * @return 车辆型号
     */
    @Override
    public ModelVO selectModelById(Long id, boolean scope)
    {
        ModelQuery query = new ModelQuery();
        query.setId(id);
        query.setScope(scope);
        return this.selectOne(query);
    }

    private ModelVO selectOne(ModelQuery query) {
        PageHelper.startPage(1, 1);
        List<ModelVO> list = this.selectModelList(query);
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询车辆型号列表
     *
     * @param model 车辆型号
     * @return 车辆型号
     */
    @Override
    public List<ModelVO> selectModelList(ModelQuery model)
    {
        return modelMapper.selectModelList(model);
    }

    /**
     * 新增车辆型号
     *
     * @param model 车辆型号
     * @return 结果
     */
    @Override
    public int insertModel(ModelVO data)
    {
        data.setCreateTime(DateUtils.getNowDate());

        Integer result = transactionTemplate.execute(status -> {
            int rows = modelMapper.insertModel(data);

            if (rows > 0 ) {
                // 更新套餐关联
                this.updateModelSuit(data);

                modelValidator.validate(data.getId());
            }

            return rows;
        });

        return result == null ? 0 : result;
    }

    /**
     * 修改车辆型号
     *
     * @param model 车辆型号
     * @return 结果
     */
    @Override
    public int updateModel(ModelVO data)
    {
        Integer result = transactionTemplate.execute(status -> {
            int rows = modelMapper.updateModel(data);

            if (rows > 0 ) {
                // 更新套餐关联
                this.updateModelSuit(data);

                // 验证
                modelValidator.validate(data.getId());  
            }

            return rows;
        });

        return result == null ? 0 : result;
    }

    private void updateModelSuit(ModelVO data) {
        // 删除套餐关联
        modelSuitService.deleteModelSuitByModelIds(Collections.singletonList(data.getId()));
        // 新增套餐关联
        if (data.getSuitIds() != null) {
            List<ModelSuit> modelSuitList = new ArrayList<>();
            for (Long suitId : data.getSuitIds()) {
                if (suitId == null) {
                    continue;
                }
                ModelSuit modelSuit = new ModelSuit();
                modelSuit.setModelId(data.getId());
                modelSuit.setSuitId(suitId);
                modelSuitList.add(modelSuit);
            }
            int insert = modelSuitService.batchInsert(modelSuitList);
            ServiceUtil.assertion(insert != modelSuitList.size(), "新增套餐关联失败");
        }
    }
    
    /**
     * 批量删除车辆型号
     *
     * @param ids 需要删除的车辆型号主键
     * @return 结果
     */
    @Override
    public int deleteModelByIds(Long[] ids)
    {
        return modelMapper.deleteModelByIds(ids);
    }

    /**
     * 删除车辆型号信息
     *
     * @param id 车辆型号主键
     * @return 结果
     */
    @Override
    public int deleteModelById(Long id)
    {
        return modelMapper.deleteModelById(id);
    }

    @Override
    public int logicDel(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return 0;
        }
        return modelMapper.logicDel(ids);
    }

    @Override
    public List<ModelVO> selectModelListByIds(List<Long> ids, boolean scope) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return Collections.emptyList();
        }
        ModelQuery query = new ModelQuery();
        query.setIds(ids);
        query.setScope(scope);
        return modelMapper.selectModelList(query);
    }
}
