/**
 * Copyright © 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.test.wdt.merchantCompany.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.test.wdt.merchantCompany.domain.MerchantCompany;
import com.jeeplus.test.wdt.merchantCompany.domain.MerchantProduct;
import com.jeeplus.test.wdt.merchantCompany.mapper.MerchantProductMapper;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantProductDTO;
import com.jeeplus.test.wdt.merchantCompany.service.mapstuct.MerchantCompanyWrapper;
import com.jeeplus.test.wdt.merchantCompany.service.mapstuct.MerchantProductWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入驻产品Service
 * @author 徐滕
 * @version 2024-01-04 15:18
 */
@Service
@Transactional
public class MerchantProductService extends ServiceImpl <MerchantProductMapper, MerchantProduct> {

    /**
     * 自定义分页检索
     * @param page
     * @param merchantProductDTO
     * @return
     */
    public IPage<MerchantProductDTO> findPage(Page<MerchantProductDTO> page, MerchantProductDTO merchantProductDTO) throws Exception {
        QueryWrapper<MerchantProduct> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( MerchantProductWrapper.INSTANCE.toEntity(merchantProductDTO), MerchantProduct.class );
        queryWrapper.eq ("a.del_flag", 0 ); // 排除已经删除
        queryWrapper.orderByAsc("a.sort");
        if(StringUtils.isNotBlank(merchantProductDTO.getName())){
            queryWrapper.like("a.name",merchantProductDTO.getName());
        }
        if (merchantProductDTO.getDates() != null && merchantProductDTO.getDates().length > 0) {
            if(merchantProductDTO.getDates().length == 1){
                queryWrapper.ge("a.create_time",merchantProductDTO.getDates()[0]);
            }else {
                queryWrapper.between("a.create_time", merchantProductDTO.getDates()[0], merchantProductDTO.getDates()[1]);
            }
        }
        return  baseMapper.findList (page, queryWrapper);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public MerchantProductDTO findById(String id) {
        return baseMapper.findById ( id );
    }



    /**
     * 自定义分页检索
     * @param merchantCompanyDTO
     * @return
     */
    public List<MerchantProductDTO> getListJson(MerchantProductDTO merchantProductDTO) throws Exception {
        QueryWrapper<MerchantProduct> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( MerchantProductWrapper.INSTANCE.toEntity(merchantProductDTO), MerchantProduct.class );

        queryWrapper.eq ("a.del_flag", 0 ); // 排除已经删除
        queryWrapper.orderByDesc("a.name");
        return  baseMapper.getListJson (queryWrapper);
    }


}
