package jnpf.personnels.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.base.vo.PageListVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.personnels.dto.resignation.FtbResignationConfigurationDTO;
import jnpf.model.personnels.po.FtbPersonnelsResignationCategoryConfiguration;
import jnpf.model.personnels.po.FtbPersonnelsResignationConfiguration;
import jnpf.model.personnels.vo.resignation.FtbResignationConfigurationVO;
import jnpf.personnels.mapper.FtbPersonnelsResignationConfigurationMapper;
import jnpf.personnels.service.FtbPersonnelsPermissionsService;
import jnpf.personnels.service.FtbPersonnelsResignationCategoryConfigurationService;
import jnpf.personnels.service.FtbPersonnelsResignationConfigurationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FtbPersonnelsResignationConfigurationServiceImpl extends ServiceImpl<FtbPersonnelsResignationConfigurationMapper, FtbPersonnelsResignationConfiguration> implements FtbPersonnelsResignationConfigurationService {

    @Resource
    FtbPersonnelsResignationCategoryConfigurationService categoryConfigurationService;

    @Resource
    private FtbPersonnelsPermissionsService ftbPersonnelsPermissionsService;



    @Override
    @Transactional
    public void configurationSave(List<FtbResignationConfigurationDTO> resignationConfigurationDTO) {
        List<String> names = resignationConfigurationDTO.stream().map(FtbResignationConfigurationDTO::getResignationName)
                .collect(Collectors.toList());
        List<String> configIds = resignationConfigurationDTO.stream().map(FtbResignationConfigurationDTO::getConfigId)
                .filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        LambdaQueryWrapper<FtbPersonnelsResignationConfiguration> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbPersonnelsResignationConfiguration::getResignationName, names);
        wrapper.notIn(CollUtil.isNotEmpty(configIds), SuperBaseEntity.SuperIBaseEntity::getId,configIds);
        Long l = baseMapper.selectCount(wrapper);
        if (l > 0) throw new RuntimeException("离职原因名称已经存在,请勿重复添加!");
        resignationConfigurationDTO.stream().filter(vo -> StringUtils.isNotEmpty(vo.getConfigId()))
                .map(FtbResignationConfigurationDTO::convertUpdate)
                .forEach(this::update);
        List<FtbPersonnelsResignationConfiguration> collect = resignationConfigurationDTO.
                stream().filter(vo -> StringUtils.isEmpty(vo.getConfigId()))
                .map(FtbPersonnelsResignationConfiguration::convert)
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) this.saveBatch(collect);
    }

    @Override
    public PageListVO<FtbResignationConfigurationVO> getList(String keyWords, String resignationTypeId, CultivatePage page) {
        Page<FtbResignationConfigurationVO> objectPage = page.coverCultivatePage();
        LambdaQueryWrapper<FtbPersonnelsResignationConfiguration> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(resignationTypeId), FtbPersonnelsResignationConfiguration::getResignationTypeId, resignationTypeId);
        wrapper.like(StrUtil.isNotBlank(keyWords), FtbPersonnelsResignationConfiguration::getResignationName, keyWords);
        wrapper.orderByDesc(SuperBaseEntity.SuperCBaseEntity::getCreatorTime);
        List<FtbResignationConfigurationVO> collect = this.list(wrapper)
                .stream()
                .map(vo -> {
                    FtbResignationConfigurationVO convert = FtbResignationConfigurationVO.convert(vo);
                    LambdaQueryWrapper<FtbPersonnelsResignationCategoryConfiguration> tLambdaQueryWrapper = Wrappers.lambdaQuery();
                    tLambdaQueryWrapper.eq(FtbPersonnelsResignationCategoryConfiguration::getId, vo.getResignationTypeId());
                    FtbPersonnelsResignationCategoryConfiguration one = categoryConfigurationService.getOne(tLambdaQueryWrapper);
                    if (ObjectUtil.isNotEmpty(one))convert.setResignationTypeName(one.getResignationTypeName());
                    return convert;
                })
                .collect(Collectors.toList());
        return CultivatePage.paginate(collect,objectPage);
    }

    @Override
    public List<FtbResignationConfigurationVO> reasonForResignationDropDown(Integer individualApplication) {
        LambdaQueryWrapper<FtbPersonnelsResignationConfiguration> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsResignationConfiguration::getChecked, 0);
        if (individualApplication == null) {
            queryWrapper.in(FtbPersonnelsResignationConfiguration::getIsVisible, 0, 1);
        }else {
            queryWrapper.eq(FtbPersonnelsResignationConfiguration::getIsVisible, 1);
        }
        queryWrapper.eq(FtbPersonnelsResignationConfiguration::getEnableMark, 0);
        return this.list(queryWrapper)
                .stream()
                .map(FtbResignationConfigurationVO::convert)
                .collect(Collectors.toList());
    }

}
