package com.hotwj.platform.operationManagement.invoiceManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.operationManagement.invoiceManage.domain.HotInvoiceManage;
import com.hotwj.platform.operationManagement.invoiceManage.domain.bo.HotInvoiceManageBo;
import com.hotwj.platform.operationManagement.invoiceManage.domain.vo.HotInvoiceManageVo;
import com.hotwj.platform.operationManagement.invoiceManage.mapper.HotInvoiceManageMapper;
import com.hotwj.platform.operationManagement.invoiceManage.service.IHotInvoiceManageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 发票管理Service业务层处理
 *
 * @author shihongwei
 * @date 2026-05-06
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotInvoiceManageServiceImpl implements IHotInvoiceManageService {

    private final HotInvoiceManageMapper baseMapper;

    /**
     * 查询发票管理
     *
     * @param id 主键
     * @return 发票管理
     */
    @Override
    public HotInvoiceManageVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询发票管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 发票管理分页列表
     */
    @Override
    public TableDataInfo<HotInvoiceManageVo> queryPageList(HotInvoiceManageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotInvoiceManage> lqw = buildQueryWrapper(bo);
        Page<HotInvoiceManageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的发票管理列表
     *
     * @param bo 查询条件
     * @return 发票管理列表
     */
    @Override
    public List<HotInvoiceManageVo> queryList(HotInvoiceManageBo bo) {
        LambdaQueryWrapper<HotInvoiceManage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotInvoiceManage> buildQueryWrapper(HotInvoiceManageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotInvoiceManage> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotInvoiceManage::getId);
        lqw.eq(bo.getCompanyId() != null, HotInvoiceManage::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getOrderId() != null, HotInvoiceManage::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), HotInvoiceManage::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getBuyerCompanyId() != null, HotInvoiceManage::getBuyerCompanyId, bo.getBuyerCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getBuyerCompanyName()), HotInvoiceManage::getBuyerCompanyName, bo.getBuyerCompanyName());
        lqw.eq(bo.getOrderType() != null, HotInvoiceManage::getOrderType, bo.getOrderType());
        lqw.eq(bo.getOrderAmount() != null, HotInvoiceManage::getOrderAmount, bo.getOrderAmount());
        lqw.eq(bo.getPayMethod() != null, HotInvoiceManage::getPayMethod, bo.getPayMethod());
        lqw.eq(bo.getPayTime() != null, HotInvoiceManage::getPayTime, bo.getPayTime());
        lqw.eq(bo.getInvoiceAmount() != null, HotInvoiceManage::getInvoiceAmount, bo.getInvoiceAmount());
        lqw.eq(bo.getInvoiceType() != null, HotInvoiceManage::getInvoiceType, bo.getInvoiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoiceTitle()), HotInvoiceManage::getInvoiceTitle, bo.getInvoiceTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxNo()), HotInvoiceManage::getTaxNo, bo.getTaxNo());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), HotInvoiceManage::getBankName, bo.getBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAccount()), HotInvoiceManage::getBankAccount, bo.getBankAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyAddress()), HotInvoiceManage::getCompanyAddress, bo.getCompanyAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyPhone()), HotInvoiceManage::getCompanyPhone, bo.getCompanyPhone());
        lqw.eq(bo.getInvoiceStatus() != null, HotInvoiceManage::getInvoiceStatus, bo.getInvoiceStatus());
        lqw.eq(bo.getApplyTime() != null, HotInvoiceManage::getApplyTime, bo.getApplyTime());
        lqw.eq(bo.getInvoiceTime() != null, HotInvoiceManage::getInvoiceTime, bo.getInvoiceTime());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoiceCode()), HotInvoiceManage::getInvoiceCode, bo.getInvoiceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoiceNo()), HotInvoiceManage::getInvoiceNo, bo.getInvoiceNo());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoiceUrl()), HotInvoiceManage::getInvoiceUrl, bo.getInvoiceUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getRejectReason()), HotInvoiceManage::getRejectReason, bo.getRejectReason());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotInvoiceManage::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotInvoiceManage::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotInvoiceManage::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增发票管理
     *
     * @param bo 发票管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotInvoiceManageBo bo) {
        HotInvoiceManage add = MapstructUtils.convert(bo, HotInvoiceManage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发票管理
     *
     * @param bo 发票管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotInvoiceManageBo bo) {
        HotInvoiceManage update = MapstructUtils.convert(bo, HotInvoiceManage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除发票管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
