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.OaFurnitureTableBo;
import com.ruoyi.oa.domain.vo.OaFurnitureTableVo;
import com.ruoyi.oa.domain.OaFurnitureTable;
import com.ruoyi.oa.mapper.OaFurnitureTableMapper;
import com.ruoyi.oa.service.IOaFurnitureTableService;

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

/**
 * 家具Service业务层处理
 *
 * @author liujingchao
 * @date 2025-07-08
 */
@RequiredArgsConstructor
@Service
public class OaFurnitureTableServiceImpl implements IOaFurnitureTableService {

    private final OaFurnitureTableMapper baseMapper;

    /**
     * 查询家具
     */
    @Override
    public OaFurnitureTableVo queryById(Long furnitureId){
        return baseMapper.selectVoById(furnitureId);
    }

    /**
     * 查询家具列表
     */
    @Override
    public TableDataInfo<OaFurnitureTableVo> queryPageList(OaFurnitureTableBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaFurnitureTable> lqw = buildQueryWrapper(bo);
        Page<OaFurnitureTableVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询家具列表
     */
    @Override
    public List<OaFurnitureTableVo> queryList(OaFurnitureTableBo bo) {
        LambdaQueryWrapper<OaFurnitureTable> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaFurnitureTable> buildQueryWrapper(OaFurnitureTableBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaFurnitureTable> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), OaFurnitureTable::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), OaFurnitureTable::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getProcurementOfProducts()), OaFurnitureTable::getProcurementOfProducts, bo.getProcurementOfProducts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactGroup()), OaFurnitureTable::getContactGroup, bo.getContactGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), OaFurnitureTable::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getFax()), OaFurnitureTable::getFax, bo.getFax());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), OaFurnitureTable::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getZip()), OaFurnitureTable::getZip, bo.getZip());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), OaFurnitureTable::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getWebsite()), OaFurnitureTable::getWebsite, bo.getWebsite());
        lqw.eq(bo.getEmailSendCount() != null, OaFurnitureTable::getEmailSendCount, bo.getEmailSendCount());
        lqw.eq(bo.getLastEmailSendTime() != null, OaFurnitureTable::getLastEmailSendTime, bo.getLastEmailSendTime());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPerson()), OaFurnitureTable::getContactPerson, bo.getContactPerson());
        lqw.eq(bo.getReceiveCount() != null, OaFurnitureTable::getReceiveCount, bo.getReceiveCount());
        
        // 优质筛选：只查询邮箱不为空的记录
        if (Boolean.TRUE.equals(bo.getQualityFilter())) {
            lqw.isNotNull(OaFurnitureTable::getEmail);
            lqw.ne(OaFurnitureTable::getEmail, "");
        }
        
        // 按邮件发送次数升序排序，发送次数少的靠前显示
        lqw.orderByAsc(OaFurnitureTable::getEmailSendCount);
        return lqw;
    }

    /**
     * 新增家具
     */
    @Override
    public Boolean insertByBo(OaFurnitureTableBo bo) {
        OaFurnitureTable add = BeanUtil.toBean(bo, OaFurnitureTable.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFurnitureId(add.getFurnitureId());
        }
        return flag;
    }

    /**
     * 修改家具
     */
    @Override
    public Boolean updateByBo(OaFurnitureTableBo bo) {
        OaFurnitureTable update = BeanUtil.toBean(bo, OaFurnitureTable.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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