package com.example.module.admin.mgr;

import com.example.frame.utils.IDgenerator;
import com.example.frame.utils.Message;
import com.example.frame.base.ErrorType;
import com.example.frame.base.Token;
import com.example.frame.utils.MapperUtil;
import com.example.frame.utils.ValidationUtil;
import com.example.module.admin.ent.Mapping;
import com.example.module.admin.req.MappingCreateRequest;
import com.example.module.admin.req.MappingDeleteRequest;
import com.example.module.admin.req.MappingFindRequest;
import com.example.module.admin.req.MappingUpdateRequest;
import com.example.module.admin.rsp.MappingCreateResponse;
import com.example.module.admin.rsp.MappingDeleteResponse;
import com.example.module.admin.rsp.MappingFindResponse;
import com.example.module.admin.rsp.MappingUpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.wbsite.wsqlite.ObjectClient;
import xyz.wbsite.wsqlite.Where;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * MAPPING - 映射
 *
 * @author author
 * @version 0.0.1
 * @since 2019-09-28
 */
@Service
public class MappingManagerImpl implements MappingManager {

    @Autowired
    private ObjectClient objectClient;

    /**
     * 插入
     *
     * @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);
        entity.setCreateTime(new Date());
        try {
            int insert = objectClient.insert(Mapping.class, entity);
            if (insert != 1) {
                response.addError(ErrorType.BUSINESS_ERROR, "新增错误");
                return response;
            }
            response.setId(id);
        } catch (SQLException | ClassNotFoundException e) {
            response.addError(ErrorType.BUSINESS_ERROR, "新增错误");
        }

        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;
        }

        try {
            int update = objectClient.delete(Mapping.class, Where.builder().eq("ID", request.getId()).build());
            response.setResult((long) update);
        } catch (SQLException | ClassNotFoundException e) {
            response.addError(ErrorType.BUSINESS_ERROR, "删除错误");
        }


        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;
        }

        Mapping mapping = MapperUtil.map(request, Mapping.class);

        try {
            int update = objectClient.update(Mapping.class, mapping, Where.builder().eq("ID", request.getId()).build());
            response.setResult((long) update);
        } catch (SQLException | ClassNotFoundException e) {
            response.addError(ErrorType.BUSINESS_ERROR, "更新错误");
        }

        return response;
    }

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

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

        try {
            List<Mapping> select = objectClient.select(Mapping.class, Where.builder().build(), 1, 0);
            response.setResult(select);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            response.addError(ErrorType.BUSINESS_ERROR, "Sqlite错误");
        }

        return response;
    }
}
