package ${basePackage}.module.system.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 ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.auth.Token;
import ${basePackage}.frame.utils.IDgenerator;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.utils.Message;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.module.system.ent.File;
import ${basePackage}.module.system.mpr.FileMapper;
import ${basePackage}.module.system.req.FileCreateRequest;
import ${basePackage}.module.system.req.FileDeleteRequest;
import ${basePackage}.module.system.req.FileFindRequest;
import ${basePackage}.module.system.req.FileGetRequest;
import ${basePackage}.module.system.rsp.FileCreateResponse;
import ${basePackage}.module.system.rsp.FileDeleteResponse;
import ${basePackage}.module.system.rsp.FileFindResponse;
import ${basePackage}.module.system.rsp.FileGetResponse;

/**
 * FILE - 文件
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class FileManagerImpl implements FileManager {

    @Autowired
    private FileMapper fileMapper;

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

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

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

        long result = fileMapper.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 FileDeleteResponse delete(FileDeleteRequest request, Token token) {
        FileDeleteResponse response = new FileDeleteResponse();

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

        long result = fileMapper.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 响应
     */
    @Transactional(readOnly = true)
    public FileFindResponse find(FileFindRequest request, Token token) {
        FileFindResponse response = new FileFindResponse();

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

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

        return response;
    }

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

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

        File po = fileMapper.get(request, token);

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

        return response;
    }
}
