package com.org.web.honor.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.org.system.enums.YesOrNoState;
import com.org.utils.SecurityUtil;
import com.org.utils.exception.CustomException;
import com.org.web.honor.domain.BcHonorShow;
import com.org.web.honor.mapper.BcHonorShowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BcHonorShowService extends ServiceImpl<BcHonorShowMapper, BcHonorShow> {

    /**
     * 新增或编辑
     */
    public boolean executeSaveOrUpdate(BcHonorShow bcHonorShow) {
        log.info("BcHonorShowService - executeSaveOrUpdate bcHonorShow:{}", bcHonorShow);

        if (ObjectUtil.isEmpty(bcHonorShow.getId())) {
            bcHonorShow.setCreateBy(SecurityUtil.getUserId());
            bcHonorShow.setCreateTime(new Date());
            bcHonorShow.setIsDelete(YesOrNoState.NO.getState());
        } else {
            bcHonorShow.setUpdateBy(SecurityUtil.getUserId());
            bcHonorShow.setUpdateTime(new Date());
        }
        return saveOrUpdate(bcHonorShow);
    }

    /**
     * 删除
     */
    public boolean del(Long id) {
        log.info("BcHonorShowService - del id:{}", id);

        BcHonorShow bcHonorShow = getById(id);
        if (ObjectUtil.isEmpty(bcHonorShow)) {
            throw new CustomException("删除记录不存在");
        }
        bcHonorShow.setIsDelete(YesOrNoState.YES.getState());
        bcHonorShow.setDeleteBy(SecurityUtil.getUserId());
        bcHonorShow.setDeleteTime(new Date());
        return updateById(bcHonorShow);
    }

    /**
     * 列表查询
     */
    public List<BcHonorShow> listShow(BcHonorShow params) {
        return baseMapper.queryList(params);
    }

    /**
     * 置顶操作
     */
    public void setTop(Long id) {
        log.info("BcHonorShowService - setTop id:{}", id);

        BcHonorShow bcHonorShow = getById(id);
        if (ObjectUtil.isEmpty(bcHonorShow)) {
            throw new CustomException("数据不存在");
        }

        // 取消 其他数据的 置顶状态
        baseMapper.setTop(null, 0);

        // 设置当前数据 为置顶
        baseMapper.setTop(id, 1);

    }
    
}
