package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.sys.dao.algs.ApprovalMapper;
import io.renren.modules.sys.dao.algs.ApprovalVoMapper;
import io.renren.modules.sys.entity.algs.Approval;
import io.renren.modules.sys.entity.algs.Approval_Vo;
import io.renren.modules.sys.service.ApprovalService;
import io.renren.modules.sys.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("approvalService")
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper, Approval> implements ApprovalService {
    @Autowired
    ApprovalVoMapper approvalVoMapper;
    @Autowired
    ApprovalMapper approvalMapper;
    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPageOnUse(Map<String, Object> params) {
        String missName=(String)params.get("missName");
        String classificationtag = (String)params.get("classificationtag");
        Long categoryId = categoryService.getByName(classificationtag);

        List<Approval_Vo> approvalInfo;
        List<Approval_Vo> approvalAllInfo;
        int pageSize = Integer.valueOf((String) params.get("limit"));
        int pageIndex = Integer.valueOf((String) params.get("page"));
        
        //此方法如果修改,则也需要修改下面的获取总页数的方法
        approvalInfo=approvalVoMapper.getApprovalInfo(new QueryWrapper<Approval_Vo>()
                .like(StringUtils.isNotBlank(missName),"miss_name",missName)
                .eq(classificationtag != null, "a.category_id", categoryId)
                .last("limit " + (pageIndex - 1) * pageSize + "," + pageSize + ";")
        );
        //用于查全部，获取总页数。
        approvalAllInfo=approvalVoMapper.getApprovalInfo(new QueryWrapper<Approval_Vo>()
                .like(StringUtils.isNotBlank(missName),"miss_name",missName)
                .eq(classificationtag != null, "a.category_id", categoryId)
        );
        return new PageUtils(approvalInfo,approvalAllInfo.size(),pageSize,pageIndex);
    }

    @Override
    public boolean update(Approval approval) {
        return this.updateById(approval);
    }
}
