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.JudgeFaultLogic;
import org.eco.als.domain.bo.JudgeFaultLogicBo;
import org.eco.als.domain.vo.JudgeFaultLogicVo;
import org.eco.als.mapper.JudgeFaultLogicMapper;
import org.eco.als.service.IJudgeFaultLogicService;
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.JudgeFaultLogicTableDef.JUDGE_FAULT_LOGIC;

/**
 * 判故逻辑Service业务层处理
 *
 * @author wgk
 * @date 2024-12-11
 */
@Service
@Slf4j
public class JudgeFaultLogicServiceImpl extends BaseServiceImpl<JudgeFaultLogicMapper, JudgeFaultLogic> implements IJudgeFaultLogicService {
    @Resource
    private JudgeFaultLogicMapper judgeFaultLogicMapper;

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

    private QueryWrapper buildQueryWrapper(JudgeFaultLogicBo judgeFaultLogicBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();

        queryWrapper.and(JUDGE_FAULT_LOGIC.NAME.like
            (judgeFaultLogicBo.getKeyWord()));
        queryWrapper.and(JUDGE_FAULT_LOGIC.MODEL.eq
            (judgeFaultLogicBo.getModel()));
        queryWrapper.and(JUDGE_FAULT_LOGIC.HMC_CODE.eq
            (judgeFaultLogicBo.getHmcCode()));
        queryWrapper.and(JUDGE_FAULT_LOGIC.NAME.eq
            (judgeFaultLogicBo.getName()));
        queryWrapper.and(JUDGE_FAULT_LOGIC.SYS_NAME.eq
            (judgeFaultLogicBo.getSysName()));
        queryWrapper.and(JUDGE_FAULT_LOGIC.SYS_ID.eq
            (judgeFaultLogicBo.getSysId()));
        queryWrapper.and(JUDGE_FAULT_LOGIC.REMARKS.eq
            (judgeFaultLogicBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询判故逻辑
     *
     * @param id 判故逻辑主键
     * @return 判故逻辑
     */
    @Override
    public JudgeFaultLogicVo selectById(Long id) {
        return this.getOneAs(query().where(JUDGE_FAULT_LOGIC.ID.eq(id)), JudgeFaultLogicVo.class);

    }

    /**
     * 查询判故逻辑
     *
     * @param code hmc码
     * @return 判故逻辑
     */
    @Override
    public JudgeFaultLogicVo selectByHmc(String code) {
        return this.getOneAs(query().where(JUDGE_FAULT_LOGIC.HMC_CODE.eq(code)), JudgeFaultLogicVo.class);

    }


    /**
     * 查询判故逻辑列表
     *
     * @param judgeFaultLogicBo 判故逻辑Bo
     * @return 判故逻辑集合
     */
    @Override
    public List<JudgeFaultLogicVo> selectList(JudgeFaultLogicBo judgeFaultLogicBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(judgeFaultLogicBo);
        return this.listAs(queryWrapper, JudgeFaultLogicVo.class);
    }

    /**
     * 分页查询判故逻辑列表
     *
     * @param judgeFaultLogicBo 判故逻辑Bo
     * @return 分页判故逻辑集合
     */
    @Override
    public PageResult<JudgeFaultLogicVo> selectPage(JudgeFaultLogicBo judgeFaultLogicBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(judgeFaultLogicBo);
        Page<JudgeFaultLogicVo> page = this.pageAs(PageQuery.build(), queryWrapper, JudgeFaultLogicVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增判故逻辑
     *
     * @param judgeFaultLogicBo 判故逻辑Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(JudgeFaultLogicBo judgeFaultLogicBo) {
        JudgeFaultLogic judgeFaultLogic = MapstructUtils.convert(judgeFaultLogicBo, JudgeFaultLogic.class);
        //使用全局配置的雪花算法主键生成器生成ID值
        return this.save(judgeFaultLogic);
    }

    /**
     * 新增判故逻辑，前台提供主键值，一般用于导入的场合
     *
     * @param judgeFaultLogicBo 判故逻辑Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(JudgeFaultLogicBo judgeFaultLogicBo) {
        JudgeFaultLogic judgeFaultLogic = MapstructUtils.convert(judgeFaultLogicBo, JudgeFaultLogic.class);
        //前台传来主键值
        return judgeFaultLogicMapper.insertWithPk(judgeFaultLogic) > 0;
    }

    /**
     * 修改判故逻辑
     *
     * @param judgeFaultLogicBo 判故逻辑Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(JudgeFaultLogicBo judgeFaultLogicBo) {
        JudgeFaultLogic judgeFaultLogic = MapstructUtils.convert(judgeFaultLogicBo, JudgeFaultLogic.class);
        if (ObjectUtil.isNotNull(judgeFaultLogic) && ObjectUtil.isNotNull(judgeFaultLogic.getId())) {
            return this.updateById(judgeFaultLogic);
        }
        return false;
    }

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

}
