package cn.iocoder.lyzsys.module.tjt.service.employeeyearcostbudget;

import cn.iocoder.lyzsys.framework.common.pojo.PageResult;
import cn.iocoder.lyzsys.framework.common.util.object.BeanUtils;
import cn.iocoder.lyzsys.module.tjt.controller.admin.employeeyearcostbudget.vo.EmployeeYearCostBudgetPageReqVO;
import cn.iocoder.lyzsys.module.tjt.controller.admin.employeeyearcostbudget.vo.EmployeeYearCostBudgetRespVO;
import cn.iocoder.lyzsys.module.tjt.controller.admin.employeeyearcostbudget.vo.EmployeeYearCostBudgetSaveReqVO;
import cn.iocoder.lyzsys.module.tjt.dal.dataobject.employee.EmployeeDO;
import cn.iocoder.lyzsys.module.tjt.dal.dataobject.employeeyearcostbudget.EmployeeYearCostBudgetDO;
import cn.iocoder.lyzsys.module.tjt.dal.mysql.employeeyearcostbudget.EmployeeYearCostBudgetMapper;
import cn.iocoder.lyzsys.module.tjt.service.employee.EmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Objects;

import static cn.iocoder.lyzsys.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.lyzsys.module.tjt.enums.ErrorCodeConstants.EMPLOYEE_YEAR_COST_BUDGET_DUPLICATE;
import static cn.iocoder.lyzsys.module.tjt.enums.ErrorCodeConstants.EMPLOYEE_YEAR_COST_BUDGET_NOT_EXISTS;

@Service
@Validated
public class EmployeeYearCostBudgetServiceImpl implements EmployeeYearCostBudgetService {

    @Resource
    private EmployeeYearCostBudgetMapper employeeYearCostBudgetMapper;
    @Resource
    private EmployeeService employeeService;

    @Override
    public Long createEmployeeYearCostBudget(EmployeeYearCostBudgetSaveReqVO createReqVO) {
        EmployeeDO employee = employeeService.validateEmployeeExists(createReqVO.getEmployeeId());
        validateDuplicate(null, createReqVO.getEmployeeId(), createReqVO.getBudgetYear());
        EmployeeYearCostBudgetDO budget = BeanUtils.toBean(createReqVO, EmployeeYearCostBudgetDO.class);
        budget.setEmployeeName(employee.getEmployeeName());
        if (budget.getEnabledFlag() == null) {
            budget.setEnabledFlag(Boolean.TRUE);
        }
        employeeYearCostBudgetMapper.insert(budget);
        return budget.getId();
    }

    @Override
    public void updateEmployeeYearCostBudget(EmployeeYearCostBudgetSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        EmployeeDO employee = employeeService.validateEmployeeExists(updateReqVO.getEmployeeId());
        validateDuplicate(updateReqVO.getId(), updateReqVO.getEmployeeId(), updateReqVO.getBudgetYear());
        EmployeeYearCostBudgetDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeYearCostBudgetDO.class);
        updateObj.setEmployeeName(employee.getEmployeeName());
        employeeYearCostBudgetMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmployeeYearCostBudget(Long id) {
        validateExists(id);
        employeeYearCostBudgetMapper.deleteById(id);
    }

    @Override
    public EmployeeYearCostBudgetRespVO getEmployeeYearCostBudget(Long id) {
        return BeanUtils.toBean(validateExists(id), EmployeeYearCostBudgetRespVO.class);
    }

    @Override
    public PageResult<EmployeeYearCostBudgetRespVO> getEmployeeYearCostBudgetPage(EmployeeYearCostBudgetPageReqVO pageReqVO) {
        return BeanUtils.toBean(employeeYearCostBudgetMapper.selectPage(pageReqVO), EmployeeYearCostBudgetRespVO.class);
    }

    private EmployeeYearCostBudgetDO validateExists(Long id) {
        EmployeeYearCostBudgetDO budget = employeeYearCostBudgetMapper.selectById(id);
        if (budget == null) {
            throw exception(EMPLOYEE_YEAR_COST_BUDGET_NOT_EXISTS);
        }
        return budget;
    }

    private void validateDuplicate(Long id, Long employeeId, Integer budgetYear) {
        EmployeeYearCostBudgetDO duplicate = employeeYearCostBudgetMapper.selectByEmployeeIdAndBudgetYear(employeeId, budgetYear);
        if (duplicate != null && !Objects.equals(duplicate.getId(), id)) {
            throw exception(EMPLOYEE_YEAR_COST_BUDGET_DUPLICATE);
        }
    }

}
