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.OaProductBo;
import com.ruoyi.oa.domain.vo.OaProductVo;
import com.ruoyi.oa.domain.OaProduct;
import com.ruoyi.oa.mapper.OaProductMapper;
import com.ruoyi.oa.service.IOaProductService;

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

/**
 * CRM 产品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@RequiredArgsConstructor
@Service
public class OaProductServiceImpl implements IOaProductService {

    private final OaProductMapper baseMapper;

    /**
     * 查询CRM 产品
     */
    @Override
    public OaProductVo queryById(Long productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 查询CRM 产品列表
     */
    @Override
    public TableDataInfo<OaProductVo> queryPageList(OaProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaProduct> lqw = buildQueryWrapper(bo);
        Page<OaProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询CRM 产品列表
     */
    @Override
    public List<OaProductVo> queryList(OaProductBo bo) {
        LambdaQueryWrapper<OaProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaProduct> buildQueryWrapper(OaProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaProduct> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), OaProduct::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), OaProduct::getProductNumber, bo.getProductNumber());
        lqw.eq(bo.getUnit() != null, OaProduct::getUnit, bo.getUnit());
        lqw.eq(bo.getPrice() != null, OaProduct::getPrice, bo.getPrice());
        lqw.eq(bo.getStatus() != null, OaProduct::getStatus, bo.getStatus());
        lqw.eq(bo.getCategoryId() != null, OaProduct::getCategoryId, bo.getCategoryId());
        lqw.like(StringUtils.isNotBlank(bo.getDescription()), OaProduct::getDescription, bo.getDescription());
        lqw.eq(bo.getOwnerUserId() != null, OaProduct::getOwnerUserId, bo.getOwnerUserId());
        return lqw;
    }

    /**
     * 新增CRM 产品
     */
    @Override
    public Boolean insertByBo(OaProductBo bo) {
        OaProduct add = BeanUtil.toBean(bo, OaProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductId(add.getProductId());
        }
        return flag;
    }

    /**
     * 修改CRM 产品
     */
    @Override
    public Boolean updateByBo(OaProductBo bo) {
        OaProduct update = BeanUtil.toBean(bo, OaProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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