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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.TransportDTO;
import org.springblade.modules.app.common.enums.YesNoEnum;
import org.springblade.modules.app.common.query.AdminWaybillQuery;
import org.springblade.modules.app.common.query.TransportQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.TransportDO;
import org.springblade.modules.app.mapper.TransportMapper;
import org.springblade.modules.app.service.ITransportService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 运输单信息 服务实现类
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-05
 */
@Service
public class TransportServiceImpl extends ServiceImpl<TransportMapper, TransportDO> implements ITransportService {

    @Autowired
    private TransportMapper transportMapper;

    @Override
    public Long addTransport(TransportDTO transportDTO) {
        TransportDO transportDO = transportDTO.copyTo(TransportDO.class);
        Validator.isTrue(save(transportDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return transportDO.getId();
    }

    @Override
    public TransportDTO getTransportById(Long transportId) {
        TransportDO transportDO = getById(transportId);
        return transportDO == null ? null : transportDO.copyTo(TransportDTO.class);
    }

    @Override
    public List<TransportDTO> listTransport(TransportQuery query) {
        QueryWrapper<TransportDO> wrapper = new QueryWrapper<>();
        wrapper.eq(query.getWayBillId() != null, DbField.WAYBILL_ID, query.getWayBillId());
        wrapper.eq(query.getDriverId() != null, DbField.DRIVER_USER_ID, query.getDriverId());
        wrapper.eq(query.getAgentId() != null, DbField.AGENT_USER_ID, query.getAgentId());
        wrapper.eq(query.getBuyerId() != null, DbField.BUYER_USER_ID, query.getBuyerId());
        wrapper.eq(query.getSalerId() != null, DbField.SALER_USER_ID, query.getSalerId());
        wrapper.eq(query.getSlaughterHouseId() != null, DbField.SLAUGHTER_HOUSE_ID, query.getSlaughterHouseId());
        wrapper.eq(query.getState() != null, DbField.STATE, query.getState());
        if (query.getIsList() != null && query.getIsList().equals(YesNoEnum.YES.getCode())) {
            wrapper.and(queryWrapper->queryWrapper.in(CollectionUtils.isNotEmpty(query.getStateList()), DbField.STATE, query.getStateList()).or().eq(query.getAcceptState() != null, DbField.ACCEPT_STATE, query.getAcceptState()));
        } else {
            wrapper.eq(query.getAcceptState() != null, DbField.ACCEPT_STATE, query.getAcceptState());
            wrapper.in(CollectionUtils.isNotEmpty(query.getStateList()), DbField.STATE, query.getStateList());
        }
        wrapper.ne(query.getNeAcceptState() != null, DbField.ACCEPT_STATE, query.getNeAcceptState());
        wrapper.in(CollectionUtils.isNotEmpty(query.getWayBillIdList()), DbField.WAYBILL_ID, query.getWayBillIdList());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        List<TransportDO> list = list(wrapper);
        return CollectionUtils.isNotEmpty(list) ? ModelConvertUtil.copyListTo(list, TransportDTO.class) : null;
    }

    @Override
    public Boolean updateTransport(TransportDTO transport) {
        Validator.notNull(transport, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(transport.getId(), ErrorCode.INPUT_PARAMETERS_NULL);
        TransportDO transportDO = getById(transport.getId());
        if (transportDO != null) {
            updateById(transport.copyTo(TransportDO.class));
        }
        return true;
    }

    @Override
    public TransportDTO getTransport(TransportQuery query) {
        QueryWrapper<TransportDO> wrapper = new QueryWrapper<>();
        wrapper.eq(query.getWayBillId() != null, DbField.WAYBILL_ID, query.getWayBillId());
        wrapper.eq(query.getDriverId() != null, DbField.DRIVER_USER_ID, query.getDriverId());
        wrapper.eq(query.getDriverId() != null, DbField.WAYBILL_OUT_CODE, query.getWaybillOutCode());
        wrapper.in(CollectionUtils.isNotEmpty(query.getWayBillIdList()), DbField.WAYBILL_ID, query.getWayBillIdList());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        wrapper.last("limit 1");
        TransportDO transportDO = getOne(wrapper);
        return transportDO == null ? null : transportDO.copyTo(TransportDTO.class);
    }

    @Override
    public Page<TransportDTO> pageTransport(AdminWaybillQuery query) {
        Page<TransportDO> page = query.covertToPage();
        QueryWrapper<TransportDO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getDriverUserName()), DbField.DRIVER_USER_NAME, query.getDriverUserName());
        wrapper.like(StringUtils.isNotBlank(query.getBuyerName()), DbField.BUYER_USER_NAME, query.getBuyerName());
        wrapper.like(StringUtils.isNotBlank(query.getSalerName()), DbField.SALER_USER_NAME, query.getSalerName());
        wrapper.eq(StringUtils.isNotBlank(query.getOutTradeNo()), DbField.WAYBILL_OUT_CODE, query.getOutTradeNo());
        wrapper.eq(query.getState() != null, DbField.STATE, query.getState());
        wrapper.le(query.getEndTime() != null, DbField.CREATED_TIME, query.getEndTime());
        wrapper.ge(query.getStartTime() != null, DbField.CREATED_TIME, query.getStartTime());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        page = page(page, wrapper);
        return ModelConvertUtil.copyPageTo(page, TransportDTO.class);
    }

    @Override
    public Boolean updateBatch(List<TransportDTO> transportDTOList) {
        Validator.isTrue(CollectionUtils.isNotEmpty(transportDTOList), ErrorCode.INPUT_PARAMETERS_NULL);
        List<TransportDO> transportDOList = ModelConvertUtil.copyListTo(transportDTOList, TransportDO.class);
        updateBatchById(transportDOList);
        return true;
    }

    @Override
    public List<TransportDTO> queryAutoCloseData(){
        return transportMapper.queryAutoCloseData();
    }
}
