package com.ruoyi.drill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.DrillEnum;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.drill.domain.DrillContingent;
import com.ruoyi.drill.domain.DrillOrganizationalStructure;
import com.ruoyi.drill.mapper.DrillContingentMapper;
import com.ruoyi.drill.service.IDrillContingentService;
import com.ruoyi.drill.service.IDrillOrganizationalStructureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * DrillContingentServiceImpl
 *
 * @Auther: mtx
 * @Date: 2023/09/09 9:37
 */
@Service
@Slf4j
public class DrillContingentServiceImpl extends ServiceImpl<DrillContingentMapper, DrillContingent> implements IDrillContingentService {

    @Resource
    private IDrillOrganizationalStructureService drillOrganizationalStructureService;

    @Override
    @Transactional
    public AjaxResult saveAndUpdate(DrillContingent drillContingent) {
        if (Objects.isNull(this.getById(drillContingent.getId()))) {
            if (Objects.isNull(drillContingent.getContingentStatus())) {
                drillContingent.setContingentStatus(DrillEnum.DRILL_CONTINGENT_STATUS_YES.getValue());
            }
            drillContingent.setCreateDefault();
            drillContingent.setId(IdUtils.fastSimpleUUID());

            DrillOrganizationalStructure structure = new DrillOrganizationalStructure();
            structure.setId(IdUtils.fastSimpleUUID());
            structure.setContingentId(drillContingent.getId());
            structure.setDrillAncestors("0");
            structure.setParentId("0");
            structure.setDrillPost("请更新岗位");
            structure.setOrderNum(0L);
            structure.setCreateDefault();
            drillOrganizationalStructureService.save(structure);
        } else {
            drillContingent.setUpdateDefault();
        }
        this.saveOrUpdate(drillContingent);

        return AjaxResult.success();
    }
}
