package com.zksy.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.domain.TerminalFault;
import com.zksy.base.mapper.TerminalFaultMapper;
import com.zksy.base.service.TerminalFaultService;
import com.zksy.common.exception.ServiceException;
import com.zksy.manhole.dto.in.TerminalFaultHandleInDTO;
import com.zksy.manhole.dto.in.TerminalFaultPageInDTO;
import com.zksy.manhole.dto.out.TerminalFaultOutDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【terminal_fault(终端故障表)】的数据库操作Service实现
* @createDate 2026-06-26 14:36:18
*/
@Slf4j
@Service
public class TerminalFaultServiceImpl extends ServiceImpl<TerminalFaultMapper, TerminalFault>
    implements TerminalFaultService{

    /**
     * 分页查询终端故障列表
     * @param pageInDTO
     * @return
     */
    @Override
    public Page<TerminalFaultOutDTO> findByPage(TerminalFaultPageInDTO pageInDTO) {
        log.info("分页查询终端故障列表-入参:{}", pageInDTO);
        Page<TerminalFault> page = new Page<>(pageInDTO.getPageNum(), pageInDTO.getPageSize());
        LambdaQueryWrapper<TerminalFault> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(pageInDTO.getFaultNo()), TerminalFault::getFaultNo, pageInDTO.getFaultNo())
                .eq(StringUtils.isNotEmpty(pageInDTO.getDeviceId()), TerminalFault::getDeviceId, pageInDTO.getDeviceId())
                .eq(pageInDTO.getFaultLevel() != null, TerminalFault::getFaultLevel, pageInDTO.getFaultLevel())
                .eq(pageInDTO.getFaultSource() != null, TerminalFault::getFaultSource, pageInDTO.getFaultSource())
                .eq(pageInDTO.getFaultStatus() != null, TerminalFault::getFaultStatus, pageInDTO.getFaultStatus())
                .ge(pageInDTO.getStartTime() != null, TerminalFault::getHappenTime, pageInDTO.getStartTime())
                .le(pageInDTO.getEndTime() != null, TerminalFault::getHappenTime, pageInDTO.getEndTime())
                .orderByDesc(TerminalFault::getUpdateTime);
        Page<TerminalFault> rs = this.page(page, queryWrapper);
        List<TerminalFaultOutDTO> outDTOList = BeanUtil.copyToList(rs.getRecords(), TerminalFaultOutDTO.class);
        Page<TerminalFaultOutDTO> outPage = new Page<>(pageInDTO.getPageNum(), pageInDTO.getPageSize(), rs.getTotal());
        outPage.setRecords(outDTOList);
        return outPage;
    }

    /**
     * 获取终端故障详情
     * @param faultId
     * @return
     */
    @Override
    public TerminalFaultOutDTO getDetailById(Long faultId) {
        log.info("获取终端故障详情-入参:{}", faultId);
        TerminalFault terminalFault = this.getById(faultId);
        if(terminalFault == null){
            return null;
        }
        return BeanUtil.copyProperties(terminalFault, TerminalFaultOutDTO.class);
    }

    /**
     * 处置终端故障
     * @param inDTO
     * @return
     */
    @Override
    public boolean handleFault(TerminalFaultHandleInDTO inDTO) {
        log.info("处置终端故障-入参:{}", inDTO);
        TerminalFault fault = this.getById(inDTO.getFaultId());
        if(fault == null){
            throw new ServiceException("故障ID不存在");
        }
        if(inDTO.getHandleType() == null){
            throw new ServiceException("处置类型不能为空");
        }
        TerminalFault entity = new TerminalFault();
        entity.setFaultId(inDTO.getFaultId());
        entity.setHandleUserId(inDTO.getHandleUserId());
        if(inDTO.getHandleType() == 1){
            // 1-开始处理：未处理 -> 处理中
            entity.setFaultStatus(2);
        } else if(inDTO.getHandleType() == 2){
            // 2-已修复：-> 已修复
            entity.setFaultStatus(3);
            entity.setHandleTime(new Date());
            entity.setHandleResult(inDTO.getHandleResult());
        } else if(inDTO.getHandleType() == 3){
            // 3-已返厂：-> 已返厂
            entity.setFaultStatus(4);
            entity.setHandleTime(new Date());
            entity.setHandleResult(inDTO.getHandleResult());
        } else if(inDTO.getHandleType() == 4){
            // 4-已报废：-> 已报废
            entity.setFaultStatus(5);
            entity.setHandleTime(new Date());
            entity.setHandleResult(inDTO.getHandleResult());
        } else {
            throw new ServiceException("处置类型不合法");
        }
        return this.updateById(entity);
    }
}




