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

import cn.ezhizao.project.business.deduction.domain.BizDeduction;
import cn.ezhizao.project.business.deduction.mapper.BizDeductionMapper;
import cn.ezhizao.project.business.deduction.service.IBizDeductionService;
import cn.ezhizao.project.system.domain.SysFileStorage;
import cn.ezhizao.project.system.service.ISysFileStorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 王建
 */

@Service
public class BizDeductionServiceImpl extends ServiceImpl<BizDeductionMapper, BizDeduction> implements IBizDeductionService {

    @Resource
    BizDeductionMapper deductionMapper;
    @Resource
    ISysFileStorageService fileStorageService;

    @Override
    public List<BizDeduction> getList(Map<String,Object> conditions) {
        return deductionMapper.getList(conditions);
    }

    @Override
    public List<BizDeduction> getListByBean(BizDeduction deduction) {
        return deductionMapper.getList(deduction);
    }

    @Override
    public int getTotal(Map<String,Object> conditions) {
        return deductionMapper.getTotal(conditions);
    }

    @Override
    public boolean save(BizDeduction deduction) {
        if (deduction.getFiles() == null || deduction.getFiles().isEmpty()) {
            return super.save(deduction);
        } else {
            return save(deduction, deduction.getFiles());
        }
    }
    private boolean save(BizDeduction deduction, List<SysFileStorage> files) {
        boolean result = super.save(deduction);
        result = result && fileStorageService.saveBatch(files);
        return result;
    }
    @Override
    public boolean updateById(BizDeduction deduction) {
        if (deduction.getFiles() == null || deduction.getFiles().isEmpty()) {
            return super.updateById(deduction);
        } else {
            return updateById(deduction, deduction.getFiles());
        }
    }

    private boolean updateById(BizDeduction deduction, List<SysFileStorage> files) {
        boolean result = super.updateById(deduction);
        SysFileStorage delConditions = new SysFileStorage();
        delConditions.setMasterTableName("biz_deduction");
        delConditions.setMasterId(deduction.getId());
        fileStorageService.physicalDelete(delConditions);
        result = result && fileStorageService.saveBatch(files);
        return result;
    }

}
