package com.tuoheng.admin.service.workorder.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.WorkOrder;
import com.tuoheng.admin.enums.code.workorder.QueryWorkOrderDetailsCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 查询工单详情业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-06
 */
@Slf4j
@Service
public class QueryWorkOrderDetailsService {

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private WorkOrderFileMapper workOrderFileMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    public JsonResult getDetails(String id) {
        log.info("进入查询工单详情业务, id:{}", id);

        String userId = CurrentUserUtil.getUserId();

        JsonResult result = this.check(id);
        if (0 != result.getCode()) {
            log.info("进入查询工单详情业务：校验失败：{}", result.getMsg());
            return result;
        }

        WorkOrder workOrder = (WorkOrder) result.getData();

        return JsonResult.success(workOrder);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(String id) {
        // 判断任务id是否为空
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryWorkOrderDetailsCodeEnum.ID_IS_NULL.getCode(), QueryWorkOrderDetailsCodeEnum.ID_IS_NULL.getMsg());
        }

        // 判断工单是否存在
        WorkOrder workOrder = workOrderMapper.selectOne(new LambdaQueryWrapper<WorkOrder>()
                .eq(WorkOrder::getId, id)
                .eq(WorkOrder::getMark, 1));
        if (null == workOrder) {
            return JsonResult.error(QueryWorkOrderDetailsCodeEnum.WORK_ORDER_IS_NOT_EXIST.getCode(), QueryWorkOrderDetailsCodeEnum.WORK_ORDER_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(workOrder);
    }

}
