package org.eco.als.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eco.als.domain.TechnicalDoc;
import org.eco.als.domain.bo.TechnicalDocBo;
import org.eco.als.domain.vo.TechnicalDocImportVo;
import org.eco.als.domain.vo.TechnicalDocVo;
import org.eco.als.listener.TechnicalDocImportListener;
import org.eco.als.mapper.TechnicalDocMapper;
import org.eco.als.service.ITechnicalDocService;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.common.core.utils.bean.BeanUtils;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.excel.service.IExcelService;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import org.eco.system.domain.bo.ImportExportBo;
import org.eco.system.service.IImportExportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.TechnicalDocTableDef.TECHNICAL_DOC;

/**
 * 技术资料Service业务层处理
 *
 * @author wgk
 * @date 2024-07-18
 */
@Service
@Slf4j
public class TechnicalDocServiceImpl extends BaseServiceImpl<TechnicalDocMapper, TechnicalDoc> implements ITechnicalDocService {
    @Resource
    private TechnicalDocMapper technicalDocMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

    @Override
    public QueryWrapper query() {
        return super.query().from(TECHNICAL_DOC);
    }

    private QueryWrapper buildQueryWrapper(TechnicalDocBo technicalDocBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TECHNICAL_DOC.NAME.like
            (technicalDocBo.getName()));
        queryWrapper.and(TECHNICAL_DOC.AUTHOR.eq
            (technicalDocBo.getAuthor()));
        queryWrapper.and(TECHNICAL_DOC.PUBLICATION_DATE.eq
            (technicalDocBo.getPublicationDate()));
        queryWrapper.and(TECHNICAL_DOC.TYPE.eq
            (technicalDocBo.getType()));

        return queryWrapper;
    }

    /**
     * 查询技术资料
     *
     * @param id 技术资料主键
     * @return 技术资料
     */
    @Override
    public TechnicalDocVo selectById(Long id) {
        return this.getOneAs(query().where(TECHNICAL_DOC.ID.eq(id)), TechnicalDocVo.class);

    }

    /**
     * 查询技术资料列表
     *
     * @param technicalDocBo 技术资料Bo
     * @return 技术资料集合
     */
    @Override
    public List<TechnicalDocVo> selectList(TechnicalDocBo technicalDocBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(technicalDocBo);
        return this.listAs(queryWrapper, TechnicalDocVo.class);
    }

    /**
     * 分页查询技术资料列表
     *
     * @param technicalDocBo 技术资料Bo
     * @return 分页技术资料集合
     */
    @Override
    public PageResult<TechnicalDocVo> selectPage(TechnicalDocBo technicalDocBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(technicalDocBo);
        Page<TechnicalDocVo> page = this.pageAs(PageQuery.build(), queryWrapper, TechnicalDocVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增技术资料
     *
     * @param technicalDocBo 技术资料Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TechnicalDocBo technicalDocBo) {
        TechnicalDoc technicalDoc = MapstructUtils.convert(technicalDocBo, TechnicalDoc.class);

        return this.save(technicalDoc);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增技术资料，前台提供主键值，一般用于导入的场合
     *
     * @param technicalDocBo 技术资料Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TechnicalDocBo technicalDocBo) {
        TechnicalDoc technicalDoc = MapstructUtils.convert(technicalDocBo, TechnicalDoc.class);


        return technicalDocMapper.insertWithPk(technicalDoc) > 0;//前台传来主键值
    }

    /**
     * 修改技术资料
     *
     * @param technicalDocBo 技术资料Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TechnicalDocBo technicalDocBo) {
        TechnicalDoc technicalDoc = MapstructUtils.convert(technicalDocBo, TechnicalDoc.class);
        if (ObjectUtil.isNotNull(technicalDoc) && ObjectUtil.isNotNull(technicalDoc.getId())) {
            boolean updated = this.updateById(technicalDoc);
            return updated;
        }
        return false;
    }

    @Override
    public void asyncImportData(MultipartFile file, boolean updateSupport, LoginUser loginUser) {
        ExcelResultRes result;
        try {
            String name = file.getOriginalFilename();
            result = excelService.importExcel(file.getInputStream(), name, TechnicalDocImportVo.class, new TechnicalDocImportListener(updateSupport, loginUser));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        ImportExportBo bo = new ImportExportBo();
        BeanUtils.copyProperties(result, bo);
        bo.setUpdateBy(loginUser.getUserId());
        bo.setCreateBy(loginUser.getUserId());
        bo.setType("0");
        boolean flag = importExportService.insert(bo);
        if (flag) {
            log.info("异步导入日志写入成功");
        }
    }

    @Override
    public void asyncExport(List<TechnicalDocVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, TechnicalDocVo.class);
        ImportExportBo bo = new ImportExportBo();
        BeanUtils.copyProperties(result, bo);
        bo.setUpdateBy(loginUser.getUserId());
        bo.setCreateBy(loginUser.getUserId());
        bo.setType("1");
        boolean flag = importExportService.insert(bo);
        if (flag) {
            log.info("异步导出日志写入成功");
        }
    }

    /**
     * 批量删除技术资料
     *
     * @param ids 需要删除的技术资料主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
