package org.nl.system.service.logicflow.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.exception.BadRequestException;
import org.nl.common.utils.SecurityUtils;
import org.nl.system.service.logicflow.IStageService;
import org.nl.system.service.logicflow.dao.Stage;
import org.nl.system.service.logicflow.dao.mapper.StageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 舞台主表 服务实现类
 * </p>
 *
 * @author generator
 * @since 2023-05-08
 */
@Service
public class StageServiceImpl extends ServiceImpl<StageMapper, Stage> implements IStageService {

    @Autowired
    private StageMapper stageMapper;

    @Override
    public IPage<Stage> queryAll(Map whereJson, PageQuery page) {
        LambdaQueryWrapper<Stage> lam = new LambdaQueryWrapper<>();
        IPage<Stage> pages = new Page<>(page.getPage() + 1, page.getSize());
        stageMapper.selectPage(pages, lam);
        return pages;
    }

    @Override
    public void create(Stage entity) {
        Stage stage = stageMapper.selectOne(new LambdaQueryWrapper<Stage>().eq(Stage::getStage_code, entity.getStage_code()));
        if (ObjectUtil.isNotEmpty(stage)) {
            throw new BadRequestException("舞台编码[" + entity.getStage_code() + "]已存在");
        }
        String currentUserId = SecurityUtils.getCurrentUserId();
        String nickName = SecurityUtils.getCurrentNickName();
        String now = DateUtil.now();

        entity.setStage_uuid(IdUtil.simpleUUID());
        entity.setCreate_id(currentUserId);
        entity.setCreate_name(nickName);
        entity.setCreate_time(now);
        entity.setUpdate_id(currentUserId);
        entity.setUpdate_name(nickName);
        entity.setUpdate_time(now);
        stageMapper.insert(entity);
    }

    @Override
    public void update(Stage dto) {
        Stage entity = stageMapper.selectOne(new LambdaQueryWrapper<Stage>().eq(Stage::getStage_uuid, dto.getStage_uuid()));
        if (entity == null) throw new BadRequestException("被删除或无权限，操作失败!");

        String currentUsername = SecurityUtils.getCurrentUsername();
        String currentUserId = SecurityUtils.getCurrentUserId();
        String now = DateUtil.now();
        dto.setUpdate_time(now);
        dto.setUpdate_id(currentUserId);
        dto.setUpdate_name(currentUsername);
        stageMapper.updateById(dto);
    }

    @Override
    public void deleteAll(Set<String> ids) {
        stageMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Stage> selectList() {
        List<Stage> stages = stageMapper.selectList(new LambdaQueryWrapper<Stage>()
                .eq(Stage::getIs_delete, "0")
                .eq(Stage::getIs_active, "1"));
        return stages;
    }

    @Override
    public void addNewStage(Stage dto) {
        Stage stage = stageMapper.selectOne(new LambdaQueryWrapper<Stage>().eq(Stage::getStage_code, dto.getStage_code()));
        if (ObjectUtil.isEmpty(stage)) {
            throw new BadRequestException("舞台不存在");
        }
        // 设置内容
        stage.setStage_data(dto.getStage_data());
        // 获取当前用户与时间
        String currentUsername = SecurityUtils.getCurrentUsername();
        String currentUserId = SecurityUtils.getCurrentUserId();
        String now = DateUtil.now();
        stage.setUpdate_time(now);
        stage.setUpdate_name(currentUsername);
        stage.setUpdate_id(currentUserId);
        stageMapper.updateById(stage);
    }
}
