package cn.ezhizao.project.business.deposit.service.impl;

import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;

import cn.ezhizao.project.system.domain.SysFileStorage;
import cn.ezhizao.project.system.service.ISysFileStorageService;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.ezhizao.project.business.deposit.mapper.BizDepositMapper;
import cn.ezhizao.project.business.deposit.domain.BizDeposit;
import cn.ezhizao.project.business.deposit.service.IBizDepositService;

/**
 * 存款Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@Service
public class BizDepositServiceImpl extends ServiceImpl<BizDepositMapper, BizDeposit> implements IBizDepositService
{
    @Resource
    private BizDepositMapper bizDepositMapper;

    @Resource
    private ISysFileStorageService sysFileStorageService;

    /**
     * 查询存款列表
     *
     * @param bizDeposit 存款
     * @return 存款
     */
    @Override
    public List<BizDeposit> getList(BizDeposit bizDeposit)
    {
        return bizDepositMapper.getList(bizDeposit);
    }

    /**
     * 物理删除
     * @param bizDeposit
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizDeposit bizDeposit){ return bizDepositMapper.physicalDelete(bizDeposit); }

    @Override
    public BigDecimal getTotalRemain(BizDeposit bizDeposit) {
        return bizDepositMapper.getTotalRemain(bizDeposit);
    }

    @Override
    public BigDecimal getDonateRemain(BizDeposit bizDeposit) {
        return bizDepositMapper.getDonateRemain(bizDeposit);
    }

    @Override
    public boolean save(BizDeposit bizDeposit) {
        bizDeposit.setDepositDate(bizDeposit.getDepositDate() == null? DateTime.now().toSqlDate() : bizDeposit.getDepositDate());
        boolean status = super.save(bizDeposit);
        List<SysFileStorage> sysFileStorage = bizDeposit.getFiles();
        if (sysFileStorage != null) {
            sysFileStorage.forEach(l -> {
                l.setMasterId(bizDeposit.getId());
                l.setMasterTableName("biz_deposit");
            });
        }
        status = status && sysFileStorageService.saveBatch(sysFileStorage);
        return status;
    }

    @Override
    public boolean updateById(BizDeposit bizDeposit) {
        boolean status = super.updateById(bizDeposit);
        List<SysFileStorage> sysFileStorage = bizDeposit.getFiles();
        if (sysFileStorage != null) {
            sysFileStorage.forEach(l -> {
                l.setMasterId(bizDeposit.getId());
                l.setMasterTableName("biz_deposit");
            });
        }
        SysFileStorage delCondition = new SysFileStorage();
        delCondition.setMasterTableName("biz_deposit");
        delCondition.setMasterId(bizDeposit.getId());
        sysFileStorageService.physicalDelete(delCondition);
        status = status && (sysFileStorage == null || sysFileStorage.isEmpty() || sysFileStorageService.saveBatch(sysFileStorage));
        return status;
    }
}
