package com.jwipc.sys.client.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.sys.client.PurchaseGroupClient;
import com.jwipc.sys.client.dto.PurchaseGroupDTO;
import com.jwipc.sys.client.convert.PurchaseGroupClientConvert;
import com.jwipc.sys.convert.PurchaseGroupConvert;
import com.jwipc.sys.entity.PurchaseGroup;
import com.jwipc.sys.model.po.PurchaseGroupPO;
import com.jwipc.sys.model.query.PurchaseGroupQuery;
import com.jwipc.sys.service.IPurchaseGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class PurchaseGroupClientImpl implements PurchaseGroupClient {
    private final IPurchaseGroupService purchaseGroupService;
    private final PurchaseGroupClientConvert purchaseGroupClientConvert;
    private final PurchaseGroupConvert purchaseGroupConvert;

    public PurchaseGroupDTO getByCode(String code) {
        final PurchaseGroup purchaseGroup = purchaseGroupService.getOne(
                new LambdaQueryWrapper<PurchaseGroup>().eq(PurchaseGroup::getPurchaseGroupCode, code), false);
        return purchaseGroupClientConvert.entity2Dto(purchaseGroup);
    }
    public PurchaseGroupDTO getByName(String name) {
        final PurchaseGroup purchaseGroup = purchaseGroupService.getOne(
                new LambdaQueryWrapper<PurchaseGroup>().eq(PurchaseGroup::getPurchaseGroupName, name), false);
        return purchaseGroupClientConvert.entity2Dto(purchaseGroup);
    }

    public List<PurchaseGroupDTO> getAll(){
        final List<PurchaseGroup> purchaseGroup = purchaseGroupService.list();
        return purchaseGroupClientConvert.entities2Dtos(purchaseGroup);
    }
    @Override
    public List<PurchaseGroupDTO> listAll(Long userId) {
        List<PurchaseGroupDTO> purchaseGroupList = new ArrayList<>();
        PurchaseGroupQuery query = new PurchaseGroupQuery();
        query.setUserId(userId);
        List<PurchaseGroupPO> purchaseGroupPOS =  purchaseGroupService.findPageByQuery(null,query);
        for (PurchaseGroupPO purchaseGroupPO : purchaseGroupPOS) {
            PurchaseGroupDTO dto = new PurchaseGroupDTO();
            dto.setPurchaseGroupCode(purchaseGroupPO.getPurchaseGroupCode());
            dto.setPurchaseGroupName(purchaseGroupPO.getPurchaseGroupCode());
            purchaseGroupList.add(dto);
        }
        return purchaseGroupList;
    }
}
