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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
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.IStageImageService;
import org.nl.system.service.logicflow.dao.StageImage;
import org.nl.system.service.logicflow.dao.mapper.StageImageMapper;
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 StageImageServiceImpl extends ServiceImpl<StageImageMapper, StageImage> implements IStageImageService {

    @Autowired
    private StageImageMapper stageImageMapper;

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

    @Override
    public void create(StageImage entity) {
        String currentUserId = SecurityUtils.getCurrentUserId();
        String nickName = SecurityUtils.getCurrentNickName();
        String now = DateUtil.now();

        entity.setImage_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);
        stageImageMapper.insert(entity);
    }

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

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

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

    @Override
    public List<StageImage> selectList() {
        List<StageImage> stageImages = stageImageMapper.selectList(new LambdaQueryWrapper<StageImage>()
                .eq(StageImage::getIs_delete, "0")
                .eq(StageImage::getIs_active, "1")
                .orderByDesc(StageImage::getUpdate_time));
        stageImages.forEach(stageImage -> stageImage.setImage_name(stageImage.getImage_code().split("-")[0]));
        return stageImages;
    }
}
