package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.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.ruoyi.oa.domain.bo.OaBusinessProductBo;
import com.ruoyi.oa.domain.vo.OaBusinessProductVo;
import com.ruoyi.oa.domain.OaBusinessProduct;
import com.ruoyi.oa.mapper.OaBusinessProductMapper;
import com.ruoyi.oa.service.IOaBusinessProductService;

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

/**
 * CRM 商机产品关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@RequiredArgsConstructor
@Service
public class OaBusinessProductServiceImpl implements IOaBusinessProductService {

    private final OaBusinessProductMapper baseMapper;

    /**
     * 查询CRM 商机产品关联
     */
    @Override
    public OaBusinessProductVo queryById(Long businessProductId){
        return baseMapper.selectVoById(businessProductId);
    }

    /**
     * 查询CRM 商机产品关联列表
     */
    @Override
    public TableDataInfo<OaBusinessProductVo> queryPageList(OaBusinessProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaBusinessProduct> lqw = buildQueryWrapper(bo);
        Page<OaBusinessProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询CRM 商机产品关联列表
     */
    @Override
    public List<OaBusinessProductVo> queryList(OaBusinessProductBo bo) {
        LambdaQueryWrapper<OaBusinessProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaBusinessProduct> buildQueryWrapper(OaBusinessProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaBusinessProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBusinessId() != null, OaBusinessProduct::getBusinessId, bo.getBusinessId());
        lqw.eq(bo.getProductId() != null, OaBusinessProduct::getProductId, bo.getProductId());
        lqw.eq(bo.getProductPrice() != null, OaBusinessProduct::getProductPrice, bo.getProductPrice());
        lqw.eq(bo.getBusinessPrice() != null, OaBusinessProduct::getBusinessPrice, bo.getBusinessPrice());
        lqw.eq(bo.getCount() != null, OaBusinessProduct::getCount, bo.getCount());
        lqw.eq(bo.getTotalPrice() != null, OaBusinessProduct::getTotalPrice, bo.getTotalPrice());
        return lqw;
    }

    /**
     * 新增CRM 商机产品关联
     */
    @Override
    public Boolean insertByBo(OaBusinessProductBo bo) {
        OaBusinessProduct add = BeanUtil.toBean(bo, OaBusinessProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBusinessProductId(add.getBusinessProductId());
        }
        return flag;
    }

    /**
     * 修改CRM 商机产品关联
     */
    @Override
    public Boolean updateByBo(OaBusinessProductBo bo) {
        OaBusinessProduct update = BeanUtil.toBean(bo, OaBusinessProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除CRM 商机产品关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
