package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.helper.LoginHelper;
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.OaCustomerBo;
import com.ruoyi.oa.domain.vo.OaCustomerVo;
import com.ruoyi.oa.domain.OaCustomer;
import com.ruoyi.oa.mapper.OaCustomerMapper;
import com.ruoyi.oa.service.IOaCustomerService;

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

/**
 * CRM 客户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@RequiredArgsConstructor
@Service
public class OaCustomerServiceImpl implements IOaCustomerService {

    private final OaCustomerMapper baseMapper;

    /**
     * 查询CRM 客户
     */
    @Override
    public OaCustomerVo queryById(Long customerId){
        return baseMapper.selectVoById(customerId);
    }

    /**
     * 查询CRM 客户列表
     */
    @Override
    public TableDataInfo<OaCustomerVo> queryPageList(OaCustomerBo bo, PageQuery pageQuery) {
        QueryWrapper<OaCustomer> lqw = buildQueryWrapper(bo);
        Page<OaCustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询CRM 客户列表
     */
    @Override
    public List<OaCustomerVo> queryList(OaCustomerBo bo) {
        QueryWrapper<OaCustomer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OaCustomer> buildQueryWrapper(OaCustomerBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaCustomer> lqw = Wrappers.query();
        lqw.like(StringUtils.isNotBlank(bo.getName()), "oc.name", bo.getName());
        lqw.eq(bo.getAreaId() != null, "oc.area_id", bo.getAreaId());
        lqw.eq(bo.getIndustryId() != null, "oc.industry_id", bo.getAreaId());
        lqw.eq(bo.getLevel() != null, "oc.level", bo.getAreaId());
        lqw.eq("oc.del_flag", 0L);
        return lqw;
    }

    /**
     * 新增CRM 客户
     */
    @Override
    public Boolean insertByBo(OaCustomerBo bo) {
        OaCustomer add = BeanUtil.toBean(bo, OaCustomer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomerId(add.getCustomerId());
        }
        return flag;
    }

    /**
     * 修改CRM 客户
     */
    @Override
    public Boolean updateByBo(OaCustomerBo bo) {
        OaCustomer update = BeanUtil.toBean(bo, OaCustomer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public TableDataInfo<OaCustomerVo> queryPageListToUser(OaCustomerBo bo, PageQuery pageQuery) {
        QueryWrapper<OaCustomer> lqw = buildQueryWrapper(bo);
        Page<OaCustomerVo> result = baseMapper.selectVoPageToUser(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<OaCustomerVo> queryPageListToSea(OaCustomerBo bo, PageQuery pageQuery) {
        QueryWrapper<OaCustomer> lqw = buildQueryWrapper(bo);
        Page<OaCustomerVo> result = baseMapper.selectVoPageToUser(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
