package com.dayou.service.impl;

import com.dayou.entity.DictType;
import com.dayou.mapper.DictTypeMapper;
import com.dayou.service.IDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2022-11-29
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {

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

    static{
            dictTypeExcelTitles.add("字典类型名称");
            dictTypeExcelTitles.add("字典类型");
            dictTypeExcelTitles.add("字典编号");
            dictTypeExcelTitles.add("字典描述");
    }


    @Override
    @SuppressWarnings("unchecked")
    public Page<DictType> selectPage(Page page,DictType dictType){
        return this.page(page, new QueryWrapper<DictType>(dictType));
    }


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

    @Override
    public Boolean add(DictType dictType){
        return  this.save(dictType);
    }

    @Override
    public Boolean update(DictType dictType){
        return  this.updateById(dictType);
    }

    @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 < dictTypeExcelTitles.size(); i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style);
            cell.setCellValue(dictTypeExcelTitles.get(i));
        }
    }

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

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

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

    @Override
    public XSSFWorkbook exportExcel(DictType dictType) {
        XSSFWorkbook wb = ExcelUtil.export(DictType.class,
        (page, dictTypeReq) -> selectPage(page,dictTypeReq),
        dictType);
        return wb;
    }

}
