package org.dromara.web.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.dromara.web.domain.Settlement;
import org.dromara.web.domain.bo.SettlementBo;
import org.dromara.web.domain.vo.SettlementVo;
import org.dromara.web.mapper.SettlementMapper;
import org.dromara.web.service.ISettlementService;
import org.springframework.stereotype.Service;

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

/**
 * 结算管理Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SettlementServiceImpl implements ISettlementService {

    private final SettlementMapper baseMapper;

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

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

    /**
     * 查询符合条件的结算管理列表
     *
     * @param bo 查询条件
     * @return 结算管理列表
     */
    @Override
    public List<SettlementVo> queryList(SettlementBo bo) {
        LambdaQueryWrapper<Settlement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Settlement> buildQueryWrapper(SettlementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Settlement> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Settlement::getId);
        lqw.eq(bo.getOrderTime() != null, Settlement::getOrderTime, bo.getOrderTime());
        lqw.eq(StringUtils.isNotBlank(bo.getVisitType()), Settlement::getVisitType, bo.getVisitType());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeType()), Settlement::getChargeType, bo.getChargeType());
        lqw.eq(bo.getPatientId() != null, Settlement::getPatientId, bo.getPatientId());
        lqw.like(StringUtils.isNotBlank(bo.getPatientName()), Settlement::getPatientName, bo.getPatientName());
        lqw.eq(StringUtils.isNotBlank(bo.getPatientNo()), Settlement::getPatientNo, bo.getPatientNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Settlement::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), Settlement::getIdCard, bo.getIdCard());
        lqw.eq(bo.getReceivableAmount() != null, Settlement::getReceivableAmount, bo.getReceivableAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentStatus()), Settlement::getPaymentStatus, bo.getPaymentStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentMethod()), Settlement::getPaymentMethod, bo.getPaymentMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Settlement::getStatus, bo.getStatus());
        return lqw;
    }

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

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

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

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