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.Locations;
import xyz.wbsite.module.admin.mpr.LocationsMapper;
import xyz.wbsite.module.admin.req.LocationsCreateRequest;
import xyz.wbsite.module.admin.req.LocationsDeleteRequest;
import xyz.wbsite.module.admin.req.LocationsFindRequest;
import xyz.wbsite.module.admin.req.LocationsUpdateRequest;
import xyz.wbsite.module.admin.rsp.LocationsCreateResponse;
import xyz.wbsite.module.admin.rsp.LocationsDeleteResponse;
import xyz.wbsite.module.admin.rsp.LocationsFindResponse;
import xyz.wbsite.module.admin.rsp.LocationsUpdateResponse;
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;

/**
 * LOCATIONS - 路径配置
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-07
 */
@Transactional
@Service
public class LocationsManagerImpl implements LocationsManager {

    @Autowired
    private LocationsMapper locationsMapper;

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

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

        {// 配置标题唯一检查
            LocationsFindRequest locationsFindRequest = new LocationsFindRequest();
            locationsFindRequest.setLocalTitle(request.getLocalTitle());
            LocationsFindResponse locationsFindResponse = this.find(locationsFindRequest, token);
            if (locationsFindResponse.hasError()) {
                response.addErrors(locationsFindResponse.getErrors());
                return response;
            } else if (locationsFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[localTitle]配置标题已存在，请检查!");
                return response;
            }
        }

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

        long result = locationsMapper.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 LocationsDeleteResponse delete(LocationsDeleteRequest request, Token token) {
        LocationsDeleteResponse response = new LocationsDeleteResponse();

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

        long result = locationsMapper.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 LocationsUpdateResponse update(LocationsUpdateRequest request, Token token) {
        LocationsUpdateResponse response = new LocationsUpdateResponse();

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

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

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

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

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

        return response;
    }
}
