package com.ruoyi.bst.fault.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.bst.fault.domain.FaultVerifyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.bst.fault.domain.Fault;
import com.ruoyi.bst.fault.domain.FaultQuery;
import com.ruoyi.bst.fault.domain.FaultVO;
import com.ruoyi.bst.fault.domain.enums.FaultHandleStatus;
import com.ruoyi.bst.fault.domain.enums.FaultStatus;
import com.ruoyi.bst.fault.mapper.FaultMapper;
import com.ruoyi.bst.fault.service.FaultService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 故障Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-26
 */
@Service
public class FaultServiceImpl implements FaultService
{
    @Autowired
    private FaultMapper faultMapper;
    @Autowired
    private DeviceService deviceService;

    @Override
    public FaultVO selectFaultById(Long id, boolean scope) {
        if (id == null) {
            return null;
        }
        FaultQuery query = new FaultQuery();
        query.setId(id);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.FAULT_VIEW.getCode());
        return this.selectOne(query);
    }

    @Override
    public FaultVO selectOne(FaultQuery query) {
        PageHelper.startPage(1, 1);
        List<FaultVO> list = faultMapper.selectFaultList(query);
        if(list.isEmpty()) {
            ServiceUtil.assertion(true,"当前故障信息不属于您");
        }
        return CollectionUtils.firstElement(list);
    }


    /**
     * 查询故障列表
     *
     * @param fault 故障
     * @return 故障
     */
    @Override
    public List<FaultVO> selectFaultList(FaultQuery fault)
    {
        return faultMapper.selectFaultList(fault);
    }

    /**
     * 新增故障
     *
     * @param fault 故障
     * @return 结果
     */
    @Override
    public int insertFault(Fault fault)
    {
            fault.setCreateTime(DateUtils.getNowDate());
            fault.setUserId(SecurityUtils.getLoginUser().getUserId());
        DeviceVO vo = deviceService.selectDeviceBySn(fault.getVehicleCode());
        ServiceUtil.assertion(vo==null,"当前设备不存在");
        return faultMapper.insertFault(fault);
    }

    /**
     * 修改故障
     *
     * @param fault 故障
     * @return 结果
     */
    @Override
    public int updateFault(Fault fault)
    {
        return faultMapper.updateFault(fault);
    }

    /**
     * 批量删除故障
     *
     * @param ids 需要删除的故障主键
     * @return 结果
     */
    @Override
    public int deleteFaultByIds(Long[] ids)
    {
        return faultMapper.deleteFaultByIds(ids);
    }

    /**
     * 删除故障信息
     *
     * @param id 故障主键
     * @return 结果
     */
    @Override
    public int deleteFaultById(Long id)
    {
        return faultMapper.deleteFaultById(id);
    }

    @Override
    public FaultVO selectFaultByUserId(Long id) {
        return faultMapper.selectFaultByUserId(id);
    }

    @Override
    public FaultVO handle(Fault fault) {
        FaultVO vo = faultMapper.selectFaultById(fault.getId());
        ServiceUtil.assertion(vo == null,"当前申报信息不存在");
        if (vo.getAppealStatus().equals(FaultStatus.PENDING.getCode()) && fault.getHandle().equals(FaultHandleStatus.REJECTED.getCode())) {
            vo.setAppealStatus(FaultStatus.REJECTED.getCode());
        }else
        if (vo.getAppealStatus().equals(FaultStatus.PENDING.getCode()) && fault.getHandle().equals(FaultHandleStatus.DO.getCode())) {
            vo.setAppealStatus(FaultStatus.REPAIRING.getCode());
        }else
        if (vo.getAppealStatus().equals(FaultStatus.REPAIRING.getCode()) && fault.getHandle().equals(FaultHandleStatus.DO.getCode())) {
            vo.setAppealStatus(FaultStatus.COMPLETED.getCode());
        }
        return vo;
    }

    @Override
    public int verify(FaultVerifyDTO dto) {
        // 查询故障申报
        FaultVO fault = this.selectFaultById(dto.getId());
        ServiceUtil.assertion(fault == null, "当前故障申报信息不存在", dto.getId());
        ServiceUtil.assertion(!FaultStatus.canVerify().contains(fault.getAppealStatus()), "ID为%s的故障申报当前不允许审核", dto.getId());
        // 更新故障申报状态
        boolean pass = dto.getPass() != null && dto.getPass();
        // 更新故障申报状态
        Fault data = new Fault();
        data.setAppealStatus(pass ? FaultStatus.REPAIRING.getCode() : FaultStatus.REJECTED.getCode());
        data.setVerifyEndTime(LocalDateTime.now());
        data.setVerifyRemark(dto.getVerifyRemark());
        data.setVerifyBy(SecurityUtils.getUserId());
        FaultQuery query = new FaultQuery();
        query.setId(dto.getId());
        query.setStatusList(FaultStatus.canVerify());
        int rows = faultMapper.updateByQuery(data, query);
        ServiceUtil.assertion(rows != 1, "ID为%s的故障申报审核失败", dto.getId());
        return rows;
    }

    @Override
    public int complete(FaultVerifyDTO dto) {
        // 查询故障申报
        FaultVO fault = this.selectFaultById(dto.getId());
        ServiceUtil.assertion(fault == null, "当前故障申报信息不存在", dto.getId());
        ServiceUtil.assertion(!FaultStatus.canComplete().contains(fault.getAppealStatus()), "ID为%s的故障申报当前不允许审核", dto.getId());
        // 更新故障申报状态
        boolean pass = dto.getPass() != null && dto.getPass();
        ServiceUtil.assertion(!pass,"当前故障申报不可更新为此状态");
        // 更新故障申报状态
        Fault data = new Fault();
        data.setAppealStatus(FaultStatus.COMPLETED.getCode());
        data.setVerifyEndTime(LocalDateTime.now());
        FaultQuery query = new FaultQuery();
        query.setId(dto.getId());
        query.setStatusList(FaultStatus.canComplete());
        int rows = faultMapper.updateByQuery(data, query);
        ServiceUtil.assertion(rows != 1, "ID为%s的故障申报完成失败", dto.getId());
        return rows;
    }
}
