package com.hzy.project.business.salary.service.impl;

import java.util.List;
import com.hzy.common.utils.DateUtils;
import com.hzy.project.business.salary.domain.SalaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hzy.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hzy.project.business.salary.domain.SalaryDetail;
import com.hzy.project.business.salary.mapper.SalaryMapper;
import com.hzy.project.business.salary.domain.Salary;
import com.hzy.project.business.salary.service.ISalaryService;
import com.hzy.common.utils.text.Convert;

/**
 * 工资Service业务层处理
 * 
 * @author ruoyi
 * @date 2026-01-02
 */
@Service
public class SalaryServiceImpl implements ISalaryService 
{
    @Autowired
    private SalaryMapper salaryMapper;

    /**
     * 查询工资
     * 
     * @param salaryId 工资主键
     * @return 工资
     */
    @Override
    public Salary selectSalaryBySalaryId(Long salaryId)
    {
        return salaryMapper.selectSalaryBySalaryId(salaryId);
    }

    /**
     * 查询工资列表
     * 
     * @param salary 工资
     * @return 工资
     */
    @Override
    public List<SalaryVo> selectSalaryList(SalaryVo salary)
    {
        return salaryMapper.selectSalaryList(salary);
    }

    /**
     * 新增工资
     * 
     * @param salary 工资
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSalary(Salary salary)
    {
        salary.setCreateTime(DateUtils.getNowDate());
        int rows = salaryMapper.insertSalary(salary);
        insertSalaryDetail(salary);
        return rows;
    }

    /**
     * 修改工资
     * 
     * @param salary 工资
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSalary(Salary salary)
    {
        salary.setUpdateTime(DateUtils.getNowDate());
        salaryMapper.deleteSalaryDetailBySalaryId(salary.getSalaryId());
        insertSalaryDetail(salary);
        return salaryMapper.updateSalary(salary);
    }

    /**
     * 批量删除工资
     * 
     * @param salaryIds 需要删除的工资主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalaryBySalaryIds(String salaryIds)
    {
        salaryMapper.deleteSalaryDetailBySalaryIds(Convert.toStrArray(salaryIds));
        return salaryMapper.deleteSalaryBySalaryIds(Convert.toStrArray(salaryIds));
    }

    /**
     * 删除工资信息
     * 
     * @param salaryId 工资主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalaryBySalaryId(Long salaryId)
    {
        salaryMapper.deleteSalaryDetailBySalaryId(salaryId);
        return salaryMapper.deleteSalaryBySalaryId(salaryId);
    }

    /**
     * 新增工资明细信息
     * 
     * @param salary 工资对象
     */
    public void insertSalaryDetail(Salary salary)
    {
        List<SalaryDetail> salaryDetailList = salary.getSalaryDetailList();
        Long salaryId = salary.getSalaryId();
        if (StringUtils.isNotNull(salaryDetailList))
        {
            List<SalaryDetail> list = new ArrayList<SalaryDetail>();
            for (SalaryDetail salaryDetail : salaryDetailList)
            {
                salaryDetail.setSalaryId(salaryId);
                list.add(salaryDetail);
            }
            if (list.size() > 0)
            {
                salaryMapper.batchSalaryDetail(list);
            }
        }
    }
}
