package com.tuoheng.admin.service.inspection.update;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.conver.InspectionConverMapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.entity.Section;
import com.tuoheng.admin.enums.code.dept.AddDeptCodeEnum;
import com.tuoheng.admin.enums.code.inspection.EditInspectionCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.request.inspection.EditInspectionRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.DateUtils;
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-11-24
 */
@Slf4j
@Service
public class UpdateInspectionService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    /**
     * 添加任务
     *
     * @return
     */
    public JsonResult update(EditInspectionRequest editInspectionRequest) {
        log.info("进入修改任务业务接口");
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, editInspectionRequest);
        if (0 != result.getCode()) {
            log.info("修改任务业务接口：校验失败：{}", result.getMsg());
            return result;
        }

        Inspection inspection = InspectionConverMapper.INSTANCE.fromEditInspectionRequestToInspection(editInspectionRequest);
        inspection.setUpdateUser("");
        inspection.setUpdateTime(DateUtils.now());

        Integer rowId = inspectionMapper.update(inspection);
        if (rowId <= 0) {
            log.info("修改任务业务接口：修改任务失败：{}", result.getMsg());
            return JsonResult.error(AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getCode(), AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getMsg());
        }

        log.info("修改任务业务接口：修改任务成功:{}", inspection);
        return JsonResult.success();
    }

    /**
     * 检查参数
     * @param tenantId
     * @param editInspectionRequest
     * @return
     */
    private JsonResult check(String tenantId, EditInspectionRequest editInspectionRequest) {
        if (StringUtils.isEmpty(editInspectionRequest.getId())) {
            return JsonResult.error(EditInspectionCodeEnum.ID_IS_NULL.getCode(), EditInspectionCodeEnum.ID_IS_NULL.getMsg());
        }

        if (StringUtils.isEmpty(editInspectionRequest.getName())) {
            return JsonResult.error(EditInspectionCodeEnum.NAME_IS_NULL.getCode(), EditInspectionCodeEnum.NAME_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getType()) {
            return JsonResult.error(EditInspectionCodeEnum.TYPE_IS_NULL.getCode(), EditInspectionCodeEnum.TYPE_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getRoadId() || StringUtils.isEmpty(editInspectionRequest.getRoadName())) {
            return JsonResult.error(EditInspectionCodeEnum.ROAD_IS_NULL.getCode(), EditInspectionCodeEnum.ROAD_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getSectionId() || StringUtils.isEmpty(editInspectionRequest.getSectionName()) ) {
            return JsonResult.error(EditInspectionCodeEnum.SECTION_IS_NULL.getCode(), EditInspectionCodeEnum.SECTION_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getInspectionType()) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_TYPE_IS_NULL.getCode(), EditInspectionCodeEnum.INSPECTION_TYPE_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getAirportId() || StringUtils.isEmpty(editInspectionRequest.getAirportName())) {
            return JsonResult.error(EditInspectionCodeEnum.AIRPORT_IS_NULL.getCode(), EditInspectionCodeEnum.AIRPORT_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getInspectionLine() || StringUtils.isEmpty(editInspectionRequest.getInspectionLineName())) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_LINE_IS_NULL.getCode(), EditInspectionCodeEnum.INSPECTION_LINE_IS_NULL.getMsg());
        }

        if (null == editInspectionRequest.getInspectionTime()) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_TIME_IS_NULL.getCode(), EditInspectionCodeEnum.INSPECTION_TIME_IS_NULL.getMsg());
        }

        Integer roadCount = roadInformationMapper.selectCount(Wrappers.<RoadInformation>lambdaQuery()
                            .eq(RoadInformation::getTenantId, tenantId)
                            .eq(RoadInformation::getId, editInspectionRequest.getRoadId())
                            .eq(RoadInformation::getMark, 1));

        if (roadCount <= 0) {
            return JsonResult.error(EditInspectionCodeEnum.ROAD_IS_NOT_EXIST.getCode(), EditInspectionCodeEnum.ROAD_IS_NOT_EXIST.getMsg());
        }

        Integer sectionCount = sectionMapper.selectCount(Wrappers.<Section>lambdaQuery()
                                .eq(Section::getTenantId, tenantId)
                                .eq(Section::getId, editInspectionRequest.getSectionId())
                                .eq(Section::getMark, 1));
        if (sectionCount <= 0) {
            return JsonResult.error(EditInspectionCodeEnum.SECTION_IS_NOT_EXIST.getCode(), EditInspectionCodeEnum.SECTION_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success();
    }


}
