package com.sckw.system.repository;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.system.dao.KwsWeighbridgeDao;
import com.sckw.system.model.KwsWeighbridge;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Repository
public class KwsWeighbridgeRepository extends ServiceImpl<KwsWeighbridgeDao, KwsWeighbridge> {

    public IPage<KwsWeighbridge> pageQuery(int pageNum, int pageSize, String weighbridgeName, Collection<Long> entIds) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwsWeighbridge>lambdaQuery()
                .eq(KwsWeighbridge::getDelFlag, 0)
                .like(StringUtils.isNotBlank(weighbridgeName), KwsWeighbridge::getWeighbridgeName, weighbridgeName)
                .in(entIds != null && !entIds.isEmpty(), KwsWeighbridge::getEntId, entIds)
                .orderByDesc(KwsWeighbridge::getCreateTime)
                .orderByDesc(KwsWeighbridge::getId));
    }

    public KwsWeighbridge findAvailableById(Long id) {
        return getOne(Wrappers.<KwsWeighbridge>lambdaQuery()
                .eq(KwsWeighbridge::getId, id)
                .eq(KwsWeighbridge::getDelFlag, 0)
                .last("limit 1"));
    }

    public KwsWeighbridge findByUniqueCode(String uniqueCode) {
        return getOne(Wrappers.<KwsWeighbridge>lambdaQuery()
                .eq(KwsWeighbridge::getUniqueCode, uniqueCode)
                .eq(KwsWeighbridge::getDelFlag, 0)
                .last("limit 1"));
    }

    public List<KwsWeighbridge> listByNameAndCode(String weighbridgeName, String uniqueCode, Collection<Long> entIds) {
        return list(Wrappers.<KwsWeighbridge>lambdaQuery()
                .eq(KwsWeighbridge::getDelFlag, 0)
                .like(StringUtils.isNotBlank(weighbridgeName), KwsWeighbridge::getWeighbridgeName, weighbridgeName)
                .like(StringUtils.isNotBlank(uniqueCode), KwsWeighbridge::getUniqueCode, uniqueCode)
                .in(entIds != null && !entIds.isEmpty(), KwsWeighbridge::getEntId, entIds)
                .orderByDesc(KwsWeighbridge::getCreateTime));
    }

    public boolean updateDiffConfigByEntId(Long entId, String diffConfig, Long updateBy) {
        return update(Wrappers.<KwsWeighbridge>lambdaUpdate()
                .eq(KwsWeighbridge::getEntId, entId)
                .eq(KwsWeighbridge::getDelFlag, 0)
                .set(KwsWeighbridge::getDiffConfig, diffConfig)
                .set(KwsWeighbridge::getUpdateBy, updateBy)
                .set(KwsWeighbridge::getUpdateTime, new java.util.Date()));
    }
}
