package com.ruoyi.bst.creditUser.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.creditUser.domain.CreditUser;
import com.ruoyi.bst.creditUser.domain.CreditUserQuery;
import com.ruoyi.bst.creditUser.domain.CreditUserVO;
import com.ruoyi.bst.creditUser.mapper.CreditUserMapper;
import com.ruoyi.bst.creditUser.service.CreditUserService;
import com.ruoyi.bst.creditUser.service.CreditUserValidator;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 挂账用户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-28
 */
@Service
public class CreditUserServiceImpl implements CreditUserService
{
    @Autowired
    private CreditUserMapper creditUserMapper;

    @Autowired
    private CreditUserValidator creditUserValidator;

    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询挂账用户
     *
     * @param id 挂账用户主键
     * @return 挂账用户
     */
    @Override
    public CreditUserVO selectCreditUserById(Long id, boolean scope)
    {
        if (id == null) {
            return null;
        }
        CreditUserQuery query = new CreditUserQuery();
        query.setId(id);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.CREDIT_USER_VIEW.getCode());
        return this.selectOne(query);
    }

    private CreditUserVO selectOne(CreditUserQuery query) {
        PageHelper.startPage(1, 1);
        List<CreditUserVO> list = this.selectCreditUserList(query);
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询挂账用户列表
     *
     * @param creditUser 挂账用户
     * @return 挂账用户
     */
    @Override
    public List<CreditUserVO> selectCreditUserList(CreditUserQuery creditUser)
    {
        return creditUserMapper.selectCreditUserList(creditUser);
    }

    /**
     * 新增挂账用户
     *
     * @param creditUser 挂账用户
     * @return 结果
     */
    @Override
    public int insertCreditUser(CreditUser creditUser)
    {
        creditUser.setCreateTime(DateUtils.getNowDate());

        Integer result = transactionTemplate.execute(status -> {
            int rows = creditUserMapper.insertCreditUser(creditUser);
            if (rows > 0) {
                creditUserValidator.validate(creditUser.getId());
            }
            return rows;
        });
        return result == null ? 0 : result;
    }

    /**
     * 修改挂账用户
     *
     * @param creditUser 挂账用户
     * @return 结果
     */
    @Override
    public int updateCreditUser(CreditUser creditUser)
    {
        Integer result = transactionTemplate.execute(status -> {
            int rows = creditUserMapper.updateCreditUser(creditUser);
            if (rows > 0) {
                creditUserValidator.validate(creditUser.getId());
            }   
            return rows;
        });
        return result == null ? 0 : result;
    }

    /**
     * 批量删除挂账用户
     *
     * @param ids 需要删除的挂账用户主键
     * @return 结果
     */
    @Override
    public int deleteCreditUserByIds(List<Long> ids)
    {
        return creditUserMapper.deleteCreditUserByIds(ids);
    }

    @Override
    public int addUsedAmount(Long id, BigDecimal amount) {
        if (id == null || amount == null) {
            return 0;
        }
        if (MathUtils.equals(amount, BigDecimal.ZERO)) {
            return 1;
        }
        return creditUserMapper.addUsedAmount(id, amount);
    }
}
