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

import java.util.List;

import javax.annotation.Resource;

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.common.utils.bean.BeanUtils;
import com.trade.service.enterprise.companyrelated.domain.BizEntityRelated;
import com.trade.service.enterprise.companyrelated.domain.BizEntityRelatedPerson;
import com.trade.service.enterprise.companyrelated.domain.vo.BizEntityRelatedSearch;
import com.trade.service.enterprise.companyrelated.domain.vo.BizEntityRelatedVO;
import com.trade.service.enterprise.companyrelated.mapper.BizEntityRelatedMapper;
import com.trade.service.enterprise.companyrelated.service.BizEntityRelatedPersonService;
import com.trade.service.enterprise.companyrelated.service.BizEntityRelatedService;

/**
 * <p>
 * 关系企业信息 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-05-28
 */
@Service
public class BizEntityRelatedServiceImp extends ServiceImpl<BizEntityRelatedMapper, BizEntityRelated> implements BizEntityRelatedService {

    @Resource
    private BizEntityRelatedPersonService bizEntityRelatedPersonService;

    @Override
    public PageInfo<BizEntityRelated> queryListBySearch(BizEntityRelatedSearch search) {
        // 使用mybatis-plus分页插件, 对于带有关联对象的分页查询，该插件使用不够灵活简洁，故使用PageHelper分页插件
        /*
         * Page<BizEntityRelatedSearch> page = search.toMpPageDefaultSortByCreateTimeDesc(); Page<BizEntityRelated>
         * result = getBaseMapper().queryListBySearch(page, search); return GridPage.of(result, BizEntityRelated.class);
         */
        // 使用PageHelper分页插件
        PageHelper.startPage(search.getPage(), search.getRows());
        List<BizEntityRelated> list = getBaseMapper().queryListBySearch(search);
        return new PageInfo<>(list);
    }

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

    @Override
    public BizEntityRelatedVO getByBizEntityId(Long bizEntityId) {
        BizEntityRelated bizEntityRelated = this.getById(bizEntityId);
        if (bizEntityRelated != null) {
            BizEntityRelatedVO bizEntityRelatedVO = new BizEntityRelatedVO();
            BeanUtils.copyProperties(bizEntityRelated, bizEntityRelatedVO);
            List<BizEntityRelatedPerson> relatedPersons = bizEntityRelatedPersonService
                    .list(new QueryWrapper<BizEntityRelatedPerson>().eq("entity_id", bizEntityId));
            bizEntityRelatedVO.setRelatedPersons(relatedPersons);
            return bizEntityRelatedVO;
        }
        return null;
    }
}
