package cn.com.ty.lift.enterprise.oa.service;

import cn.com.ty.lift.enterprise.oa.dto.LiftCertificateRequest;
import cn.com.ty.lift.enterprise.oa.dto.LiftCertificateResponse;
import cn.com.ty.lift.enterprise.oa.entity.LiftCertificate;
import cn.com.ty.lift.enterprise.oa.mapper.LiftCertificateMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 操作证 服务类
 * </p>
 *
 * @author wcz
 * @since 2019-12-04
 */
@Service
public class LiftCertificateService extends ServiceImpl<LiftCertificateMapper,LiftCertificate> {

    /**
     * 根据条件分页查询操作证记录
     * @param request 查询参数请求model
     * @return IPage<LiftCertificateResponse>
     */
    public IPage<LiftCertificateResponse> pageByCondition(LiftCertificateRequest request){
        return baseMapper.pageByCondition(request.getPage(),request);
    }

    /**
     * 根据条件总计操作证记录条数
     * @param request 查询参数请求model
     * @return long
     */
    public long countByCondition(LiftCertificateRequest request){
        return baseMapper.countByCondition(request);
    }

    /**
     * 根据用户id和维保公司统计操作证条数
     * @param userId 用户id
     * @param mtCompanyId 维保公司id
     * @return int
     */
    public int countByUserAndMtCompany(Long userId,Long mtCompanyId){
        LambdaQueryWrapper<LiftCertificate> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(LiftCertificate::getOwnerId,userId);
        lambdaQueryWrapper.eq(LiftCertificate::getMtCompanyId, mtCompanyId);
        return count(lambdaQueryWrapper);
    }

    /**
     * 根据id和用户，维保公司查询
     * @param id 操作证id
     * @param ownerId 用户id
     * @param mtCompanyId 维保公司id
     * @return LiftCertificate
     */
    public LiftCertificate findByUserAndMtCompany(Long id, Long ownerId, Long mtCompanyId) {
        LambdaQueryWrapper<LiftCertificate> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(id != null && id > 0, LiftCertificate::getId, id);
        lambdaQueryWrapper.eq(LiftCertificate::getOwnerId, ownerId);
        lambdaQueryWrapper.eq(LiftCertificate::getMtCompanyId, mtCompanyId);
        lambdaQueryWrapper.last("LIMIT 1");
        return getOne(lambdaQueryWrapper);
    }

    /**
     * 根据id查询操作证，带关联信息
     * @param request 参数请求model
     * @return LiftCertificateResponse
     */
    public LiftCertificateResponse infoById(LiftCertificateRequest request){
        return baseMapper.infoById(request);
    }
}