package jnpf.personnels.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.model.enums.SalaryChangeTypeEnum;
import jnpf.model.personnels.dto.staff.salarylog.AddSalaryChangeLogDto;
import jnpf.model.personnels.dto.staff.salarylog.FtbPersonnelsStaffSalaryChangeLogDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffSalaryChangeLog;
import jnpf.personnels.mapper.FtbPersonnelsStaffSalaryChangeLogMapper;
import jnpf.personnels.service.FtbPersonnelsStaffSalaryChangeLogService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class FtbPersonnelsStaffSalaryChangeLogServiceImpl extends ServiceImpl<FtbPersonnelsStaffSalaryChangeLogMapper, FtbPersonnelsStaffSalaryChangeLog> implements FtbPersonnelsStaffSalaryChangeLogService {

    @Override
    public List<FtbPersonnelsStaffSalaryChangeLogDto> queryAll(String userId) {

        List<FtbPersonnelsStaffSalaryChangeLog> list = baseMapper.selectList(
                new LambdaQueryWrapper<FtbPersonnelsStaffSalaryChangeLog>()
                        .eq(FtbPersonnelsStaffSalaryChangeLog::getUserId, userId)
                        .orderByDesc(FtbPersonnelsStaffSalaryChangeLog::getCreatorTime)
        );
        if (CollectionUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        return BeanUtil.copyToList(list, FtbPersonnelsStaffSalaryChangeLogDto.class);
    }


    @Override
    public void addSalaryChangeLog(AddSalaryChangeLogDto dto) {
        if (dto.getSalary() == null) {
            return;
        }
        int comparisonResult = dto.getSalary().compareTo(BigDecimal.ZERO);
        if (comparisonResult <= 0) {
            return;
        }
        FtbPersonnelsStaffSalaryChangeLog entity = new FtbPersonnelsStaffSalaryChangeLog();
        entity.setUserId(dto.getUserId());
        entity.setChangeDate(dto.getChangeDate());
        entity.setSalary(dto.getSalary());
        SalaryChangeTypeEnum salaryChangeTypeEnum = SalaryChangeTypeEnum.fromCode(dto.getChangeType());
        entity.setTitle(salaryChangeTypeEnum.getMsg());
        entity.setSalaryType(salaryChangeTypeEnum.getCode());
        baseMapper.insert(entity);
    }
}
