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.OaClueBo;
import com.ruoyi.oa.domain.vo.OaClueVo;
import com.ruoyi.oa.domain.OaClue;
import com.ruoyi.oa.mapper.OaClueMapper;
import com.ruoyi.oa.service.IOaClueService;

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

/**
 * CRM 线索Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@RequiredArgsConstructor
@Service
public class OaClueServiceImpl implements IOaClueService {

    private final OaClueMapper baseMapper;

    /**
     * 查询CRM 线索
     */
    @Override
    public OaClueVo queryById(Long clueId){
        return baseMapper.selectVoById(clueId);
    }

    /**
     * 查询CRM 线索列表
     */
    @Override
    public TableDataInfo<OaClueVo> queryPageList(OaClueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaClue> lqw = buildQueryWrapper(bo);
        Page<OaClueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询CRM 线索列表
     */
    @Override
    public List<OaClueVo> queryList(OaClueBo bo) {
        LambdaQueryWrapper<OaClue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaClue> buildQueryWrapper(OaClueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaClue> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getClueName()), OaClue::getClueName, bo.getClueName());
        lqw.eq(bo.getFollowUpStatus() != null, OaClue::getFollowUpStatus, bo.getFollowUpStatus());
        lqw.eq(bo.getContactLastTime() != null, OaClue::getContactLastTime, bo.getContactLastTime());
        lqw.eq(StringUtils.isNotBlank(bo.getContactLastContent()), OaClue::getContactLastContent, bo.getContactLastContent());
        lqw.eq(bo.getContactNextTime() != null, OaClue::getContactNextTime, bo.getContactNextTime());
        lqw.eq(bo.getOwnerUserId() != null, OaClue::getOwnerUserId, bo.getOwnerUserId());
        lqw.eq(bo.getTransformStatus() != null, OaClue::getTransformStatus, bo.getTransformStatus());
        lqw.eq(bo.getCustomerId() != null, OaClue::getCustomerId, bo.getCustomerId());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), OaClue::getMobile, bo.getMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getTelephone()), OaClue::getTelephone, bo.getTelephone());
        lqw.eq(StringUtils.isNotBlank(bo.getQq()), OaClue::getQq, bo.getQq());
        lqw.eq(StringUtils.isNotBlank(bo.getWechat()), OaClue::getWechat, bo.getWechat());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), OaClue::getEmail, bo.getEmail());
        lqw.eq(bo.getAreaId() != null, OaClue::getAreaId, bo.getAreaId());
        lqw.like(StringUtils.isNotBlank(bo.getDetailAddress()), OaClue::getDetailAddress, bo.getDetailAddress());
        lqw.eq(bo.getIndustryId() != null, OaClue::getIndustryId, bo.getIndustryId());
        lqw.eq(bo.getLevel() != null, OaClue::getLevel, bo.getLevel());
        lqw.eq(bo.getSource() != null, OaClue::getSource, bo.getSource());
        return lqw;
    }

    /**
     * 新增CRM 线索
     */
    @Override
    public Boolean insertByBo(OaClueBo bo) {
        OaClue add = BeanUtil.toBean(bo, OaClue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClueId(add.getClueId());
        }
        return flag;
    }

    /**
     * 修改CRM 线索
     */
    @Override
    public Boolean updateByBo(OaClueBo bo) {
        OaClue update = BeanUtil.toBean(bo, OaClue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public TableDataInfo<OaClueVo> queryPageListVo(OaClueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaClue> lqw = buildQueryWrapper(bo);
        Page<OaClueVo> result = baseMapper.selectVoListPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
