package org.springblade.modules.app.manager;

import org.springblade.modules.app.common.dto.SlaughterBuyerRelationLogDTO;
import org.springblade.modules.app.common.dto.UserDTO;
import org.springblade.modules.app.common.enums.UserTypeEnum;
import org.springblade.modules.app.common.query.RelationQuery;
import org.springblade.modules.app.service.ISlaughterBuyerRelationLogService;
import org.springblade.modules.app.service.IAppUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class SlaughterBuyerRelationLogManager {
    @Autowired
    private IAppUserService userService;
    @Autowired
    private ISlaughterBuyerRelationLogService slaughterBuyerRelationLogService;

    public List<SlaughterBuyerRelationLogDTO> list(UserDTO userDTO) {
        RelationQuery query = new RelationQuery();
        if(userDTO.getType() == UserTypeEnum.SLAUGHTER_HOUSE.getCode()){
            query.setSlaughterHouseId(userDTO.getId());
        }else if (userDTO.getType() == UserTypeEnum.SLAUGHTER.getCode()){
            query.setBuyerUserId(userDTO.getId());
        }
        List<SlaughterBuyerRelationLogDTO> list = slaughterBuyerRelationLogService.queryUnbindList(query);
        if(CollectionUtils.isNotEmpty(list)){
            for(SlaughterBuyerRelationLogDTO dto : list){
                UserDTO buyerUser = userService.getUserByIdAll(dto.getBuyerUserId());
                if(buyerUser!=null){
                    dto.setBuyerUserName(buyerUser.getUserName());
                }
                UserDTO houseUser = userService.getUserByIdAll(dto.getSlaughterHouseId());
                if(houseUser!=null){
                    dto.setSlaughterHouseName(houseUser.getUserName());
                }

            }
        }
        return list;
    }

}
