package com.engine.salary.biz;

import com.engine.salary.entity.salaryBill.dto.SalaryTemplateListDTO;
import com.engine.salary.entity.salaryBill.param.SalaryTemplateQueryParam;
import com.engine.salary.entity.salaryBill.po.SalaryTemplatePO;
import com.engine.salary.mapper.salarybill.SalaryTemplateMapper;
import org.apache.ibatis.session.SqlSession;
import weaver.conn.mybatis.MyBatisFactory;

import java.util.Collection;
import java.util.List;

/**
 * 工资单模板
 */
public class SalaryTemplateBiz {

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    public SalaryTemplatePO getById(Long id) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            return mapper.getById(id);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 根据id更新
     * @param salaryTemplateNew
     */
    public void updateById(SalaryTemplatePO salaryTemplateNew) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            mapper.updateIgnoreNull(salaryTemplateNew);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 根据现有字段查询
     * @param po
     * @return
     */
    public List<SalaryTemplatePO> listSome(SalaryTemplatePO po) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            return mapper.listSome(po);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 新增
     * @param salaryTemplatePO
     * @return
     */
    public void insert(SalaryTemplatePO salaryTemplatePO) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            mapper.insertIgnoreNull(salaryTemplatePO);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 查询工资单模板列表
     * @param param
     * @return
     */
    public List<SalaryTemplateListDTO> list(SalaryTemplateQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            return mapper.list(param);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 批量删除工资单模板
     * @param ids
     */
    public void deleteByIds(Collection<Long> ids) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            mapper.deleteByIds(ids);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 默认模板
     */
    public List<SalaryTemplatePO> listDefaultTemplates(List<Long> ids) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            return mapper.listDefaultTemplates(ids);
        } finally {
            sqlSession.close();
        }
    }

    public void updateBySalarySobId(SalaryTemplatePO po) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            mapper.updateUsetypeBySalarySobId(po);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public List<SalaryTemplateListDTO> listDTO(SalaryTemplatePO param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryTemplateMapper mapper = sqlSession.getMapper(SalaryTemplateMapper.class);
            return mapper.listDTO(param);
        } finally {
            sqlSession.close();
        }
    }
}
