package com.ruoyi.drill.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.drill.domain.DrillOrganizationalStructure;
import com.ruoyi.drill.domain.DrillOrganizationalStructurePersonnel;
import com.ruoyi.drill.dto.DrillOrganizationalStructureDto;
import com.ruoyi.drill.mapper.DrillOrganizationalStructureMapper;
import com.ruoyi.drill.service.IDrillOrganizationalStructurePersonnelService;
import com.ruoyi.drill.service.IDrillOrganizationalStructureService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * DrillOrganizationalStructureServiceImpl
 *
 * @Auther: mtx
 * @Date: 2023/09/07 17:28
 */
@Service
@Slf4j
public class DrillOrganizationalStructureServiceImpl extends ServiceImpl<DrillOrganizationalStructureMapper, DrillOrganizationalStructure> implements IDrillOrganizationalStructureService {

    @Resource
    private IDrillOrganizationalStructurePersonnelService drillOrganizationalStructurePersonnelService;

    @Override
    @Transactional
    public AjaxResult saveAndUpdate(DrillOrganizationalStructureDto drillOrganizationalStructure) {
        if (StringUtils.isBlank(drillOrganizationalStructure.getContingentId())) {
            return AjaxResult.warn("请选择演习队伍");
        }
        if (StringUtils.isBlank(drillOrganizationalStructure.getDrillPost())) {
            return AjaxResult.warn("请填写应急岗位名称");
        }
        if (Objects.isNull(drillOrganizationalStructure.getPersonList())) {
            return AjaxResult.warn("请选择岗位人员");
        }
        DrillOrganizationalStructure structure = new DrillOrganizationalStructure();
        // 修改 则修改本级 传id
        if (Objects.nonNull(this.getById(drillOrganizationalStructure.getId()))) {
            long count = this.count(Wrappers.<DrillOrganizationalStructure>lambdaQuery()
                    .eq(DrillOrganizationalStructure::getContingentId,drillOrganizationalStructure.getContingentId())
                    .ne(DrillOrganizationalStructure::getId, drillOrganizationalStructure.getId())
                    .eq(DrillOrganizationalStructure::getDrillPost, drillOrganizationalStructure.getDrillPost()));
            if (count > 0) {
                return AjaxResult.warn("应急'" + drillOrganizationalStructure.getDrillPost() + "'已存在");
            }

            structure.setId(drillOrganizationalStructure.getId());
            structure.setIsDel(DeletedEnum.VALID.getValue());
            structure.setUpdateDefault();
            structure.setDrillPost(drillOrganizationalStructure.getDrillPost());
            structure.setOrderNum(drillOrganizationalStructure.getOrderNum());
        } else {
            // 新增 则增加下一级 传父级id
            DrillOrganizationalStructure organizationalStructure = this.getById(drillOrganizationalStructure.getParentId());
            if (Objects.nonNull(organizationalStructure)) {
                long count = this.count(Wrappers.<DrillOrganizationalStructure>lambdaQuery()
                        .eq(DrillOrganizationalStructure::getContingentId,drillOrganizationalStructure.getContingentId())
                        .eq(DrillOrganizationalStructure::getDrillPost, drillOrganizationalStructure.getDrillPost()));
                if (count > 0) {
                    return AjaxResult.warn("应急'" + drillOrganizationalStructure.getDrillPost() + "'已存在");
                }

                structure.setParentId(drillOrganizationalStructure.getParentId());
                structure.setDrillAncestors(organizationalStructure.getDrillAncestors() + "," + drillOrganizationalStructure.getParentId());
                structure.setId(IdUtils.fastSimpleUUID());
                structure.setCreateDefault();
                structure.setContingentId(drillOrganizationalStructure.getContingentId());
                structure.setDrillPost(drillOrganizationalStructure.getDrillPost());
                structure.setOrderNum(drillOrganizationalStructure.getOrderNum());
            } else {
                return AjaxResult.error("数据异常");
            }
        }
        List<Long> personList = drillOrganizationalStructure.getPersonList();
        List<DrillOrganizationalStructurePersonnel> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(personList)) {
            for (Long userId : personList) {
                DrillOrganizationalStructurePersonnel dosp = new DrillOrganizationalStructurePersonnel();
                dosp.setDrillOrganizationalStructureId(structure.getId());
                dosp.setUserId(userId);
                dosp.setCreateDefault();
                list.add(dosp);
            }
        }
        drillOrganizationalStructurePersonnelService.remove(Wrappers.<DrillOrganizationalStructurePersonnel>lambdaQuery()
                .eq(DrillOrganizationalStructurePersonnel::getDrillOrganizationalStructureId, structure.getId()));
        drillOrganizationalStructurePersonnelService.saveBatch(list);
        this.saveOrUpdate(structure);
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult removeOrganizationalStructure(String id) {
        DrillOrganizationalStructure structure = this.getById(id);
        if (Objects.nonNull(structure)) {
            if (Objects.equals("0", structure.getParentId())) {
                return AjaxResult.warn("该数据不允许删除");
            }
            removeFor(id);
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    public void removeFor(String id) {

        List<DrillOrganizationalStructure> list = this.list(Wrappers.<DrillOrganizationalStructure>lambdaQuery().eq(DrillOrganizationalStructure::getParentId, id));

        if (CollectionUtils.isNotEmpty(list)) {
            for (DrillOrganizationalStructure structure : list) {
                String structureId = structure.getId();
                removeFor(structureId);

                this.removeById(structureId);
                drillOrganizationalStructurePersonnelService.remove(Wrappers.<DrillOrganizationalStructurePersonnel>lambdaQuery()
                        .eq(DrillOrganizationalStructurePersonnel::getDrillOrganizationalStructureId, structureId));
            }
        }
        this.removeById(id);
        drillOrganizationalStructurePersonnelService.remove(Wrappers.<DrillOrganizationalStructurePersonnel>lambdaQuery()
                .eq(DrillOrganizationalStructurePersonnel::getDrillOrganizationalStructureId, id));
    }
}
