package com.ssj.service.weixin.library.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ssj.bean.weixin.libmy.domain.TbLibManagerAudit;
import com.ssj.dao.weixin.library.dao.IOneBookQueryDao;
import com.ssj.dao.weixin.library.dao.LibManagerAuditDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.service.mapper.LibManagerAuditMapper;
import com.ssj.service.sys.curator.dto.CuratorAuditDTO;
import com.ssj.service.weixin.library.dto.TeacherInfoDTO;
import com.ssj.service.weixin.library.service.LibManagerAuditService;
import com.ssj.utils.MybatisPlusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author Mount Liu
 * @date 2019/2/28
 */
@Service(version = "1.0")
@Transactional
public class LibManagerAuditServiceImpl extends BaseServiceImpl<TbLibManagerAudit, String> implements LibManagerAuditService {

    @Autowired
    private LibManagerAuditDao dao;
    @Autowired
    private IOneBookQueryDao oneBookQueryDao;
    @Autowired
    private LibManagerAuditMapper libManagerAuditMapper;

    @Override
    public PagingAndSortingRepository<TbLibManagerAudit, String> getDao() {
        return dao;
    }

    @Override
    public Page<TbLibManagerAudit> findByPage(Map<String, Object> params, Pageable initPage) {
        Map<String, SearchFilter> filters = SearchFilter.parse(params);
        Specification<TbLibManagerAudit> specification = DynamicSpecifications.bySearchFilter(filters.values(), TbLibManagerAudit.class);
        return dao.findAll(specification, initPage);
    }

    @Override
    public List<TbLibManagerAudit> findByLibManagerId(String libManagerId) {
        return dao.findByLibManagerIdOrderByCreateTimeDesc(libManagerId);
    }

    @Override
    public TbLibManagerAudit findByUserIdAndType(String userId, int type) {
        return dao.findTopByUserIdAndTypeOrderByCreateTimeDesc(userId, type);
    }


    @Override
    public TbLibManagerAudit findNewestOneByUserIdAndType(String userId, int type) {
        return dao.findTopByUserIdAndTypeOrderByCreateTimeDesc(userId, type);
    }

    @Override
    public TbLibManagerAudit findNewestOneByUserIdAndTypeIn(String userId, int[] type) {
        return dao.findTopByUserIdAndTypeInOrderByCreateTimeDesc(userId, type);
    }


    @Override
    public TeacherInfoDTO findMyInfo(String teacherId) {
        return oneBookQueryDao.findMyApplicationInfo(teacherId);
    }

    @Override
    public TbLibManagerAudit findByUserIdAndTypeAndStatus(String teacherId, int type, int status) {
        return dao.findByUserIdAndTypeAndStatus(teacherId, type, status);
    }

    @Override
    public TbLibManagerAudit findNewestOneByLibManagerId(String libManagerId) {
        return dao.findTopByLibManagerIdOrderByCreateTimeDesc(libManagerId);
    }

    @Override
    public com.ssj.framework.basic.common.bean.Page findPageByTypeAndName(int pageNo, int pageSize, int type, String name) {
        IPage<CuratorAuditDTO> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNo, pageSize);
        page = libManagerAuditMapper.findPageByType(page, type, name);
        return MybatisPlusUtil.parsePage(page);
    }

    @Override
    public TbLibManagerAudit findCorrectAuditByUserId(String userId) {
        return dao.findTopByUserIdAndStatusAndTypeInOrderByCreateTimeDesc(userId, 3, new int[]{8, 13});
    }

    @Override
    public void deleteByUserId(String userId) {
         dao.deleteByUserId(userId);
    }

    @Override
    public int countUnOperateNum(){
       return   dao.findUnOperateNum();
    }

    @Override
    public List<TbLibManagerAudit> findAllByTypeAndStatus(int type,int status){
        return dao.findAllByTypeAndStatus(type,status);
    }

    @Override
    public List<TbLibManagerAudit> findByLibManagerIdAndStatus(String libManagerId, int status) {
        return dao.findByLibManagerIdAndStatusOrderByCreateTimeDesc(libManagerId, status);
    }

    @Override
    public List<TbLibManagerAudit> findByLibManagerIdListAndStatus(List<String> libManagerIds, int status) {
        return dao.findByLibManagerIdInAndStatusOrderByCreateTimeDesc(libManagerIds, status);
    }


}
