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

import cn.ezhizao.project.business.housingFund.domain.BizHousingFundConfirmDetail;
import cn.ezhizao.project.business.housingFund.domain.BizHousingFundConfirmDetailEmployee;
import cn.ezhizao.project.business.housingFund.mapper.BizHousingFundConfirmDetailMapper;
import cn.ezhizao.project.business.housingFund.service.IBizHousingFundConfirmDetailEmployeeService;
import cn.ezhizao.project.business.housingFund.service.IBizHousingFundConfirmDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 周晓
 */

@Service
public class BizHousingFundConfirmDetailServiceImpl extends ServiceImpl<BizHousingFundConfirmDetailMapper, BizHousingFundConfirmDetail> implements IBizHousingFundConfirmDetailService {

    @Resource
    BizHousingFundConfirmDetailMapper financialHousingFundConfirmDetailDao;
    @Resource
    IBizHousingFundConfirmDetailEmployeeService financialHousingFundConfirmDetailEmployeeService;

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

    @Override
    public List<BizHousingFundConfirmDetail> getListByBean(BizHousingFundConfirmDetail financialHousingFundConfirmDetail) {
        return financialHousingFundConfirmDetailDao.getList(financialHousingFundConfirmDetail);
    }

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

    @Override
    public List<BizHousingFundConfirmDetail> getList2(Map<String, Object> conditions) {
        return financialHousingFundConfirmDetailDao.getList2(conditions);
    }

    @Override
    @Transactional
    public boolean save(BizHousingFundConfirmDetail entity) {
        List<BizHousingFundConfirmDetailEmployee> employees = entity.getDetails();
        return this.save(entity, employees);
    }

    @Override
    @Transactional
    public boolean updateById(BizHousingFundConfirmDetail entity) {
        List<BizHousingFundConfirmDetailEmployee> employees = entity.getDetails();
        return this.updateById(entity, employees);
    }

    private boolean updateById(BizHousingFundConfirmDetail entity, List<BizHousingFundConfirmDetailEmployee> employees) {
        boolean bool = super.updateById(entity);

        Map<String, Object> delDetail = new HashMap<>();
        delDetail.put("housingFundConfirmDetailId", entity.getId());
        financialHousingFundConfirmDetailEmployeeService.physicallyDelete(delDetail);

        employees.forEach(l -> {
            l.setHousingFundConfirmId(entity.getHousingFundConfirmId());
            l.setHousingFundConfirmDetailId(entity.getId());
            l.setCompanyId(entity.getCompanyId());
        });
        bool = bool && (employees.isEmpty() || financialHousingFundConfirmDetailEmployeeService.saveBatch(employees));
        return bool;
    }

    private boolean save(BizHousingFundConfirmDetail entity, List<BizHousingFundConfirmDetailEmployee> employees) {
        boolean bool = super.save(entity);
        employees.forEach(l -> {
            l.setHousingFundConfirmId(entity.getHousingFundConfirmId());
            l.setHousingFundConfirmDetailId(entity.getId());
            l.setCompanyId(entity.getCompanyId());
        });
        bool = bool && (employees.isEmpty() || financialHousingFundConfirmDetailEmployeeService.saveBatch(employees));
        return bool;
    }

}
