package com.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.dto.ClientDTO;
import com.om.entity.dto.ClientQueryPageDTO;
import com.om.entity.po.Client;
import com.om.entity.po.Distributor;
import com.om.entity.vo.ClientQueryPageVO;
import com.om.entity.vo.ClientVO;
import com.om.entity.vo.DistributorQueryPageVO;
import com.om.entity.vo.DistributorVO;
import com.om.exception.BadReqException;
import com.om.mapper.ClientMapper;
import com.om.service.IClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bmmx
 * @since 2024-02-15
 */
@Service
@Slf4j
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements IClientService {

    @Override
    public Result addClient(ClientDTO dto) {
        if (BeanUtil.isEmpty(dto)){
            throw new BadReqException("数据异常");
        }
        // 判断是否已有该客户端编号的
        Client one = this.lambdaQuery()
                .eq(Client::getNumber, dto.getNumber())
                .one();
        if (BeanUtil.isNotEmpty(one)){
            //存在
            throw new BadReqException("该客户端已存在");
        }

        Client client = BeanUtil.copyProperties(dto, Client.class);
        client.setCreateTime(LocalDateTime.now());
        client.setUpdateTime(LocalDateTime.now());
        this.save(client);

        return Result.ok();
    }

    @Override
    public Result deleteClient(Integer id) {
        //根据id查询
        Client client = this.getById(id);
        if (BeanUtil.isEmpty(client)){
            throw new BadReqException("该客户端不存在");
        }

        //删除
        this.removeById(id);

        return Result.ok();
    }

    @Override
    public Result<ClientVO> getClient(Integer id) {
        //根据id查询
        Client client = this.getById(id);
        if (BeanUtil.isEmpty(client)){
            throw new BadReqException("该客户端不存在");
        }
        ClientVO clientVO = BeanUtil.copyProperties(client, ClientVO.class);
        return Result.ok(clientVO);
    }

    @Override
    public Result<List<ClientVO>> getList() {

        List<Client> list = this.list();

        if (CollectionUtil.isEmpty(list)){
            return Result.ok(Collections.emptyList());
        }

        List<ClientVO> clientVOS = BeanUtil.copyToList(list, ClientVO.class);
        return Result.ok(clientVOS);
    }

    @Override
    public Result editClient(ClientDTO dto) {
        //查询该客户端是否存在
        Client client = this.getById(dto.getId());
        if (BeanUtil.isEmpty(client)){
            throw new BadReqException("该客户端不存在");
        }

        BeanUtil.copyProperties(dto,client);
        client.setUpdateTime(LocalDateTime.now());
        this.updateById(client);
        return Result.ok();
    }

    @Override
    public Result<ClientQueryPageVO> queryPageClient(ClientQueryPageDTO dto) {
        Integer pageSize = dto.getPageSize();
        Integer pageIndex = dto.getPageIndex();
        String search = dto.getSearch();
        //分页查询
        Page<Client> page = this.lambdaQuery()
                .like(search != null, Client::getName, search)
                .orderByDesc(Client::getCreateTime)
                .page(new Page<>(pageIndex, pageSize));

        //封装vo
        ClientQueryPageVO vo = new ClientQueryPageVO();
        vo.setCurrent((int) page.getCurrent());
        vo.setSize((int) page.getSize());
        vo.setPages((int) page.getPages());
        vo.setTotal((int) page.getTotal());
        if (search !=null){
            vo.setSearchCount(true);
        }
        List<Client> records = page.getRecords();

        List<ClientVO> clientVOS = BeanUtil.copyToList(records, ClientVO.class);

        vo.setRecords(clientVOS);

        return Result.ok(vo);
    }
}
