package com.dxhy.common.service.impl;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.service.MpBaseService;
import com.dxhy.common.util.ExceptionUtil;
import com.dxhy.common.utils.PageUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 基于mybatis plus的单个数据表能完成业务的service需要继承
 *
 * @author jiaohongyang
 */
@Slf4j

public class MpBaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements MpBaseService<T> {

    @Override
    public JSONObject succResult(JSONObject json) {
        json.put(CommonConstants.KEY_ERRCODE, CommonConstants.EX_SUCC);
        json.put(CommonConstants.KEY_ERRMSG, "");
        return json;
    }

    @Override
    public JSONObject errorResult(Exception e) {
       log.info("业务处理异常：{}", e);
        JSONObject resultJson = new JSONObject();
        resultJson.put(CommonConstants.KEY_ERRCODE, CommonConstants.EX_FAIL);
        resultJson.put(CommonConstants.KEY_ERRMSG, ExceptionUtil.getExceptionDeteil(e, 0));
        return resultJson;
    }

    @Override
    public JSONObject pageResult(JSONObject json, PageInfo<T> pageInfo) {
        PageUtils page =
            new PageUtils(pageInfo.getList(), (int)pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());
        json = (JSONObject)JSON.toJSON(page);
        return json;
    }

    @Override
    public JSONObject pageResult(JSONObject json, IPage<T> pageInfo) {
        PageUtils page = new PageUtils(pageInfo);
        json = (JSONObject)JSON.toJSON(page);
        return json;
    }

    @Override
    public JSONObject pageResult(JSONObject json, PageInfo<T> pageInfo, List<Object> returnList) {
        PageUtils page =
            new PageUtils(returnList, (int)pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());
        json = (JSONObject)JSON.toJSON(page);
        return json;
    }

    @Override
    public String responseResult(JSONObject json) {
        String jsonString = null;
        if (json != null) {
            log.debug("返回结果：{}", json.toJSONString());
            jsonString = Base64.encode(json.toJSONString());
        }
        return jsonString;
    }

    @Override
    public JSONObject setTitleList(String[] titlelist, String[] titleprop) {
        JSONObject titleList = new JSONObject();
        if (titlelist != null) {
            titleList.put("titlelist", titlelist);
        }
        if (titleprop != null) {
            titleList.put("titleprop", titleprop);
        }

        JSONObject returnJson = new JSONObject();
        returnJson.put("titlelist", titleList);
        return returnJson;
    }

    /**
    * @Description 代码号码生产uuid
    * @Author wangruwei
    * @Date 2023/10/13 16:08
    **/
    public String getInvoiceUuid(String fpdm,String fphm){
        return (StringUtils.isEmpty(fpdm)?"":fpdm )+ fphm;
    }

}
