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.WaybillDTO;
import org.springblade.modules.app.common.enums.WaybillStateEnum;
import org.springblade.modules.app.common.query.AdminWaybillQuery;
import org.springblade.modules.app.common.query.WaybillQuery;
import org.springblade.modules.app.common.vo.WaybillListVO;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.WaybillDO;
import org.springblade.modules.app.mapper.WaybillMapper;
import org.springblade.modules.app.service.IWaybillService;
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 WaybillServiceImpl extends ServiceImpl<WaybillMapper, WaybillDO> implements IWaybillService {

    @Autowired
    private WaybillMapper waybillMapper;

    @Override
    public List<WaybillListVO> listWaybill(WaybillQuery query) {
        QueryWrapper<WaybillDO> wrapper = new QueryWrapper<>();
        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.ne(query.getSalerId() != null || query.getAgentId() != null, DbField.STATE, WaybillStateEnum.WAYBILL_SLAUGHTER.getCode());
        wrapper.ne(query.getSalerId() != null || query.getAgentId() != null, DbField.STATE, WaybillStateEnum.WAYBILL_SLAUGHTER_REJECT.getCode());
        wrapper.eq(query.getSlaughterHouseId() != null, DbField.SLAUGHTER_HOUSE_ID, query.getSlaughterHouseId());
        wrapper.in(CollectionUtils.isNotEmpty(query.getStateList()), DbField.STATE,query.getStateList());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        List<WaybillDO> list = list(wrapper);
        return CollectionUtils.isNotEmpty(list) ? ModelConvertUtil.copyListTo(list, WaybillListVO.class) : null;
    }

    @Override
    public WaybillDTO getWaybill(WaybillQuery query) {
        QueryWrapper<WaybillDO> wrapper = new QueryWrapper<>();
        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(StringUtils.isNotBlank(query.getBillCode()), DbField.BILL_CODE, query.getBillCode());
        wrapper.eq(StringUtils.isNotBlank(query.getOutTradeNo()), DbField.OUT_TRADE_NO, query.getOutTradeNo());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        wrapper.last("limit 1");
        WaybillDO waybillDO = getOne(wrapper);
        return waybillDO != null ? waybillDO.copyTo(WaybillDTO.class) : null;
    }

    @Override
    public WaybillDTO getWaybillById(Long id) {
        WaybillDO waybillDO = getById(id);
        return waybillDO == null ? null : waybillDO.copyTo(WaybillDTO.class);
    }

    @Override
    public Long addWaybill(WaybillDTO waybillDTO) {
        WaybillDO waybillDO = waybillDTO.copyTo(WaybillDO.class);
        Validator.isTrue(save(waybillDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return waybillDO.getId();
    }

    @Override
    public Boolean updateWaybill(WaybillDTO waybillDTO) {
        Validator.notNull(waybillDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(waybillDTO.getId(), ErrorCode.INPUT_PARAMETERS_NULL);
        WaybillDO waybillDO = getById(waybillDTO.getId());
        if (waybillDO != null) {
            updateById(waybillDTO.copyTo(WaybillDO.class));
        }
        return true;
    }

    @Override
    public Page<WaybillDTO> pageWaybill(AdminWaybillQuery query) {
        Page<WaybillDO> page = query.covertToPage();
        QueryWrapper<WaybillDO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getAgentName()), DbField.AGENT_NAME, query.getAgentName());
        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.OUT_TRADE_NO, 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, WaybillDTO.class);
    }

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