package com.boman.web.core.service.delete;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.redis.RedisKey;
import com.boman.common.redis.service.RedisService;
import com.boman.domain.GenTable;
import com.boman.domain.GenTableColumn;
import com.boman.domain.dto.RowResult;
import com.boman.web.core.mapper.StandardMapper;
import com.boman.web.core.utils.ColumnUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;
import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;

/**
 * @author shiqian
 * @date 2021年03月26日 11:16
 **/
@Service
public class BaseDeleteServiceImpl implements IBaseDeleteService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseDeleteServiceImpl.class);

    @Autowired
    private StandardMapper mapper;
    @Autowired
    private RedisService redisService;

    /**
     * 功能描述: 在此不对入参进行校验，所以在调用此方法之前一定要对参数进行校验，以免报错
     *
     * @param idArr     需要删除的iD
     * @param tableName 业务主表
     * @param pkName    业务表的主键名
     * @return com.boman.system.common.RowResult
     */
    @Override
    public RowResult objectDelete(Long[] idArr, String tableName, String pkName) {
        int delete = mapper.deleteByIdList(tableName, Arrays.asList(idArr), pkName);
        return RowResult.ok("共删除了 " + delete + " 条记录");
    }

    /**
     * 功能描述: 在此不对入参进行校验，所以在调用此方法之前一定要对参数进行校验，以免报错
     *
     * @param id        需要删除的iD
     * @param tableName 业务主表
     * @param pkName    业务表的主键名
     * @return com.boman.system.common.RowResult
     */
    @Override
    public RowResult deleteById(String tableName, String pkName, Long id) {
        int delete = mapper.deleteByIdList(tableName, Lists.newArrayList(id), pkName);
        return delete > 0 ? RowResult.ok() : RowResult.fail();
    }

    /**
     * 功能描述: 在此不对入参进行校验，所以在调用此方法之前一定要对参数进行校验，以免报错
     *
     * @param idArr     需要删除的iD
     * @param tableName 业务主表
     * @param pkName    业务表的主键名
     * @param model     逻辑删除的字段和值
     * @return com.boman.system.common.RowResult
     */
    @Override
    public RowResult objectLogicDelete(Long[] idArr, String tableName, String pkName, JSONObject model) {
        int delete = mapper.objectLogicDelete(tableName, pkName, Arrays.asList(idArr), model);
        return delete > 0 ? RowResult.ok() : RowResult.fail();
    }

    /**
     * 功能描述: 通用删除数据
     *
     * @param tableName tableName
     * @param condition condition
     * @return int
     */
    @Override
    public int delete(String tableName, JSONObject condition) {
        requireNonNull(tableName, "tableName is empty");
        requireNonNull(condition, "condition is empty");

        GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
        List<GenTableColumn> columns = genTable.getColumns();
        ColumnUtils.checkColumn(condition, columns);
        JSONObject packColCondition = ColumnUtils.packColCondition(columns, condition);
        Integer delete = mapper.delete(tableName, condition, packColCondition);
        return isEmpty(delete) ? 0 : delete;
    }
}
