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.Config;
import xyz.wbsite.module.admin.mpr.ConfigMapper;
import xyz.wbsite.module.admin.req.ConfigCreateRequest;
import xyz.wbsite.module.admin.req.ConfigDeleteRequest;
import xyz.wbsite.module.admin.req.ConfigFindRequest;
import xyz.wbsite.module.admin.req.ConfigUpdateRequest;
import xyz.wbsite.module.admin.rsp.ConfigCreateResponse;
import xyz.wbsite.module.admin.rsp.ConfigDeleteResponse;
import xyz.wbsite.module.admin.rsp.ConfigFindResponse;
import xyz.wbsite.module.admin.rsp.ConfigUpdateResponse;
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 - 配置预设
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-07
 */
@Transactional
@Service
public class ConfigManagerImpl implements ConfigManager {

    @Autowired
    private ConfigMapper configMapper;

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

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

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

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

        long result = configMapper.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 ConfigDeleteResponse delete(ConfigDeleteRequest request, Token token) {
        ConfigDeleteResponse response = new ConfigDeleteResponse();

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

        long result = configMapper.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 ConfigUpdateResponse update(ConfigUpdateRequest request, Token token) {
        ConfigUpdateResponse response = new ConfigUpdateResponse();

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

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

        MapperUtil.map(request, entity);
        long result = configMapper.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 ConfigFindResponse find(ConfigFindRequest request, Token token) {
        ConfigFindResponse response = new ConfigFindResponse();

        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<Config> pageInfo = new PageInfo<>(configMapper.find(request, token));

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

        return response;
    }
}
