package org.jeecg.module.custom.ocr.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.module.custom.ocr.base.param.ParamterPage;

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

/**
 * @author zhangyong
 * @title: BaseServiceImpl
 * @projectName jyjz-parent
 * @description: 顶级serviceimpl
 * @date 2021/11/12 6:18 PM
 */
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T>  {

    @Override
    public List<T> selectSearchList(QueryWrapper<T> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public T selectDtoById(Long id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 初始化排序方式
     * @param ordernames 排序字段
     * @param orders     排序方式
     * @return
     */
    private String[] getOrder(String[] ordernames,String[] orders){

        //初始化返回的排序方式数组
        String[] returnOrders = new String[ordernames.length];
        if(null != orders){
            //判断排序方式数组是否小于排序字段
            if(orders.length < ordernames.length){
                System.arraycopy(orders,0,returnOrders,0,orders.length);

                for (int i = orders.length;i < ordernames.length;i++){
                    returnOrders[i] = "asc";
                }
            }else{
                returnOrders = orders;
            }
        }else{

            for (int i = 0;i < ordernames.length;i++){
                returnOrders[i] = "asc";
            }
        }
        return returnOrders;
    }

    /**
     * 格式化排序字段参数，组装排序对象
     * @param paramterPage
     * @return
     */
    public List<OrderItem> formatParamterPage(ParamterPage paramterPage){

        //排序集合
        List<OrderItem> orderItemList = new ArrayList<>();
        //排序字段
        String ordername = paramterPage.getSortname();
        //排序方式
        String order = paramterPage.getSortorder();


        //判断排序字段是否为空
        if(StringUtils.isNotBlank(ordername)){
            //获得排序字段
            String[] ordernames = ordername.split(",");

            //排序方式
            String[] orders = null;
            //判断排序方式是否为空
            if(StringUtils.isNotBlank(order)){
                orders = order.split(",");
            }
            //初始化排序方式
            orders = getOrder(ordernames,orders);

            int j = 0;
            for (String tempOrder:ordernames){
                OrderItem orderItem = new OrderItem();
                orderItem.setColumn(tempOrder);

                if(orders[j].toUpperCase().equals("ASC")){
                    orderItem.setAsc(true);
                }else{
                    orderItem.setAsc(false);
                }
                j++;

                orderItemList.add(orderItem);
            }

        }
        return orderItemList;
    }

    @Override
    public IPage<T> selectSearchListPage(ParamterPage paramterPage,QueryWrapper<T> queryWrapper) {

        Page<T> page = new Page<>(paramterPage.getStart(), paramterPage.getPagesize());

        page.setOrders(this.formatParamterPage(paramterPage));

        return this.baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public int deleteByIdMore(String id) {
        int isok = 0;

        if(StringUtils.isNotBlank(id)){
            String[] ids = id.split(",");

            try{
                QueryWrapper<T> wrapper = new QueryWrapper<>();
                wrapper.in("id",ids);
                isok = this.baseMapper.delete(wrapper);
            }catch (Exception e){
                isok = 0;
            }

        }

        return isok;
    }

}
