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.Communication;
import org.eco.als.domain.bo.CommunicationBo;
import org.eco.als.domain.vo.CommunicationImportVo;
import org.eco.als.domain.vo.CommunicationVo;
import org.eco.als.listener.CommunicationImportListener;
import org.eco.als.mapper.CommunicationMapper;
import org.eco.als.service.ICommunicationService;
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.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.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.CommunicationTableDef.COMMUNICATION;

/**
 * 通讯管理Service业务层处理
 *
 * @author wgk
 * @date 2024-12-17
 */
@Service
@Slf4j
public class CommunicationServiceImpl extends BaseServiceImpl<CommunicationMapper, Communication> implements ICommunicationService {
    @Resource
    private CommunicationMapper communicationMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(CommunicationBo communicationBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(COMMUNICATION.NAME.like
            (communicationBo.getName()));
        queryWrapper.and(COMMUNICATION.IP_ADDRESS.eq
            (communicationBo.getIpAddress()));
        queryWrapper.and(COMMUNICATION.NETWORK_SEGMENT.eq
            (communicationBo.getNetworkSegment()));
        queryWrapper.and(COMMUNICATION.SUBNET_MASK.eq
            (communicationBo.getSubnetMask()));
        queryWrapper.and(COMMUNICATION.DNS.eq
            (communicationBo.getDns()));
        queryWrapper.and(COMMUNICATION.GATEWAY.eq
            (communicationBo.getGateway()));
        queryWrapper.and(COMMUNICATION.HTTP.eq
            (communicationBo.getHttp()));
        queryWrapper.and(COMMUNICATION.PORT.eq
            (communicationBo.getPort()));
        queryWrapper.and(COMMUNICATION.USER_NAME.like
            (communicationBo.getUserName()));
        queryWrapper.and(COMMUNICATION.PASSWORD.eq
            (communicationBo.getPassword()));

        return queryWrapper;
    }

    /**
     * 查询通讯管理
     *
     * @param id 通讯管理主键
     * @return 通讯管理
     */
    @Override
    public CommunicationVo selectById(Long id) {
        return this.getOneAs(query().where(COMMUNICATION.ID.eq(id)), CommunicationVo.class);

    }


    /**
     * 查询通讯管理列表
     *
     * @param communicationBo 通讯管理Bo
     * @return 通讯管理集合
     */
    @Override
    public List<CommunicationVo> selectList(CommunicationBo communicationBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(communicationBo);
        return this.listAs(queryWrapper, CommunicationVo.class);
    }

    /**
     * 分页查询通讯管理列表
     *
     * @param communicationBo 通讯管理Bo
     * @return 分页通讯管理集合
     */
    @Override
    public PageResult<CommunicationVo> selectPage(CommunicationBo communicationBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(communicationBo);
        Page<CommunicationVo> page = this.pageAs(PageQuery.build(), queryWrapper, CommunicationVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增通讯管理
     *
     * @param communicationBo 通讯管理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(CommunicationBo communicationBo) {
        Communication communication = MapstructUtils.convert(communicationBo, Communication.class);

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

    /**
     * 新增通讯管理，前台提供主键值，一般用于导入的场合
     *
     * @param communicationBo 通讯管理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(CommunicationBo communicationBo) {
        Communication communication = MapstructUtils.convert(communicationBo, Communication.class);


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

    /**
     * 修改通讯管理
     *
     * @param communicationBo 通讯管理Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(CommunicationBo communicationBo) {
        Communication communication = MapstructUtils.convert(communicationBo, Communication.class);
        if (ObjectUtil.isNotNull(communication) && ObjectUtil.isNotNull(communication.getId())) {
            boolean updated = this.updateById(communication);
            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, CommunicationImportVo.class, new CommunicationImportListener(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<CommunicationVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, CommunicationVo.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));
    }

}
