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

import java.util.List;

import com.zhyc.common.annotation.DataScope;
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
    @DataScope(deptAlias = "w" , userAlias = "w")
    public List<WorkOrder> selectWorkOrderList(WorkOrder workOrder)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();

        // 业务类型处理
        if (workOrder.getBizTypes() != null && !workOrder.getBizTypes().isEmpty() &&
                !workOrder.getBizTypes().equals("0")) {
            String[] bizTypeStrs = workOrder.getBizTypes().split(",");
            Integer[] bizTypes = new Integer[bizTypeStrs.length];
            for (int i = 0; i < bizTypeStrs.length; i++) {
                bizTypes[i] = Integer.parseInt(bizTypeStrs[i].trim());
            }
            workOrder.setBizTypeArray(bizTypes);
        }

        // 优先级处理
        if (workOrder.getPrioritys() != null && !workOrder.getPrioritys().isEmpty() &&
                !workOrder.getPrioritys().equals("0")) {
            String[] priorityStrs = workOrder.getPrioritys().split(",");
            Integer[] prioritys = new Integer[priorityStrs.length];
            for (int i = 0; i < priorityStrs.length; i++) {
                prioritys[i] = Integer.parseInt(priorityStrs[i].trim());
            }
            workOrder.setPriorityArray(prioritys);
        }

        // 状态处理
        if (workOrder.getStatuss() != null && !workOrder.getStatuss().isEmpty() &&
                !workOrder.getStatuss().equals("0")) {
            String[] statusStrs = workOrder.getStatuss().split(",");
            Integer[] statuss = new Integer[statusStrs.length];
            for (int i = 0; i < statusStrs.length; i++) {
                statuss[i] = Integer.parseInt(statusStrs[i].trim());
            }
            workOrder.setStatusArray(statuss);
        }

        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);
    }
}
