package com.pm.repairOrder.service.impl;

import java.util.List;

import com.pm.common.core.domain.model.LoginUser;
import com.pm.common.utils.DateUtils;
import com.pm.common.utils.SecurityUtils;
import com.pm.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pm.repairOrder.mapper.RepairOrderMapper;
import com.pm.repairOrder.service.IRepairOrderService;
import com.pm.common.config.PageData;

/**
 * 维修工单Service业务层处理
 *
 * @author pm
 * @date 2025-04-27
 */
@Service
public class RepairOrderServiceImpl implements IRepairOrderService
{
    @Autowired
    private RepairOrderMapper repairOrderMapper;

    /**
     * 查询维修工单
     *
     * @param id 维修工单主键
     * @return 维修工单
     */
    @Override
    public PageData selectRepairOrderById(String id)
    {
        return repairOrderMapper.selectRepairOrderById(id);
    }

    /**
     * 查询维修工单列表
     *
     * @param pd
     * @return 维修工单
     */
    @Override
    public List<PageData> selectRepairOrderList(PageData pd)
    {
        if(pd.containsKey("state")){
            pd.put("userId",SecurityUtils.getLoginUser().getUserId());
        }
        return repairOrderMapper.selectRepairOrderList(pd);
    }

    /**
     * 新增维修工单
     *
     * @param pd
     * @return 结果
     */
    @Override
    public int insertRepairOrder(PageData pd)
    {
        pd.put("id", IdUtils.simpleUUID());
        pd.put("createTime", DateUtils.getTime());
        if(pd.containsKey("finishBy")&&!pd.getString("finishBy").equals("")){
            pd.put("assignTime", DateUtils.getTime());
            pd.put("orderStatus", 1);
        }
        return repairOrderMapper.insertRepairOrder(pd);
    }

    /**
     * 修改维修工单
     *
     * @param pd
     * @return 结果
     */
    @Override
    public int updateRepairOrder(PageData pd)
    {
        pd.put("updateTime", DateUtils.getTime());
        if(pd.containsKey("finishBy")&&!pd.getString("finishBy").equals("")){
            pd.put("assignTime", DateUtils.getTime());
            pd.put("orderStatus", 1);
        }
        if (pd.containsKey("finishTime")&&!pd.getString("finishTime").equals("")){
            pd.put("orderStatus", 2);
        }
        return repairOrderMapper.updateRepairOrder(pd);
    }

    /**
     * 批量删除维修工单
     *
     * @param ids 需要删除的维修工单主键
     * @return 结果
     */
    @Override
    public int deleteRepairOrderByIds(String[] ids)
    {
        return repairOrderMapper.deleteRepairOrderByIds(ids);
    }

    /**
     * 删除维修工单信息
     *
     * @param id 维修工单主键
     * @return 结果
     */
    @Override
    public int deleteRepairOrderById(String id)
    {
        return repairOrderMapper.deleteRepairOrderById(id);
    }
}
