package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.annotation.MarsDataScope;
import com.ktg.common.core.text.Convert;
import com.ktg.common.utils.DateUtils;
import com.ktg.iscs.domain.IsUserWorkstation;
import com.ktg.iscs.domain.IsWorkstation;
import com.ktg.iscs.mapper.IsWorkstationMapper;
import com.ktg.iscs.service.IIsUserWorkstationService;
import com.ktg.iscs.service.IIsWorkstationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * mars岗位Service业务层处理
 *
 * @author cgj
 * @date 2024-12-16
 */
@Service
public class IsWorkstationServiceImpl extends ServiceImpl<IsWorkstationMapper, IsWorkstation> implements IIsWorkstationService
{
    @Autowired
    private IsWorkstationMapper isWorkstationMapper;
    @Autowired
    private IIsUserWorkstationService iIsUserWorkstationService;

    /**
     * 查询mars岗位
     *
     * @param workstationId mars岗位主键
     * @return mars岗位
     */
    @Override
    public IsWorkstation selectIsWorkstationByWorkstationId(Long workstationId)
    {
        return isWorkstationMapper.selectIsWorkstationByWorkstationId(workstationId);
    }

    /**
     * 查询mars岗位列表
     *
     * @param isWorkstation mars岗位
     * @return mars岗位
     */
    @Override
    public List<IsWorkstation> selectIsWorkstationList(IsWorkstation isWorkstation)
    {
        return isWorkstationMapper.selectIsWorkstationList(isWorkstation);
    }

    /**
     * 新增mars岗位
     *
     * @param isWorkstation mars岗位
     * @return 结果
     */
    @Override
    public int insertIsWorkstation(IsWorkstation isWorkstation)
    {
        // 断言校验
        Assert.isTrue(StringUtils.isNotBlank(isWorkstation.getWorkstationName()), "岗位名称不可为空!");
        // Assert.isTrue(StringUtils.isNotBlank(isWorkstation.getWorkstationCode()), "岗位编号名称不可为空!");
        List<IsWorkstation> list = list(Wrappers.<IsWorkstation>lambdaQuery()
                .eq(IsWorkstation::getWorkstationName, isWorkstation.getWorkstationName()));
        Assert.isTrue(list.isEmpty(), "该岗位名称已被使用!");
        IsWorkstation one =getById(isWorkstation.getParentId());
        String ancestors;
        if (one != null) {
            ancestors = one.getAncestors() + "," + isWorkstation.getParentId();
        } else {
            ancestors = "0";
        }
        isWorkstation.setAncestors(ancestors);
        return isWorkstationMapper.insertIsWorkstation(isWorkstation);
    }

    /**
     * 修改mars岗位
     *
     * @param isWorkstation mars岗位
     * @return 结果
     */
    @Override
    public int updateIsWorkstation(IsWorkstation isWorkstation)
    {
        // 断言校验
        Assert.notNull(isWorkstation.getWorkstationId(), "岗位ID不可为空!");
        Assert.isTrue(StringUtils.isNotBlank(isWorkstation.getWorkstationName()), "岗位名称不可为空!");
        // Assert.isTrue(StringUtils.isNotBlank(isWorkstation.getWorkstationCode()), "岗位编号名称不可为空!");
        List<IsWorkstation> list = list(Wrappers.<IsWorkstation>lambdaQuery()
                .eq(IsWorkstation::getWorkstationName, isWorkstation.getWorkstationName())
                .ne(IsWorkstation::getWorkstationId, isWorkstation.getWorkstationId()));
        Assert.isTrue(list.isEmpty(), "该岗位名称已被使用!");
        isWorkstation.setUpdateTime(DateUtils.getNowDate());
        return isWorkstationMapper.updateIsWorkstation(isWorkstation);
    }

    /**
     * 批量删除mars岗位
     *
     * @param workstationIds 需要删除的mars岗位主键
     * @return 结果
     */
    @Override
    public int deleteIsWorkstationByWorkstationIds(String workstationIds)
    {
        Assert.notBlank(workstationIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(workstationIds);
        List<IsUserWorkstation> list = iIsUserWorkstationService.list(Wrappers.<IsUserWorkstation>lambdaQuery()
                .in(IsUserWorkstation::getWorkstationId, longIds));
        Assert.isTrue(list.isEmpty(), "岗位正在被用户使用，无法删除！");
        List<IsWorkstation> isWorkstationList = list(Wrappers.<IsWorkstation>lambdaQuery().in(IsWorkstation::getParentId, longIds));
        Assert.isTrue(isWorkstationList.isEmpty(), "岗位下有子岗位，无法删除！");
        return isWorkstationMapper.deleteIsWorkstationByWorkstationIds(longIds);
    }

    /**
     * 删除mars岗位信息
     *
     * @param workstationId mars岗位主键
     * @return 结果
     */
    @Override
    public int deleteIsWorkstationByWorkstationId(Long workstationId)
    {
        return isWorkstationMapper.deleteIsWorkstationByWorkstationId(workstationId);
    }

    @Override
    @MarsDataScope(workstationAlias = "w", userAlias = "u")
    public Page<IsWorkstation> getIsWorkstationPage(IsWorkstation isWorkstation, Page<IsWorkstation> page) {
        List<Long> workstationIds = new ArrayList<>();
        if (isWorkstation.getUserId() != null) {
            List<IsUserWorkstation> list = iIsUserWorkstationService.list(Wrappers.<IsUserWorkstation>lambdaQuery()
                    .eq(IsUserWorkstation::getUserId, isWorkstation.getUserId()));
            if (!list.isEmpty()) {
                workstationIds = list.stream().map(IsUserWorkstation::getWorkstationId).collect(Collectors.toList());
                if (workstationIds.isEmpty()) {
                    return new Page<>();
                }
            }
        }
        return isWorkstationMapper.getIsWorkstationPage(page, isWorkstation, workstationIds);
    }
}
