package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.maintain.common.enums.IsDeleteStatus;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.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.maintain.business.domain.bo.ErpClientBo;
import com.maintain.business.domain.vo.ErpClientVo;
import com.maintain.business.domain.ErpClient;
import com.maintain.business.mapper.ErpClientMapper;
import com.maintain.business.service.IErpClientService;

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

/**
 * 客户信息-客户Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-25
 */
@RequiredArgsConstructor
@Service
public class ErpClientServiceImpl implements IErpClientService {

    private final ErpClientMapper baseMapper;

    /**
     * 查询客户信息-客户
     */
    @Override
    public ErpClientVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户信息-客户列表
     */
    @Override
    public TableDataInfo<ErpClientVo> queryPageList(ErpClientBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpClient> lqw = buildQueryWrapper(bo);
        Page<ErpClientVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户信息-客户列表
     */
    @Override
    public List<ErpClientVo> queryList(ErpClientBo bo) {
        LambdaQueryWrapper<ErpClient> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpClient> buildQueryWrapper(ErpClientBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpClient> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpClient::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), ErpClient::getContact, bo.getContact());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), ErpClient::getPhone, bo.getPhone());
        lqw.eq(bo.getType() != null, ErpClient::getType, bo.getType());
        lqw.eq(bo.getCertificateType() != null, ErpClient::getCertificateType, bo.getCertificateType());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificateNumber()), ErpClient::getCertificateNumber, bo.getCertificateNumber());
        lqw.eq(bo.getBirthDate() != null, ErpClient::getBirthDate, bo.getBirthDate());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), ErpClient::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getOccupation()), ErpClient::getOccupation, bo.getOccupation());
        lqw.eq(StringUtils.isNotBlank(bo.getMailbox()), ErpClient::getMailbox, bo.getMailbox());
        lqw.eq(bo.getCreateTime() != null, ErpClient::getCreateTime, bo.getCreateTime());
        lqw.eq(ErpClient::getState, IsDeleteStatus.NO.getCode());
        return lqw;
    }

    /**
     * 新增客户信息-客户
     */
    @Override
    public Boolean insertByBo(ErpClientBo bo) {
        ErpClient add = BeanUtil.toBean(bo, ErpClient.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息-客户
     */
    @Override
    public Boolean updateByBo(ErpClientBo bo) {
        ErpClient update = BeanUtil.toBean(bo, ErpClient.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除客户信息-客户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        LambdaUpdateWrapper<ErpClient> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ErpClient::getState, IsDeleteStatus.YES.getCode());
        wrapper.in(ErpClient::getId, ids);
        return baseMapper.update(wrapper) > 0;
    }
}
