package com.trade.service.enterprise.companyrelated.service.impl;

import java.util.List;

import com.trade.common.utils.DateUtils;
import com.trade.service.enterprise.companyrelated.domain.vo.ImporterEntityDict;
import com.trade.service.enterprise.companyrelated.domain.vo.ImporterEntityVO;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trade.service.enterprise.companyrelated.domain.ImporterEntity;
import com.trade.service.enterprise.companyrelated.domain.vo.ImporterEntitySearch;
import com.trade.service.enterprise.companyrelated.mapper.ImporterEntityMapper;
import com.trade.service.enterprise.companyrelated.service.IImporterEntityService;

/**
 * <p>
 * 进口商企业信息 服务实现类
 * </p>
 *
 * @author zhangchenm
 * @since 2024-09-03
 */
@Service
public class ImporterEntityServiceImpl extends ServiceImpl<ImporterEntityMapper, ImporterEntity> implements IImporterEntityService {

    @Override
    public PageInfo<ImporterEntityVO> queryListBySearch(ImporterEntitySearch search) {
        // 使用PageHelper分页插件
        PageHelper.startPage(search.getPage(), search.getRows());
        List<ImporterEntityVO> list = getBaseMapper().queryListBySearch(search);
        return new PageInfo<>(list);
    }

    @Override
    public List<ImporterEntity> getListByUserId(Long userId) {
        QueryWrapper<ImporterEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_user_id", userId);
        return this.list(queryWrapper);
    }

    @Override
    public ImporterEntity getByEntityId(Long entityId) {
        return this.getById(entityId);
    }

    @Override
    public ImporterEntityVO getVOByEntityId(Long entityId) {
        ImporterEntityVO entityVO = this.getBaseMapper().getVOByEntityId(entityId);
        if (entityVO != null) {
            entityVO.setPeriodToBeApplied(DateUtils.transPeriodUnit(entityVO.getPeriodToBeApplied()));
            entityVO.setTradingLife(DateUtils.transPeriodUnit(entityVO.getTradingLife()));
        }
        return entityVO;
    }

    @Override
    public List<ImporterEntity> getEnabledListByUserId(Long userId) {
        QueryWrapper<ImporterEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_user_id", userId);
        queryWrapper.eq("status", 1);
        return this.list(queryWrapper);
    }

    @Override
    public List<ImporterEntityDict> getList4Dict() {
        return this.getBaseMapper().getList4Dict();
    }

    @Override
    public ImporterEntity getByEntityNo(String entityNo) {
        return this.lambdaQuery().eq(ImporterEntity::getEntityNo, entityNo).one();
    }

    @Override
    public ImporterEntity getByCertNo(String certNo) {
        return this.lambdaQuery().eq(ImporterEntity::getCertNo, certNo).one();
    }
}
