package org.eco.als.service.impl;

import java.util.Arrays;
import java.util.List;

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

import java.io.IOException;

import org.eco.als.mapper.AirInstallMapper;
import org.eco.als.domain.AirInstall;
import org.eco.als.domain.bo.AirInstallBo;
import org.eco.als.domain.vo.AirInstallVo;
import org.eco.als.domain.vo.AirInstallImportVo;
import org.eco.als.listener.AirInstallImportListener;
import org.eco.als.service.IAirInstallService;

import static org.eco.als.domain.table.AirInstallTableDef.AIR_INSTALL;

/**
 * 装机信息Service业务层处理
 *
 * @author wgk
 * @date 2024-11-15
 */
@Service
@Slf4j
public class AirInstallServiceImpl extends BaseServiceImpl<AirInstallMapper, AirInstall> implements IAirInstallService {
    @Resource
    private AirInstallMapper airInstallMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(AirInstallBo airInstallBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(AIR_INSTALL.CONFIG_ID.eq
            (airInstallBo.getConfigId()));
        queryWrapper.and(AIR_INSTALL.MATERIAL_CODE.eq
            (airInstallBo.getMaterialCode()));
        queryWrapper.and(AIR_INSTALL.MATERIAL_NAME.like
            (airInstallBo.getMaterialName()));
        queryWrapper.and(AIR_INSTALL.SPECS_MODEL.eq
            (airInstallBo.getSpecsModel()));
        queryWrapper.and(AIR_INSTALL.DRAWING_NUMBER.eq
            (airInstallBo.getDrawingNumber()));
        queryWrapper.and(AIR_INSTALL.CATEGORY.eq
            (airInstallBo.getCategory()));
        queryWrapper.and(AIR_INSTALL.QUANTITY.eq
            (airInstallBo.getQuantity()));
        queryWrapper.and(AIR_INSTALL.INSTALL_TIME.eq
            (airInstallBo.getInstallTime()));
        queryWrapper.and(AIR_INSTALL.REPAIRABLE.eq
            (airInstallBo.getRepairable()));
        queryWrapper.and(AIR_INSTALL.COMPLETE_SET.eq
            (airInstallBo.getCompleteSet()));
        queryWrapper.and(AIR_INSTALL.SERVICE_LIFE.eq
            (airInstallBo.getServiceLife()));
        queryWrapper.and(AIR_INSTALL.REMARKS.eq
            (airInstallBo.getRemarks()));
        queryWrapper.and(AIR_INSTALL.CREATED_BY.eq
            (airInstallBo.getCreatedBy()));
        queryWrapper.and(AIR_INSTALL.CREATED_TIME.eq
            (airInstallBo.getCreatedTime()));
        queryWrapper.and(AIR_INSTALL.UPDATED_BY.eq
            (airInstallBo.getUpdatedBy()));
        queryWrapper.and(AIR_INSTALL.UPDATED_TIME.eq
            (airInstallBo.getUpdatedTime()));

        return queryWrapper;
    }

    /**
     * 查询装机信息
     *
     * @param id 装机信息主键
     * @return 装机信息
     */
    @Override
    public AirInstallVo selectById(Long id) {
        return this.getOneAs(query().where(AIR_INSTALL.ID.eq(id)), AirInstallVo.class);

    }


    /**
     * 查询装机信息列表
     *
     * @param airInstallBo 装机信息Bo
     * @return 装机信息集合
     */
    @Override
    public List<AirInstallVo> selectList(AirInstallBo airInstallBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(airInstallBo);
        return this.listAs(queryWrapper, AirInstallVo.class);
    }

    /**
     * 分页查询装机信息列表
     *
     * @param airInstallBo 装机信息Bo
     * @return 分页装机信息集合
     */
    @Override
    public PageResult<AirInstallVo> selectPage(AirInstallBo airInstallBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(airInstallBo);
        Page<AirInstallVo> page = this.pageAs(PageQuery.build(), queryWrapper, AirInstallVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增装机信息
     *
     * @param airInstallBo 装机信息Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AirInstallBo airInstallBo) {
        AirInstall airInstall = MapstructUtils.convert(airInstallBo, AirInstall.class);

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

    /**
     * 新增装机信息，前台提供主键值，一般用于导入的场合
     *
     * @param airInstallBo 装机信息Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(AirInstallBo airInstallBo) {
        AirInstall airInstall = MapstructUtils.convert(airInstallBo, AirInstall.class);


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

    /**
     * 修改装机信息
     *
     * @param airInstallBo 装机信息Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AirInstallBo airInstallBo) {
        AirInstall airInstall = MapstructUtils.convert(airInstallBo, AirInstall.class);
        if (ObjectUtil.isNotNull(airInstall) && ObjectUtil.isNotNull(airInstall.getId())) {
            boolean updated = this.updateById(airInstall);
            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, AirInstallImportVo.class, new AirInstallImportListener(updateSupport, loginUser));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        boolean flag = importExportService.saveInfo(result, loginUser, "0");
        if (flag) {
            log.info("异步导入日志写入成功");
        }
    }

    @Override
    public void asyncExport(List<AirInstallVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, AirInstallVo.class);
        boolean flag = importExportService.saveInfo(result, loginUser, "1");
        if (flag) {
            log.info("异步导出日志写入成功");
        }
    }

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

}
