package com.cyl.manager.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyl.manager.staff.domain.*;
import com.cyl.manager.staff.mapper.BondTransactionMapper;
import com.cyl.manager.staff.service.IBondTransactionService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.enums.BondOrderStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 人员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-26
 */
@Service
@Slf4j
public class BondTransactionServiceImpl extends ServiceImpl<BondTransactionMapper, BondTransaction> implements IBondTransactionService {

    /**
     * 更新保证金缴纳状态
     *
     * @param payId
     * @param status
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBondStatusByPayId(Long payId, Integer status) {
        BondTransaction bondTransaction = this.list(new QueryWrapper<BondTransaction>().lambda().eq(BondTransaction::getPayId, payId).eq(BondTransaction::getStatus, BondOrderStatus.UN_PAY.getType())).stream().findFirst().orElse(null);
        if (bondTransaction != null) {
            BondTransaction updateBond = new BondTransaction();
            updateBond.setId(bondTransaction.getId());
            updateBond.setStatus(status);
            return this.updateById(updateBond);
        } else {
            log.error("bondTransaction未找到,payId={}", payId);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addBondTransaction(BondTransaction bondTransaction) {
        return this.save(bondTransaction);
    }

    @Override
    public List<BondTransaction> getStaffBondList(Long staffId, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        LambdaQueryWrapper<BondTransaction> bondTransactionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (staffId != null) {
            bondTransactionLambdaQueryWrapper.eq(BondTransaction::getStaffId, bondTransactionLambdaQueryWrapper);
        }
        bondTransactionLambdaQueryWrapper.orderByDesc(BondTransaction::getCreateTme);
        return this.getBaseMapper().selectList(bondTransactionLambdaQueryWrapper);
    }

}
