package com.jwipc.supplier.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.supplier.convert.CustomerInfoConvert;
import com.jwipc.supplier.dto.CustomerInfoBaseDTO;
import com.jwipc.supplier.entity.CustomerInfo;
import com.jwipc.supplier.logic.CustomerLogic;
import com.jwipc.supplier.model.query.CustomerInfoQuery;
import com.jwipc.supplier.service.ICustomerInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class CustomerLogicImpl implements CustomerLogic {
    private final ICustomerInfoService customerInfoService;
    private final CustomerInfoConvert customerInfoConvert;

    @Override
    public PageView list(CustomerInfoQuery query) {
        if(StrUtil.isEmpty(query.getCode()) && StrUtil.isEmpty(query.getCode())){
            List<CustomerInfoBaseDTO> dtos = new ArrayList<>();
            return PageUtil.getPV(dtos, 0);
        }
        final LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<CustomerInfo>()
                .apply("RowNumber=1")
                .eq(CustomerInfo::getBloc, "800")//默认传入800
                .like(
                        StrUtil.isNotEmpty(query.getCode()), CustomerInfo::getCustomerCode, query.getCodeLike())
                .like(StrUtil.isNotEmpty(query.getName()), CustomerInfo::getCustomerName1, query.getNameLike());
        Integer count = customerInfoService.findCount(queryWrapper);
        final Integer rows = query.getRows();
        final Integer page = query.getPage();
        Integer offset = (page - 1) * rows;
        List<CustomerInfo> list = customerInfoService.findListForPage(offset, rows, queryWrapper);
        List<CustomerInfoBaseDTO> dtos = customerInfoConvert.entities2Dto(list);
        return PageUtil.getPV(dtos, count);
    }

    @Override
    public List<CustomerInfoBaseDTO> listAll(CustomerInfoQuery query) {
        final LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<CustomerInfo>()
                .eq(CustomerInfo::getBloc, "800")//默认传入800
                .like(
                        StrUtil.isNotEmpty(query.getCode()), CustomerInfo::getCustomerCode, query.getCodeLike())
                .like(StrUtil.isNotEmpty(query.getName()), CustomerInfo::getCustomerName1, query.getNameLike());
        List<CustomerInfo> list = customerInfoService.list( queryWrapper);
        List<CustomerInfoBaseDTO> dtos = customerInfoConvert.entities2Dto(list);
        return dtos;
    }
}