package com.leromro.core.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leromro.core.domain.BusinessManagement;
import com.leromro.core.domain.vo.BusinessPriceVO;
import com.leromro.core.mapper.BusinessPriceMapper;
import com.leromro.common.utils.DateUtils;
import com.leromro.core.service.IBusinessManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.leromro.core.mapper.BusinessPriceMapper;
import com.leromro.core.domain.BusinessPrice;
import com.leromro.core.service.IBusinessPriceService;

/**
 * 业务价格Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-17
 */
@Service
public class BusinessPriceServiceImpl extends ServiceImpl<BusinessPriceMapper, BusinessPrice> implements IBusinessPriceService
{
    @Autowired
    private BusinessPriceMapper businessPriceMapper;

    @Autowired
    private IBusinessManagementService businessManagementService;

    /**
     * 查询业务价格
     * 
     * @param businessPriceId 业务价格主键
     * @return 业务价格
     */
    @Override
    public BusinessPrice selectBusinessPriceByBusinessPriceId(Long businessPriceId)
    {
        return businessPriceMapper.selectBusinessPriceByBusinessPriceId(businessPriceId);
    }

    /**
     * 查询业务价格列表
     * 
     * @param businessPrice 业务价格
     * @return 业务价格
     */
    @Override
    public List<BusinessPriceVO> selectBusinessPriceList(BusinessPrice businessPrice)
    {
        return businessPriceMapper.selectBusinessPriceList(businessPrice);
    }

    /**
     * 新增业务价格
     * 
     * @param businessPrice 业务价格
     * @return 结果
     */
    @Override
    public Boolean insertBusinessPrice(BusinessPrice businessPrice)
    {
        //查询业务管理信息
        BusinessManagement businessManagement = businessManagementService.getOne(new LambdaQueryWrapper<BusinessManagement>()
                .eq(BusinessManagement::getBusinessManagementId, businessPrice.getBusinessManagementId()));
        if (StrUtil.isBlank(businessManagement.getBusinessTierName()) || businessManagement.getAncestors().length() < 2){
            throw new RuntimeException("当前服务项目层级名称错误");
        }
        //把BusinessTierName按照-分割
        String[] businessTierName = businessManagement.getBusinessTierName().split("-");
        businessPrice.setProjectName(businessTierName[0]);
        businessPrice.setProjectTypeName(businessTierName[1]);
        businessPrice.setBusinessTierName(businessManagement.getBusinessTierName());
        return this.save(businessPrice);
    }

    /**
     * 修改业务价格
     * 
     * @param businessPrice 业务价格
     * @return 结果
     */
    @Override
    public Boolean updateBusinessPrice(BusinessPrice businessPrice)
    {
        return this.updateById(businessPrice);
    }

    /**
     * 批量删除业务价格
     * 
     * @param businessPriceIds 需要删除的业务价格主键
     * @return 结果
     */
    @Override
    public int deleteBusinessPriceByBusinessPriceIds(Long[] businessPriceIds)
    {
        return businessPriceMapper.deleteBusinessPriceByBusinessPriceIds(businessPriceIds);
    }

    /**
     * 删除业务价格信息
     * 
     * @param businessPriceId 业务价格主键
     * @return 结果
     */
    @Override
    public int deleteBusinessPriceByBusinessPriceId(Long businessPriceId)
    {
        return businessPriceMapper.deleteBusinessPriceByBusinessPriceId(businessPriceId);
    }
}
