package com.app.service.platform;

import com.app.dto.PageResult;
import com.app.dto.platform.ResponseNodeDto;
import com.app.dto.platform.SearchJobDto;
import com.app.entity.BaseEntity;
import com.app.entity.platform.system.Job;
import com.app.mapper.platform.JobMapper;
import com.app.util.NodeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class JobService {

    @Resource
    private JobMapper jobMapper;


    /**
     * 保存或更新【岗位】
     *
     */
    @Transactional
    public Job saveOrUpdate(Job job) {
        Job _job = null;
        if (StringUtils.isEmpty(job.getId())) {
            Integer count = jobMapper.selectCount(new QueryWrapper<Job>().lambda().eq(Job::getName, job.getName())
                    .eq(Job::getIsDel,Boolean.FALSE));
            if(count!=0){
                return null;
            }
            _job = new Job();
            _job.setName(job.getName());
            _job.setRemarks(job.getRemarks());
            _job.setSort(job.getSort());
            jobMapper.insert(_job);
        } else {
            Optional<Job> newsOptional = Optional.ofNullable(jobMapper.selectById(job.getId()));
            if (newsOptional.isPresent()) {
                _job = newsOptional.get();
                Integer count = jobMapper.selectCount(new QueryWrapper<Job>().lambda().ne(Job::getId, _job.getId())
                        .eq(Job::getName, job.getName()).eq(Job::getIsDel,Boolean.FALSE));
                if(count!=0){
                    return null;
                }
                _job.setName(job.getName());
                _job.setRemarks(job.getRemarks());
                _job.setSort(job.getSort());
                jobMapper.updateById(_job);
            }
        }
        return _job;
    }


    /**
     * 查询【岗位】分页
     */
    public PageResult<Job> page(SearchJobDto dto) {
        LambdaQueryWrapper<Job> wrapper = new QueryWrapper<Job>().lambda();
        if(!StringUtils.isEmpty(dto.getName())){
            wrapper.like(Job::getName,dto.getName());
        }
        if(dto.getIsDel()==null){
            dto.setIsDel(false);
        }
        wrapper.eq(Job::getIsDel,dto.getIsDel());
        Page<Job> page = jobMapper.selectPage(new Page<>(dto.getPage(), dto.getSize()), wrapper);
        return new PageResult<>(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 删除【岗位】
     */
    public void handleIsDel(String id) {
        Optional<Job> optional = Optional.ofNullable(jobMapper.selectById(id));
        if (optional.isPresent()) {
            Job job = optional.get();
            job.setIsDel(!job.getIsDel());
            jobMapper.updateById(job);
        }
    }

    /**
     * 获取节点树
     * @return 节点树
     */
    public List<ResponseNodeDto> getAllNodes() {
        LambdaQueryWrapper<Job> wrapper = new QueryWrapper<Job>().lambda().eq(BaseEntity::getIsDel, Boolean.FALSE);
        List<Job> jobList = jobMapper.selectList(wrapper);
        List<ResponseNodeDto> list=new ArrayList<>();
        for (Job job : jobList) {
            ResponseNodeDto nodeDto = new ResponseNodeDto();
            nodeDto.setLabel(job.getName());
            nodeDto.setValue(job.getId());
            list.add(nodeDto);
        }
        return NodeUtils.buildTree(list);
    }

}
