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

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.hotwj.platform.operationManagement.fundTradeOrder.domain.bo.HotFundTradeOrderBo;
import com.hotwj.platform.operationManagement.fundTradeOrder.domain.vo.HotFundTradeOrderVo;
import com.hotwj.platform.operationManagement.fundTradeOrder.domain.HotFundTradeOrder;
import com.hotwj.platform.operationManagement.fundTradeOrder.mapper.HotFundTradeOrderMapper;
import com.hotwj.platform.operationManagement.fundTradeOrder.service.IHotFundTradeOrderService;

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

/**
 * 订单管理Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotFundTradeOrderServiceImpl implements IHotFundTradeOrderService {

    private final HotFundTradeOrderMapper baseMapper;

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

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

    /**
     * 查询符合条件的订单管理列表
     *
     * @param bo 查询条件
     * @return 订单管理列表
     */
    @Override
    public List<HotFundTradeOrderVo> queryList(HotFundTradeOrderBo bo) {
        LambdaQueryWrapper<HotFundTradeOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotFundTradeOrder> buildQueryWrapper(HotFundTradeOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotFundTradeOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotFundTradeOrder::getId);
        lqw.eq(bo.getCompanyId() != null, HotFundTradeOrder::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getAccountId() != null, HotFundTradeOrder::getAccountId, bo.getAccountId());
        lqw.eq(bo.getUserId() != null, HotFundTradeOrder::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), HotFundTradeOrder::getUserName, bo.getUserName());
        lqw.eq(bo.getConsumerCompanyId() != null, HotFundTradeOrder::getConsumerCompanyId, bo.getConsumerCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getConsumerCompanyName()), HotFundTradeOrder::getConsumerCompanyName, bo.getConsumerCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), HotFundTradeOrder::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderType() != null, HotFundTradeOrder::getOrderType, bo.getOrderType());
        lqw.eq(bo.getBizScene() != null, HotFundTradeOrder::getBizScene, bo.getBizScene());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), HotFundTradeOrder::getProductName, bo.getProductName());
        lqw.eq(bo.getPackageId() != null, HotFundTradeOrder::getPackageId, bo.getPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotFundTradeOrder::getPackageName, bo.getPackageName());
        lqw.eq(bo.getHourType() != null, HotFundTradeOrder::getHourType, bo.getHourType());
        lqw.eq(bo.getHourCount() != null, HotFundTradeOrder::getHourCount, bo.getHourCount());
        lqw.eq(bo.getSmsPackageId() != null, HotFundTradeOrder::getSmsPackageId, bo.getSmsPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getSmsPackageName()), HotFundTradeOrder::getSmsPackageName, bo.getSmsPackageName());
        lqw.eq(bo.getSmsCount() != null, HotFundTradeOrder::getSmsCount, bo.getSmsCount());
        lqw.eq(bo.getPreJobPackageId() != null, HotFundTradeOrder::getPreJobPackageId, bo.getPreJobPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getPreJobPackageName()), HotFundTradeOrder::getPreJobPackageName, bo.getPreJobPackageName());
        lqw.eq(bo.getPreJobActivityId() != null, HotFundTradeOrder::getPreJobActivityId, bo.getPreJobActivityId());
        lqw.like(StringUtils.isNotBlank(bo.getPreJobActivityName()), HotFundTradeOrder::getPreJobActivityName, bo.getPreJobActivityName());
        lqw.eq(bo.getUnitPrice() != null, HotFundTradeOrder::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getQuantity() != null, HotFundTradeOrder::getQuantity, bo.getQuantity());
        lqw.eq(bo.getPayableAmount() != null, HotFundTradeOrder::getPayableAmount, bo.getPayableAmount());
        lqw.eq(bo.getDiscountAmount() != null, HotFundTradeOrder::getDiscountAmount, bo.getDiscountAmount());
        lqw.eq(bo.getRefundAmount() != null, HotFundTradeOrder::getRefundAmount, bo.getRefundAmount());
        lqw.eq(bo.getPaidAmount() != null, HotFundTradeOrder::getPaidAmount, bo.getPaidAmount());
        lqw.eq(bo.getPayMethod() != null, HotFundTradeOrder::getPayMethod, bo.getPayMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getThirdTradeNo()), HotFundTradeOrder::getThirdTradeNo, bo.getThirdTradeNo());
        lqw.eq(bo.getOrderCreateTime() != null, HotFundTradeOrder::getOrderCreateTime, bo.getOrderCreateTime());
        lqw.eq(bo.getPayTime() != null, HotFundTradeOrder::getPayTime, bo.getPayTime());
        lqw.eq(bo.getPayStatus() != null, HotFundTradeOrder::getPayStatus, bo.getPayStatus());
        lqw.eq(bo.getRefundStatus() != null, HotFundTradeOrder::getRefundStatus, bo.getRefundStatus());
        lqw.eq(bo.getRefundTime() != null, HotFundTradeOrder::getRefundTime, bo.getRefundTime());
        lqw.eq(bo.getCancelTime() != null, HotFundTradeOrder::getCancelTime, bo.getCancelTime());
        lqw.eq(bo.getCloseTime() != null, HotFundTradeOrder::getCloseTime, bo.getCloseTime());
        lqw.eq(bo.getContractId() != null, HotFundTradeOrder::getContractId, bo.getContractId());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNo()), HotFundTradeOrder::getContractNo, bo.getContractNo());
        lqw.eq(bo.getContractStatus() != null, HotFundTradeOrder::getContractStatus, bo.getContractStatus());
        lqw.eq(bo.getContractSignTime() != null, HotFundTradeOrder::getContractSignTime, bo.getContractSignTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSource()), HotFundTradeOrder::getOrderSource, bo.getOrderSource());
        lqw.eq(StringUtils.isNotBlank(bo.getProductSnapshot()), HotFundTradeOrder::getProductSnapshot, bo.getProductSnapshot());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotFundTradeOrder::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotFundTradeOrder::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotFundTradeOrder::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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