package xyz.wbsite.module.conf.mgr;

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;
import xyz.wbsite.frame.auth.Token;
import xyz.wbsite.frame.base.ErrorType;
import xyz.wbsite.frame.base.SortType;
import xyz.wbsite.frame.utils.IDgenerator;
import xyz.wbsite.frame.utils.MapperUtil;
import xyz.wbsite.frame.utils.Message;
import xyz.wbsite.frame.utils.ValidationUtil;
import xyz.wbsite.module.conf.ent.Mapping;
import xyz.wbsite.module.conf.mpr.MappingMapper;
import xyz.wbsite.module.conf.req.MappingCreateRequest;
import xyz.wbsite.module.conf.req.MappingDeleteRequest;
import xyz.wbsite.module.conf.req.MappingFindRequest;
import xyz.wbsite.module.conf.req.MappingGetRequest;
import xyz.wbsite.module.conf.req.MappingUpdateRequest;
import xyz.wbsite.module.conf.rsp.MappingCreateResponse;
import xyz.wbsite.module.conf.rsp.MappingDeleteResponse;
import xyz.wbsite.module.conf.rsp.MappingFindResponse;
import xyz.wbsite.module.conf.rsp.MappingGetResponse;
import xyz.wbsite.module.conf.rsp.MappingUpdateResponse;

/**
 * MAPPING - 映射
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-03-18
 */
@Transactional
@Service
public class MappingManagerImpl implements MappingManager {

    @Autowired
    private MappingMapper mappingMapper;

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

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

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

        long result = mappingMapper.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 MappingDeleteResponse delete(MappingDeleteRequest request, Token token) {
        MappingDeleteResponse response = new MappingDeleteResponse();

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

        long result = mappingMapper.delete(request, 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 MappingUpdateResponse update(MappingUpdateRequest request, Token token) {
        MappingUpdateResponse response = new MappingUpdateResponse();

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

        long result = mappingMapper.update(request, 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 MappingFindResponse find(MappingFindRequest request, Token token) {
        MappingFindResponse response = new MappingFindResponse();

        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() != null ? request.getSortType() : SortType.ASC));
        }
        PageInfo<Mapping> pageInfo = new PageInfo<>(mappingMapper.find(request, token));

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

        return response;
    }

    /**
     * 获得对象
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public MappingGetResponse get(MappingGetRequest request, Token token) {
        MappingGetResponse response = new MappingGetResponse();

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

        Mapping po = mappingMapper.get(request, token);

        if (po != null) {
            response.setMapping(po);
        } else {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
        }

        return response;
    }
}
