/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.dzfp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.dzfp.entity.EiKprpz;
import com.tofly.feesapi.dzfp.mapper.EiKprpzMapper;
import com.tofly.feesapi.dzfp.service.EiKprpzService;
import org.springframework.stereotype.Service;

/**
 * 开票人配置
 *
 * @author tofly code generator
 * @date Tue Apr 28 00:00:00 CST 2026
 */
@Service("eiKprpzService")
public class EiKprpzServiceImpl extends ServiceImpl<EiKprpzMapper, EiKprpz> implements EiKprpzService {

    /**
     * 结转开票人
     */
    @Override
    public void Jzkpr() {
        LambdaQueryWrapper<EiKprpz> wrapper = Wrappers.<EiKprpz>lambdaQuery()
                .eq(EiKprpz::getIsdisabled, "1");
        EiKprpz eiKprpz = this.baseMapper.selectOne(wrapper);
        long xh = eiKprpz.getXh();
        long count = this.lambdaQuery()
                .eq(EiKprpz::getXh, xh + 1)
                .count();

        //第一步禁用目前启用的
        this.lambdaUpdate()
                .set(EiKprpz::getIsdisabled, "0")
                .eq(EiKprpz::getXh, xh)
                .update();
        //第二步下一个序号是否存在
        if (count > 0) {
            this.lambdaUpdate()
                    .set(EiKprpz::getIsdisabled, "1")
                    .eq(EiKprpz::getXh, xh + 1)
                    .update();
        } else {
            this.lambdaUpdate()
                    .set(EiKprpz::getIsdisabled, "1")
                    .eq(EiKprpz::getXh, 1)
                    .update();
        }

    }
}
