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

import cn.ezhizao.project.business.socialSecurity.domain.BizSocialSecurityConfirmDetail;
import cn.ezhizao.project.business.socialSecurity.domain.BizSocialSecurityConfirmDetailEmployee;
import cn.ezhizao.project.business.socialSecurity.mapper.BizSocialSecurityConfirmDetailMapper;
import cn.ezhizao.project.business.socialSecurity.service.IBizSocialSecurityConfirmDetailEmployeeService;
import cn.ezhizao.project.business.socialSecurity.service.IBizSocialSecurityConfirmDetailService;
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 BizSocialSecurityConfirmDetailServiceImpl extends ServiceImpl<BizSocialSecurityConfirmDetailMapper, BizSocialSecurityConfirmDetail> implements IBizSocialSecurityConfirmDetailService {

    @Resource
    BizSocialSecurityConfirmDetailMapper financialSocialSecurityConfirmDetailDao;
    @Resource
    IBizSocialSecurityConfirmDetailEmployeeService financialSocialSecurityConfirmDetailEmployeeService;

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

    @Override
    public List<BizSocialSecurityConfirmDetail> getListByBean(BizSocialSecurityConfirmDetail financialSocialSecurityConfirmDetail) {
        return financialSocialSecurityConfirmDetailDao.getList(financialSocialSecurityConfirmDetail);
    }

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

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

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

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

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

        employees.forEach(l -> {
            l.setSocialSecurityConfirmId(entity.getSocialSecurityConfirmId());
            l.setSocialSecurityConfirmDetailId(entity.getId());
        });
        bool = bool && (employees.isEmpty() || financialSocialSecurityConfirmDetailEmployeeService.saveBatch(employees));
        return bool;
    }

    private boolean save(BizSocialSecurityConfirmDetail entity, List<BizSocialSecurityConfirmDetailEmployee> employees) {
        boolean bool = super.save(entity);
        employees.forEach(l -> {
            l.setSocialSecurityConfirmId(entity.getSocialSecurityConfirmId());
            l.setSocialSecurityConfirmDetailId(entity.getId());
        });
        bool = bool && (employees.isEmpty() || financialSocialSecurityConfirmDetailEmployeeService.saveBatch(employees));
        return bool;
    }

}
