package xyz.wbsite.module.admin.mgr;

import xyz.wbsite.frame.utils.IDgenerator;
import xyz.wbsite.frame.utils.Message;
import xyz.wbsite.frame.base.ErrorType;
import xyz.wbsite.frame.base.Token;
import xyz.wbsite.frame.utils.MapperUtil;
import xyz.wbsite.frame.utils.ValidationUtil;
import xyz.wbsite.module.admin.ent.ConfigData;
import xyz.wbsite.module.admin.mpr.ConfigDataMapper;
import xyz.wbsite.module.admin.req.ConfigDataCreateRequest;
import xyz.wbsite.module.admin.req.ConfigDataDeleteRequest;
import xyz.wbsite.module.admin.req.ConfigDataFindRequest;
import xyz.wbsite.module.admin.req.ConfigDataUpdateRequest;
import xyz.wbsite.module.admin.rsp.ConfigDataCreateResponse;
import xyz.wbsite.module.admin.rsp.ConfigDataDeleteResponse;
import xyz.wbsite.module.admin.rsp.ConfigDataFindResponse;
import xyz.wbsite.module.admin.rsp.ConfigDataUpdateResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * CONFIG_DATA - 配置数据
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-08
 */
@Transactional
@Service
public class ConfigDataManagerImpl implements ConfigDataManager {

    @Autowired
    private ConfigDataMapper configDataMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ConfigDataCreateResponse create(ConfigDataCreateRequest request, Token token) {
        ConfigDataCreateResponse response = new ConfigDataCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        {// 配置名称唯一检查
            ConfigDataFindRequest configDataFindRequest = new ConfigDataFindRequest();
            configDataFindRequest.setConfName(request.getConfName());
            ConfigDataFindResponse configDataFindResponse = this.find(configDataFindRequest, token);
            if (configDataFindResponse.hasError()) {
                response.addErrors(configDataFindResponse.getErrors());
                return response;
            } else if (configDataFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[confName]配置名称已存在，请检查!");
                return response;
            }
        }

        long id = IDgenerator.nextId();
        ConfigData entity = MapperUtil.map(request, ConfigData.class);
        entity.setId(id);

        long result = configDataMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ConfigDataDeleteResponse delete(ConfigDataDeleteRequest request, Token token) {
        ConfigDataDeleteResponse response = new ConfigDataDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = configDataMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ConfigDataUpdateResponse update(ConfigDataUpdateRequest request, Token token) {
        ConfigDataUpdateResponse response = new ConfigDataUpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        ConfigData entity = configDataMapper.getById(request.getId(), token);
        if (entity == null) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
            return response;
        }

        MapperUtil.map(request, entity);
        long result = configDataMapper.update(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public ConfigDataFindResponse find(ConfigDataFindRequest request, Token token) {
        ConfigDataFindResponse response = new ConfigDataFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<ConfigData> pageInfo = new PageInfo<>(configDataMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
}
