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.WarningResult;
import org.eco.als.domain.bo.WarningResultBo;
import org.eco.als.domain.vo.WarningResultVo;
import org.eco.als.mapper.WarningResultMapper;
import org.eco.als.service.IWarningResultService;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static org.eco.als.domain.table.WarningResultTableDef.WARNING_RESULT;

/**
 * 警告结果Service业务层处理
 *
 * @author wgk
 * @date 2025-02-27
 */
@Service
@Slf4j
public class WarningResultServiceImpl extends BaseServiceImpl<WarningResultMapper, WarningResult> implements IWarningResultService {
    @Resource
    private WarningResultMapper warningResultMapper;

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

    private QueryWrapper buildQueryWrapper(WarningResultBo warningResultBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(WARNING_RESULT.SORTIE_NO.eq
            (warningResultBo.getSortieNo()));
        queryWrapper.and(WARNING_RESULT.AIRCRAFT_ID.eq
            (warningResultBo.getAircraftId()));
        queryWrapper.and(WARNING_RESULT.CODE.eq
            (warningResultBo.getCode()));
        queryWrapper.and(WARNING_RESULT.NAME.like
            (warningResultBo.getName()));
        queryWrapper.and(WARNING_RESULT.HMC.eq
            (warningResultBo.getHmc()));
        queryWrapper.and(WARNING_RESULT.DESCRIBE.eq
            (warningResultBo.getDescribe()));
        queryWrapper.and(WARNING_RESULT.STATUS.eq
            (warningResultBo.getStatus()));
        queryWrapper.and(WARNING_RESULT.RESULT_CONTENT.eq
            (warningResultBo.getResultContent()));
        queryWrapper.and(WARNING_RESULT.ATTRIBUTE1.eq
            (warningResultBo.getAttribute1()));
        queryWrapper.and(WARNING_RESULT.ATTRIBUTE2.eq
            (warningResultBo.getAttribute2()));
        queryWrapper.and(WARNING_RESULT.ATTRIBUTE3.eq
            (warningResultBo.getAttribute3()));
        queryWrapper.and(WARNING_RESULT.ATTRIBUTE4.eq
            (warningResultBo.getAttribute4()));
        queryWrapper.and(WARNING_RESULT.ATTRIBUTE5.eq
            (warningResultBo.getAttribute5()));

        return queryWrapper;
    }

    /**
     * 查询警告结果
     *
     * @param id 警告结果主键
     * @return 警告结果
     */
    @Override
    public WarningResultVo selectById(Long id) {
        return this.getOneAs(query().where(WARNING_RESULT.ID.eq(id)), WarningResultVo.class);

    }


    /**
     * 查询警告结果列表
     *
     * @param warningResultBo 警告结果Bo
     * @return 警告结果集合
     */
    @Override
    public List<WarningResultVo> selectList(WarningResultBo warningResultBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(warningResultBo);
        return this.listAs(queryWrapper, WarningResultVo.class);
    }

    /**
     * 分页查询警告结果列表
     *
     * @param warningResultBo 警告结果Bo
     * @return 分页警告结果集合
     */
    @Override
    public PageResult<WarningResultVo> selectPage(WarningResultBo warningResultBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(warningResultBo);
        Page<WarningResultVo> page = this.pageAs(PageQuery.build(), queryWrapper, WarningResultVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增警告结果
     *
     * @param warningResultBo 警告结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(WarningResultBo warningResultBo) {
        WarningResult warningResult = MapstructUtils.convert(warningResultBo, WarningResult.class);

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

    /**
     * 新增警告结果，前台提供主键值，一般用于导入的场合
     *
     * @param warningResultBo 警告结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(WarningResultBo warningResultBo) {
        WarningResult warningResult = MapstructUtils.convert(warningResultBo, WarningResult.class);


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

    /**
     * 修改警告结果
     *
     * @param warningResultBo 警告结果Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(WarningResultBo warningResultBo) {
        WarningResult warningResult = MapstructUtils.convert(warningResultBo, WarningResult.class);
        if (ObjectUtil.isNotNull(warningResult) && ObjectUtil.isNotNull(warningResult.getId())) {
            boolean updated = this.updateById(warningResult);
            return updated;
        }
        return false;
    }

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

}
