package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.SlaughterBuyerRelationDTO;
import org.springblade.modules.app.common.query.RelationQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.SlaughterBuyerRelationDO;
import org.springblade.modules.app.mapper.SlaughterBuyerRelationMapper;
import org.springblade.modules.app.service.ISlaughterBuyerRelationService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代宰户与屠宰场绑定关系表 服务实现类
 * </p>
 *
 * @author zhangK
 * @since 2022-01-13
 */
@Service
public class SlaughterBuyerRelationServiceImpl extends ServiceImpl<SlaughterBuyerRelationMapper, SlaughterBuyerRelationDO> implements ISlaughterBuyerRelationService {


    @Autowired
    private SlaughterBuyerRelationMapper slaughterBuyerRelationMapper;

    @Override
    public List<SlaughterBuyerRelationDTO> queryList(RelationQuery query) {
        QueryWrapper<SlaughterBuyerRelationDO> wrapper = new QueryWrapper<>();
        wrapper.eq(query.getState() != null, DbField.STATE, query.getState());
        wrapper.eq(query.getBuyerUserId() != null, DbField.BUYER_USER_ID, query.getBuyerUserId());
        wrapper.eq(query.getSlaughterHouseId() != null, DbField.SLAUGHTER_HOUSE_ID, query.getSlaughterHouseId());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        List<SlaughterBuyerRelationDO> list = list(wrapper);
        return CollectionUtils.isNotEmpty(list) ? ModelConvertUtil.copyListTo(list, SlaughterBuyerRelationDTO.class) : null;
    }

    @Override
    public SlaughterBuyerRelationDTO queryById(Long id) {
        SlaughterBuyerRelationDO slaughterBuyerRelationDO = getById(id);
        return slaughterBuyerRelationDO == null ? null : slaughterBuyerRelationDO.copyTo(SlaughterBuyerRelationDTO.class);
    }

    @Override
    public Long insert(SlaughterBuyerRelationDTO dto) {
        SlaughterBuyerRelationDO slaughterBuyerRelationDO = dto.copyTo(SlaughterBuyerRelationDO.class);
        Validator.isTrue(save(slaughterBuyerRelationDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return slaughterBuyerRelationDO.getId();
    }


    @Override
    public Boolean deleteById(Long id) {
        int count = slaughterBuyerRelationMapper.realDeleteById(id);
        Validator.isTrue(count == 1, ErrorCode.DELETE_DATA_ERROR);
        return true;
    }

    @Override
    public SlaughterBuyerRelationDTO queryByHouseIdAndBuyerId(Long slaughterHouseId, Long buyerUserId) {
        QueryWrapper<SlaughterBuyerRelationDO> wrapper = new QueryWrapper<>();
        wrapper.eq(buyerUserId != null, DbField.BUYER_USER_ID, buyerUserId);
        wrapper.eq(slaughterHouseId != null, DbField.SLAUGHTER_HOUSE_ID, slaughterHouseId);
        SlaughterBuyerRelationDO slaughterBuyerRelationDO = getOne(wrapper);
        return slaughterBuyerRelationDO == null ? null : slaughterBuyerRelationDO.copyTo(SlaughterBuyerRelationDTO.class);
    }

    @Override
    public Boolean update(SlaughterBuyerRelationDTO dto){
        SlaughterBuyerRelationDO slaughterBuyerRelationDO = dto.copyTo(SlaughterBuyerRelationDO.class);
        return updateById(slaughterBuyerRelationDO);
    }
}
