package com.njzscloud.supervisory.sys.operationManual.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.sys.operationManual.mapper.OperationManualMapper;
import com.njzscloud.supervisory.sys.operationManual.pojo.OperationManualEntity;
import com.njzscloud.supervisory.sys.operationManual.service.OperationManualService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作记录表
 */
@Slf4j
@Service
public class OperationManualServiceImpl extends ServiceImpl<OperationManualMapper, OperationManualEntity> implements OperationManualService {

    /**
     * 新增
     */
    @Override
    public void add(OperationManualEntity operationManualEntity) {
        isBizObj(operationManualEntity);
        this.save(operationManualEntity);
    }

    /**
     * 修改
     */
    @Override
    public void modify(OperationManualEntity operationManualEntity) {
        this.updateById(operationManualEntity);
    }

    public void isBizObj(OperationManualEntity operationManualEntity) {
        List<OperationManualEntity> list = this.list(Wrappers.lambdaQuery(OperationManualEntity.class)
                .eq(OperationManualEntity::getBizObj, operationManualEntity.getBizObj())
                .eq(OperationManualEntity::getEnabled, Boolean.TRUE)
                .eq(OperationManualEntity::getDeleted, Boolean.FALSE));
        if (null != list && list.size() > 0) {
            throw Exceptions.exception("当前客户类型存在已启用的操作手册");
        }
    }

    /**
     * 删除
     */
    @Override
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    @Override
    public OperationManualEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult<OperationManualEntity> paging(PageParam pageParam, OperationManualEntity operationManualEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<OperationManualEntity>lambdaQuery()
                .like(StrUtil.isNotBlank(operationManualEntity.getTitle()), OperationManualEntity::getTitle, operationManualEntity.getTitle())
                .eq(OperationManualEntity::getDeleted, Boolean.FALSE)
                .eq(operationManualEntity.getEnabled() != null, OperationManualEntity::getEnabled, operationManualEntity.getEnabled())
                .orderByDesc(OperationManualEntity::getCreateTime)));
    }

    /**
     * 启用/禁用
     */
    @Override
    public void enabled(Long id, Boolean enabled) {
        OperationManualEntity entity = new OperationManualEntity();
        entity.setId(id);
        entity.setEnabled(enabled);
        this.updateById(entity);
    }

    @Override
    public List<OperationManualEntity> appShow() {
        return this.list(Wrappers.lambdaQuery(OperationManualEntity.class)
                .eq(OperationManualEntity::getEnabled, Boolean.TRUE)
                .eq(OperationManualEntity::getDeleted, Boolean.FALSE));
    }

}
