package com.dayou.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dayou.bo.SimpleParentModel;
import com.dayou.common.BaseEntity;
import com.dayou.common.PullDownModel;
import com.dayou.common.SimpleListModel;
import com.dayou.dto.SimplePostModel;
import com.dayou.entity.Post;
import com.dayou.mapper.PostMapper;
import com.dayou.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.PostVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 岗位 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2022-11-29
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    private static final List<String> postExcelTitles= new ArrayList();

    static{
            postExcelTitles.add("名称");
            postExcelTitles.add("岗位职责");
    }

    @Autowired
    private PostMapper postMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<PostVO> selectPage(Page page, PostVO postVO){
        return postMapper.getPage(page,postVO);
    }


    @Override
    public Post detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(Post post){
        return  this.save(post);
    }

    @Override
    public Boolean update(Post post){
        return  this.updateById(post);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public XSSFWorkbook generateImportTemplate(){
        XSSFWorkbook wb = new XSSFWorkbook();
        //创建固定列的表头
        createExcelTitle(wb);
        return wb;
    }

    private void createExcelTitle(XSSFWorkbook wb) {
        Sheet sheet = wb.createSheet("岗位导入模板");
        Row row = sheet.createRow(0);
        CellStyle style = wb.createCellStyle();
        Cell cell = null;
        for (int i = 0; i < postExcelTitles.size(); i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style);
            cell.setCellValue(postExcelTitles.get(i));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importExcel(MultipartFile file){
        ExcelUtil.importExcel(Post.class, new ExcelUtil.ImportListener<Post>() {
            @Override
            public Boolean handleOneRecord(Post post) {
                return add(post);
            }

            @Override
            public void afterImport(ExcelUtil.BatchImportResult batchImportResult) {

            }
        }, file, BatchTaskTypeEnum.DEFAULT,ExcelUtil.BatchImportStrategy.ROLL_BACK);
    }

    @Override
    public XSSFWorkbook exportExcel(Post post) {
//        XSSFWorkbook wb = ExcelUtil.export(Post.class,
//        (page, postReq) -> selectPage(page,postReq),
//        post);
        return null;
    }

    @Override
    public void upsert(Post superAdminPost) {
        postMapper.upsert(superAdminPost);
    }

    @Override
    public List<PullDownModel > xSimpleAll() {
        List<PullDownModel > list = postMapper.xSimpleAll();
        return list;
    }

    @Override
    public List<Post> getPostList(PostVO postVO) {
        List<Post> list = this.list(new LambdaQueryWrapper<Post>()
                .eq(postVO.getLeaderDepartmentId() != null, Post::getDepartmentId, postVO.getLeaderDepartmentId())
                .eq(BaseEntity::getDeleted, Boolean.FALSE));
        return list;
    }

    @Override
    public List<SimpleParentModel> getDepartmentList(Set<Long> postIds) {
        if (CollectionUtils.isNotEmpty(postIds)){
            List<SimpleParentModel> list = postMapper.getDepartmentList(postIds);
            return list;
        }
        return null;
    }

    @Override
    public Boolean mostHighPost(SimplePostModel post) {
        List<Long> collect = this.list(new LambdaQueryWrapper<Post>().eq(Post::getDepartmentId, post.getDepartmentId())
                        .eq(BaseEntity::getDeleted, Boolean.FALSE)).stream().map(BaseEntity::getId)
                .collect(Collectors.toList());

        return !collect.contains(post.getParentId());
    }

}
