package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ChargeDetails;
import com.ruoyi.system.domain.WorkOrderFj;
import com.ruoyi.system.mapper.ChargeDetailsMapper;
import com.ruoyi.system.mapper.WorkOrderFjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WorkOrderInfoMapper;
import com.ruoyi.system.domain.WorkOrderInfo;
import com.ruoyi.system.service.IWorkOrderInfoService;

import static com.ruoyi.common.constant.UserConstants.*;

/**
 * 工单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@Service
public class WorkOrderInfoServiceImpl implements IWorkOrderInfoService 
{
    @Autowired
    private WorkOrderInfoMapper workOrderInfoMapper;

    @Autowired
    private WorkOrderFjMapper workOrderFjMapper;

    @Autowired
    private ChargeDetailsMapper chargeDetailsMapper;

    /**
     * 查询工单信息
     * 
     * @param orderId 工单信息主键
     * @return 工单信息
     */
    @Override
    public WorkOrderInfo selectWorkOrderInfoByOrderId(String orderId)
    {
        WorkOrderInfo workOrderInfo = workOrderInfoMapper.selectWorkOrderInfoByOrderId(orderId);
        if(workOrderInfo!=null){
            //查询附件
            WorkOrderFj workOrderFj = new WorkOrderFj();
            workOrderFj.setOrderId(orderId);
            //workOrderFj.setType(USER_DISABLE);
            List<WorkOrderFj> workOrderFjs = workOrderFjMapper.selectWorkOrderFjList(workOrderFj);
            if(workOrderFjs!=null && !workOrderFjs.isEmpty()){
                Map<String,List<WorkOrderFj>> map = workOrderFjs.stream().collect(Collectors.groupingBy(WorkOrderFj::getType));
                workOrderInfo.setWorkOrderFjXqList(map.get(ONE));
                workOrderInfo.setWorkOrderFjQmList(map.get(TWO));
            }
            //查询工单收费详情
            ChargeDetails chargeDetails = new ChargeDetails();
            chargeDetails.setOrderId(orderId);
            List<ChargeDetails> chargeDetails1 = chargeDetailsMapper.selectChargeDetailsList(chargeDetails);
            if(chargeDetails1!=null && !chargeDetails1.isEmpty()){
                workOrderInfo.setChargeDetailsList(chargeDetails1);
            }

        }
        return workOrderInfo;
    }

    /**
     * 查询工单信息列表
     * 
     * @param workOrderInfo 工单信息
     * @return 工单信息
     */
    @Override
    public List<WorkOrderInfo> selectWorkOrderInfoList(WorkOrderInfo workOrderInfo)
    {
        return workOrderInfoMapper.selectWorkOrderInfoList(workOrderInfo);
    }

    /**
     * 新增工单信息
     * 
     * @param workOrderInfo 工单信息
     * @return 结果
     */
    @Override
    public int insertWorkOrderInfo(WorkOrderInfo workOrderInfo)
    {
        workOrderInfo.setCreateTime(DateUtils.getNowDate());
        return workOrderInfoMapper.insertWorkOrderInfo(workOrderInfo);
    }

    /**
     * 修改工单信息
     * 
     * @param workOrderInfo 工单信息
     * @return 结果
     */
    @Override
    public int updateWorkOrderInfo(WorkOrderInfo workOrderInfo)
    {
        if(workOrderInfo.getChargeDetailsList()!=null && !workOrderInfo.getChargeDetailsList().isEmpty()){
            //删除收费信息重新保存
            chargeDetailsMapper.deleteChargeDetailsByOrderId(workOrderInfo.getOrderId());
            //删除之后重新保存
            for (ChargeDetails chargeDetails : workOrderInfo.getChargeDetailsList()) {
                chargeDetails.setOrderId(workOrderInfo.getOrderId());
                chargeDetailsMapper.insertChargeDetails(chargeDetails);
            }
        }
        workOrderInfo.setUpdateTime(DateUtils.getNowDate());
        return workOrderInfoMapper.updateWorkOrderInfo(workOrderInfo);
    }

    /**
     * 批量删除工单信息
     * 
     * @param orderIds 需要删除的工单信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderInfoByOrderIds(String[] orderIds)
    {
        return workOrderInfoMapper.deleteWorkOrderInfoByOrderIds(orderIds);
    }

    /**
     * 删除工单信息信息
     * 
     * @param orderId 工单信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderInfoByOrderId(String orderId)
    {
        return workOrderInfoMapper.deleteWorkOrderInfoByOrderId(orderId);
    }
}
