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.Services;
import xyz.wbsite.module.admin.mpr.ServicesMapper;
import xyz.wbsite.module.admin.req.ServicesCreateRequest;
import xyz.wbsite.module.admin.req.ServicesDeleteRequest;
import xyz.wbsite.module.admin.req.ServicesFindRequest;
import xyz.wbsite.module.admin.req.ServicesUpdateRequest;
import xyz.wbsite.module.admin.rsp.ServicesCreateResponse;
import xyz.wbsite.module.admin.rsp.ServicesDeleteResponse;
import xyz.wbsite.module.admin.rsp.ServicesFindResponse;
import xyz.wbsite.module.admin.rsp.ServicesUpdateResponse;
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;

/**
 * SERVICES - 虚拟主机
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-07
 */
@Transactional
@Service
public class ServicesManagerImpl implements ServicesManager {

    @Autowired
    private ServicesMapper servicesMapper;

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

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

        {// 主机标题唯一检查
            ServicesFindRequest servicesFindRequest = new ServicesFindRequest();
            servicesFindRequest.setTitle(request.getTitle());
            ServicesFindResponse servicesFindResponse = this.find(servicesFindRequest, token);
            if (servicesFindResponse.hasError()) {
                response.addErrors(servicesFindResponse.getErrors());
                return response;
            } else if (servicesFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[title]主机标题已存在，请检查!");
                return response;
            }
        }

        {// 主机域名唯一检查
            ServicesFindRequest servicesFindRequest = new ServicesFindRequest();
            servicesFindRequest.setDomain(request.getDomain());
            ServicesFindResponse servicesFindResponse = this.find(servicesFindRequest, token);
            if (servicesFindResponse.hasError()) {
                response.addErrors(servicesFindResponse.getErrors());
                return response;
            } else if (servicesFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[domain]主机域名已存在，请检查!");
                return response;
            }
        }

        {// 服务端口唯一检查
            ServicesFindRequest servicesFindRequest = new ServicesFindRequest();
            servicesFindRequest.setPort(request.getPort());
            ServicesFindResponse servicesFindResponse = this.find(servicesFindRequest, token);
            if (servicesFindResponse.hasError()) {
                response.addErrors(servicesFindResponse.getErrors());
                return response;
            } else if (servicesFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[port]服务端口已存在，请检查!");
                return response;
            }
        }

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

        long result = servicesMapper.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 ServicesDeleteResponse delete(ServicesDeleteRequest request, Token token) {
        ServicesDeleteResponse response = new ServicesDeleteResponse();

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

        long result = servicesMapper.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 ServicesUpdateResponse update(ServicesUpdateRequest request, Token token) {
        ServicesUpdateResponse response = new ServicesUpdateResponse();

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

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

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

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

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

        return response;
    }
}
