package com.sptg.device.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.FlowCardDto;
import com.sptg.device.api.entity.FlowCard;
import com.sptg.device.biz.mapper.FlowCardMapper;
import com.sptg.device.biz.service.FlowCardServiceInterface;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * title: DeviceServiceImpl
 * package: com.sptg.device.biz.security.impl
 * description: 流量卡业务层接口实现
 *
 *                  
 *                 
 */
@Service
@AllArgsConstructor
public class FlowCardServiceImpl extends ServiceImpl<FlowCardMapper, FlowCard> implements FlowCardServiceInterface {

    @Override
    public Page<FlowCard> pagingQuery(PageBean<FlowCard, FlowCardDto> pageBean) {
        FlowCardDto dto = pageBean.getDto();
        //  排序字段 时间降序
        pageBean.setOrderByField("t_create_date").setAsc(false);
        //  构建查询条件
        Wrapper<FlowCard> wrapper = new EntityWrapper<>();
        if (dto.getOrgName() != null){
            wrapper.like("s_org_name",dto.getOrgName(),SqlLike.DEFAULT);
        }
        if (dto.getOrgId() != null){
            wrapper.eq("l_org_id",dto.getOrgId());
        }
        if (dto.getCardNum() != null){
            wrapper.like("s_card_num",dto.getCardNum(),SqlLike.DEFAULT);
        }
        if (dto.getCardStatus() != null){
            wrapper.eq("i_card_status",dto.getCardStatus());
        }
        if (dto.getOperator() != null){
            wrapper.eq("i_operator",dto.getOperator());
        }
        if (dto.getIsFlowCardDevice()){
            wrapper.isNull("l_device_id");
            wrapper.eq("i_card_status",OperationConstant.ZERO);
        }
        wrapper.eq("i_status", OperationConstant.ZERO);
        wrapper.like("s_org_position",dto.getOrgPosition(),SqlLike.RIGHT);
        //排序
        if (dto.getSortField() != null && dto.getSortOrder() != null){
            wrapper.orderBy(dto.getSortField(),dto.getSortOrder().equals("asc"));
        }
        return this.selectPage(pageBean, wrapper);
    }

    @Override
    public FlowCard getOneFlowCard(FlowCardDto dto) {
        Wrapper<FlowCard> wrapper = new EntityWrapper<>();
        if (dto.getId() != null){
            wrapper.eq("l_id",dto.getId());
        }
        if (dto.getCardNum() != null){
            wrapper.eq("s_card_num",dto.getCardNum());
        }
        if (dto.getDeviceId() != null){
            wrapper.eq("l_device_id",dto.getDeviceId());
        }
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        return this.selectOne(wrapper);
    }
}
