package com.zhyc.module.work.service.impl;

import java.util.List;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.work.mapper.WorkOrderMapper;
import com.zhyc.module.work.domain.WorkOrder;
import com.zhyc.module.work.service.IWorkOrderService;

/**
 * 派工单Service业务层处理
 * 
 * @author piaobo
 * @date 2025-12-07
 */
@Service
public class WorkOrderServiceImpl implements IWorkOrderService 
{
    @Autowired
    private WorkOrderMapper workOrderMapper;

    /**
     * 查询派工单
     * 
     * @param id 派工单主键
     * @return 派工单
     */
    @Override
    public WorkOrder selectWorkOrderById(Long id)
    {
        return workOrderMapper.selectWorkOrderById(id);
    }

    /**
     * 查询派工单列表
     * 
     * @param workOrder 派工单
     * @return 派工单
     */
    @Override
    public List<WorkOrder> selectWorkOrderList(WorkOrder workOrder)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        return workOrderMapper.selectWorkOrderList(workOrder);
    }

    /**
     * 新增派工单
     * 
     * @param workOrder 派工单
     * @return 结果
     */
    @Override
    public int insertWorkOrder(WorkOrder workOrder)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        Long usrId = SecurityUtils.getLoginUser().getUserId();
        workOrder.setIssuer(username);
        workOrder.setIssuerId(usrId);
        workOrder.setCreateTime(DateUtils.getNowDate());
        return workOrderMapper.insertWorkOrder(workOrder);
    }

    /**
     * 修改派工单
     * 
     * @param workOrder 派工单
     * @return 结果
     */
    @Override
    public int updateWorkOrder(WorkOrder workOrder)
    {
        if (workOrder.getStatus() != null && workOrder.getStatus()>3){
            String username = SecurityUtils.getLoginUser().getUser().getNickName();
            Long usrId = SecurityUtils.getLoginUser().getUserId();
            workOrder.setIssuer(username);
            workOrder.setIssuerId(usrId);
        }
        workOrder.setUpdateTime(DateUtils.getNowDate());
        return workOrderMapper.updateWorkOrder(workOrder);
    }

    /**
     * 批量删除派工单
     * 
     * @param ids 需要删除的派工单主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderByIds(Long[] ids)
    {
        return workOrderMapper.deleteWorkOrderByIds(ids);
    }

    /**
     * 删除派工单信息
     * 
     * @param id 派工单主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderById(Long id)
    {
        return workOrderMapper.deleteWorkOrderById(id);
    }
}
