package jnpf.personnels.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.model.enums.GrowthLogEnum;
import jnpf.model.personnels.dto.staff.growth.AddGrowthLogDto;
import jnpf.model.personnels.dto.staff.growth.FtbPersonnelsStaffGrowthLogDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffGrowthLog;
import jnpf.personnels.mapper.FtbPersonnelsStaffGrowthLogMapper;
import jnpf.personnels.service.FtbPersonnelsStaffGrowthLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FtbPersonnelsStaffGrowthLogServiceImpl extends ServiceImpl<FtbPersonnelsStaffGrowthLogMapper, FtbPersonnelsStaffGrowthLog> implements FtbPersonnelsStaffGrowthLogService {

    @Override
    public List<FtbPersonnelsStaffGrowthLogDto> queryAll(String userId) {
        if(StringUtils.isEmpty(userId)){
            return new ArrayList<>();
        }
        List<FtbPersonnelsStaffGrowthLog> list = baseMapper.selectList(
                new LambdaQueryWrapper<FtbPersonnelsStaffGrowthLog>()
                        .eq(FtbPersonnelsStaffGrowthLog::getUserId, userId)
                        .orderByDesc(FtbPersonnelsStaffGrowthLog::getCreatorTime)
        );
        if (CollectionUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        return BeanUtil.copyToList(list, FtbPersonnelsStaffGrowthLogDto.class);
    }

    /**
     * 添加成长记录
     *
     * @param dto
     */
    @Override
    public void addGrowthLog(AddGrowthLogDto dto) {
        FtbPersonnelsStaffGrowthLog entity = new FtbPersonnelsStaffGrowthLog();
        entity.setUserId(dto.getUserId());
        entity.setChangeDate(dto.getChangeDate());
        GrowthLogEnum growthLogEnum = GrowthLogEnum.fromCode(dto.getGrowthType());
        entity.setGrowthType(dto.getGrowthType());
        if (GrowthLogEnum.REPEAT_JOIN == growthLogEnum) {
            entity.setTitle(String.format(growthLogEnum.getMsg(), dto.getNum()));
            entity.setNum(dto.getNum());
        }
        if (GrowthLogEnum.FIRST_JOIN == growthLogEnum) {
            entity.setTitle(growthLogEnum.getMsg());
            entity.setNum(1);
        } else {
            entity.setTitle(growthLogEnum.getMsg());
        }
        entity.setDetail(dto.getDetail());
        entity.setRemarks(dto.getRemarks());
        entity.setStartSalary(dto.getStartSalary());
        entity.setActualStartDate(dto.getActualStartDate());
        entity.setEmployeeId(dto.getEmployeeId());
        baseMapper.insert(entity);
    }

    @Override
    public void revocationGrowthLog(String userId, Date startTime) {
        LambdaQueryWrapper<FtbPersonnelsStaffGrowthLog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffGrowthLog::getUserId,userId);
        queryWrapper.eq(FtbPersonnelsStaffGrowthLog::getGrowthType,1);
        DateTime begin = DateUtil.beginOfDay(startTime);
        DateTime end = DateUtil.endOfDay(startTime);
        queryWrapper.between(FtbPersonnelsStaffGrowthLog::getChangeDate,begin,end);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void addGrowthLogBatch(List<AddGrowthLogDto> addGrowthLogDtos) {
        List<FtbPersonnelsStaffGrowthLog> entityList = addGrowthLogDtos.stream().map(dto -> {
            FtbPersonnelsStaffGrowthLog entity = new FtbPersonnelsStaffGrowthLog();
            entity.setUserId(dto.getUserId());
            entity.setChangeDate(dto.getChangeDate());
            GrowthLogEnum growthLogEnum = GrowthLogEnum.fromCode(dto.getGrowthType());
            entity.setGrowthType(dto.getGrowthType());
            if (GrowthLogEnum.REPEAT_JOIN == growthLogEnum) {
                entity.setTitle(String.format(growthLogEnum.getMsg(), dto.getNum()));
                entity.setNum(dto.getNum());
            }
            if (GrowthLogEnum.FIRST_JOIN == growthLogEnum) {
                entity.setTitle(growthLogEnum.getMsg());
                entity.setNum(1);
            } else {
                entity.setTitle(growthLogEnum.getMsg());
            }
            entity.setDetail(dto.getDetail());
            entity.setRemarks(dto.getRemarks());
            entity.setStartSalary(dto.getStartSalary());
            entity.setActualStartDate(dto.getActualStartDate());
            entity.setEmployeeId(dto.getEmployeeId());
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(entityList);
    }
}
