package com.mortals.xhx.module.attendance.service.impl;
import com.mortals.xhx.module.attendance.model.AttendanceVacationBalanceQuery;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.attendance.dao.AttendanceVacationBalanceDao;
import com.mortals.xhx.module.attendance.model.AttendanceVacationBalanceEntity;
import com.mortals.xhx.module.attendance.service.AttendanceVacationBalanceService;
/**
* AttendanceVacationBalanceService
* 员工假期余额信息 service实现
*
* @author zxfei
* @date 2023-04-07
*/
@Service("attendanceVacationBalanceService")
public class AttendanceVacationBalanceServiceImpl extends AbstractCRUDServiceImpl<AttendanceVacationBalanceDao, AttendanceVacationBalanceEntity, Long> implements AttendanceVacationBalanceService {

    @Autowired
    private StaffService staffService;

    @Override
    protected void saveBefore(AttendanceVacationBalanceEntity entity, Context context) throws AppException {
        super.saveBefore(entity,context);
        if(entity.getStaffId()==null){
            throw new AppException("员工ID不能为空");
        }
        StaffEntity staff = staffService.get(entity.getStaffId());
        if(staff==null){
            throw new AppException("员工ID不正确");
        }
        entity.setStaffName(staff.getName());
        entity.setDeptId(staff.getDeptId());
        entity.setDeptName(staff.getDeptName());

    }

    @Override
    public AttendanceVacationBalanceEntity save(AttendanceVacationBalanceEntity entity, Context context) throws AppException {
        this.saveBefore(entity, context);
        AttendanceVacationBalanceEntity temp = this.selectOne(new AttendanceVacationBalanceQuery().staffId(entity.getStaffId()));
        int iRet = 0;
        if(temp!=null){
            entity.setId(temp.getId());
            iRet = this.dao.update(entity);
        }else {
            iRet = this.dao.insert(entity);
        }
        if (iRet == 0) {
            throw new AppException(-1001, "写入数据库失败！");
        } else {
            this.saveAfter(entity, context);
            return entity;
        }
    }
}