package com.chy.borrow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chy.borrow.entity.TmpCode;
import com.chy.borrow.mapper.TmpCodeMapper;
import com.chy.borrow.service.TmpCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 临时编号 服务实现类
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-03-11
 */
@Service
public class TmpCodeServiceImpl extends ServiceImpl<TmpCodeMapper, TmpCode> implements TmpCodeService {

    @Autowired
    TmpCodeMapper tmpCodeMapper;

    @Override
    public String getList(Map map) {
        try {
            Integer current = Integer.valueOf(map.get("current").toString());
            Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
            PageHelper.startPage(current, pageSize);
            Page<Map> list = tmpCodeMapper.getList(map);
            if (list!=null) {
                return new ResultMsg<>(ResultState.SUCCESS, null, JSONObject.toJSONString(list), list.getPageNum(), list.getTotal(), list.getPageSize(), list.getPages()).toPageJSON();
            }
        }catch (Exception e){
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("服务器出错").toJSON();
        }
        return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("无数据").toJSON();
    }

    @Override
    public String getInfoById(TmpCode tmpCode) {
        Map map = tmpCodeMapper.getInfoById(tmpCode.getTmpCode());
        List result = new ArrayList();
        if (map!=null){
            result.add(map);
        }
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(result).toJSON();
    }

}
