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.DictItem;
import ${basePackage}.module.system.mpr.DictItemMapper;
import ${basePackage}.module.system.req.DictItemCreateRequest;
import ${basePackage}.module.system.req.DictItemDeleteRequest;
import ${basePackage}.module.system.req.DictItemFindRequest;
import ${basePackage}.module.system.req.DictItemGetRequest;
import ${basePackage}.module.system.req.DictItemUpdateRequest;
import ${basePackage}.module.system.rsp.DictItemCreateResponse;
import ${basePackage}.module.system.rsp.DictItemDeleteResponse;
import ${basePackage}.module.system.rsp.DictItemFindResponse;
import ${basePackage}.module.system.rsp.DictItemGetResponse;
import ${basePackage}.module.system.rsp.DictItemUpdateResponse;

/**
 * DICT_ITEM - 字典项
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class DictItemManagerImpl implements DictItemManager {

    @Autowired
    private DictItemMapper dictItemMapper;

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

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

        {// 检查字典项是否重复
            DictItemFindRequest dictItemFindRequest = new DictItemFindRequest();
            dictItemFindRequest.setDictName(request.getDictName());
            dictItemFindRequest.setKey(request.getKey());
            dictItemFindRequest.setValid(true);
            DictItemFindResponse dictItemFindResponse = this.find(dictItemFindRequest, token);
            if (dictItemFindResponse.hasError()) {
                response.addErrors(dictItemFindResponse.getErrors());
                return response;
            } else if (dictItemFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.BUSINESS_ERROR, "字典项重复");
                return response;
            }
        }

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

        long result = dictItemMapper.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 DictItemDeleteResponse delete(DictItemDeleteRequest request, Token token) {
        DictItemDeleteResponse response = new DictItemDeleteResponse();

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

        long result = dictItemMapper.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 DictItemUpdateResponse update(DictItemUpdateRequest request, Token token) {
        DictItemUpdateResponse response = new DictItemUpdateResponse();

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

        long result = dictItemMapper.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 DictItemFindResponse find(DictItemFindRequest request, Token token) {
        DictItemFindResponse response = new DictItemFindResponse();

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

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

        return response;
    }

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

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

        DictItem po = dictItemMapper.get(request, token);

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

        return response;
    }
}
