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

import java.util.Date;
import java.util.List;

import com.hzy.common.enums.EDelFlag;
import com.hzy.common.utils.DateUtils;
import com.hzy.common.utils.RMBUtil;
import com.hzy.common.utils.security.ShiroUtils;
import com.hzy.project.business.customer.domain.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzy.project.business.subject.mapper.SalarySubjectMapper;
import com.hzy.project.business.subject.domain.SalarySubject;
import com.hzy.project.business.subject.service.ISalarySubjectService;
import com.hzy.common.utils.text.Convert;

/**
 * 工资科目Service业务层处理
 * 
 * @author ruoyi
 * @date 2026-01-02
 */
@Service
public class SalarySubjectServiceImpl implements ISalarySubjectService 
{
    @Autowired
    private SalarySubjectMapper salarySubjectMapper;

    /**
     * 查询工资科目
     * 
     * @param salarySubjectId 工资科目主键
     * @return 工资科目
     */
    @Override
    public SalarySubject selectSalarySubjectBySalarySubjectId(Long salarySubjectId)
    {
        return salarySubjectMapper.selectSalarySubjectBySalarySubjectId(salarySubjectId);
    }

    /**
     * 查询工资科目列表
     * 
     * @param salarySubject 工资科目
     * @return 工资科目
     */
    @Override
    public List<SalarySubject> selectSalarySubjectList(SalarySubject salarySubject)
    {
        return salarySubjectMapper.selectSalarySubjectList(salarySubject);
    }

    /**
     * 新增工资科目
     * 
     * @param salarySubject 工资科目
     * @return 结果
     */
    @Override
    public int insertSalarySubject(SalarySubject salarySubject)
    {
        salarySubject.setCreateBy(ShiroUtils.getLoginName());
        salarySubject.setCreateTime(DateUtils.getNowDate());
        return salarySubjectMapper.insertSalarySubject(salarySubject);
    }

    /**
     * 修改工资科目
     * 
     * @param salarySubject 工资科目
     * @return 结果
     */
    @Override
    public int updateSalarySubject(SalarySubject salarySubject)
    {
        salarySubject.setUpdateBy(ShiroUtils.getLoginName());
        salarySubject.setUpdateTime(DateUtils.getNowDate());
        return salarySubjectMapper.updateSalarySubject(salarySubject);
    }

    /**
     * 批量删除工资科目
     * 
     * @param salarySubjectIds 需要删除的工资科目主键
     * @return 结果
     */
    @Override
    public int deleteSalarySubjectBySalarySubjectIds(String salarySubjectIds)
    {
        return salarySubjectMapper.deleteSalarySubjectBySalarySubjectIds(Convert.toStrArray(salarySubjectIds));
    }

    /**
     * 删除工资科目信息
     * 
     * @param salarySubjectId 工资科目主键
     * @return 结果
     */
    @Override
    public int deleteSalarySubjectBySalarySubjectId(Long salarySubjectId)
    {
        return salarySubjectMapper.deleteSalarySubjectBySalarySubjectId(salarySubjectId);
    }

    /**
     * 批量逻辑删除工资科目
     *
     * @param salarySubjectIds 需要删除的工资科目主键
     * @return 结果
     */
    @Override
    public int logicDelSalarySubjectBySalarySubjectId(String salarySubjectIds)
    {
        String[] salarySubjectIdList = Convert.toStrArray(salarySubjectIds);

        int rtn = 0;
        for (String salarySubjectId : salarySubjectIdList) {

            SalarySubject salarySubject = new SalarySubject();
            salarySubject.setSalarySubjectId(Long.valueOf(salarySubjectId));
            salarySubject.setDelFlag(EDelFlag.YES.getVal());
            salarySubject.setUpdateBy(ShiroUtils.getLoginName());
            salarySubject.setUpdateTime(DateUtils.getNowDate());

            rtn += salarySubjectMapper.updateSalarySubject(salarySubject);
        }
        return rtn;
    }

    @Override
    public int dragSorted(SalarySubject salarySubject) {
        Date now = DateUtils.getNowDate();
        int result = 0;
        Object pz = salarySubject.getParams().get("pageSize");
        int pageSize = Convert.toInt(pz, 10);
        Object pn = salarySubject.getParams().get("pageNumber");
        int pageNumber = Convert.toInt(pn, 1);
        List<SalarySubject> sorteds = salarySubject.getPramsByName("sorted", SalarySubject.class);
        if (!sorteds.isEmpty()) {
            int sort = pageSize * (pageNumber - 1) + 1;
            for (SalarySubject entity : sorteds) {
                entity.setPercent(RMBUtil.yuanToFen(entity.getPercent()));
                entity.setUpdateBy(ShiroUtils.getLoginName());
                entity.setUpdateTime(now);
                entity.setSort(Long.valueOf(sort++));
                result = result + salarySubjectMapper.updateSalarySubject(entity);
            }
        }
        return result;
    }
}
