package com.xunhong.erp.turbo.biz.infrastructure.gateway;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxSpecCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxSpecDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxSpecUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxSpecListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxSpecPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxSpecShowQry;
import com.xunhong.erp.turbo.biz.domain.entity.BoxSpec;
import com.xunhong.erp.turbo.biz.domain.gateway.BoxSpecGateway;
import com.xunhong.erp.turbo.biz.infrastructure.convert.BoxSpecConvert;
import com.xunhong.erp.turbo.biz.infrastructure.entity.BoxSpecDO;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.BoxSpecMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author shenyifei
 */
@Repository
@RequiredArgsConstructor
public class BoxSpecGatewayImpl implements BoxSpecGateway {
    private final BoxSpecMapper boxSpecMapper;
    private final BoxSpecConvert boxSpecConvert;

    @Override
    public BoxSpec save(BoxSpecCreateCmd boxSpecCreateCmd) {
        BoxSpecDO boxSpecDO = boxSpecConvert.toBoxSpecDO(boxSpecCreateCmd);
        boxSpecMapper.insert(boxSpecDO);

        return boxSpecConvert.toBoxSpec(boxSpecDO);
    }

    @Override
    public IPage<BoxSpec> page(BoxSpecPageQry boxSpecPageQry) {
        LambdaQueryWrapper<BoxSpecDO> queryWrapper = Wrappers.lambdaQuery(BoxSpecDO.class);
		queryWrapper.orderByDesc(BoxSpecDO::getCreatedAt);

        IPage<BoxSpecDO> page = new Page<>(boxSpecPageQry.getPageIndex(), boxSpecPageQry.getPageSize());
        page = boxSpecMapper.selectPage(page, queryWrapper);

        return page.convert(boxSpecConvert::toBoxSpec);
    }

    @Override
    public List<BoxSpec> list(BoxSpecListQry boxSpecListQry) {
        LambdaQueryWrapper<BoxSpecDO> queryWrapper = Wrappers.lambdaQuery(BoxSpecDO.class);
		List<BoxSpecDO> boxSpecDOList = boxSpecMapper.selectList(queryWrapper);
        return boxSpecDOList.stream().map(boxSpecConvert::toBoxSpec).toList();
    }

    @Override
    public BoxSpec update(BoxSpecUpdateCmd boxSpecUpdateCmd) {
		LambdaQueryWrapper<BoxSpecDO> queryWrapper = Wrappers.lambdaQuery(BoxSpecDO.class);
		queryWrapper.eq(BoxSpecDO::getSpecId, boxSpecUpdateCmd.getSpecId());
		queryWrapper.last("limit 1");

		BoxSpecDO boxSpecDO = boxSpecMapper.selectOne(queryWrapper);

		boxSpecConvert.toBoxSpecDO(boxSpecDO, boxSpecUpdateCmd);
		boxSpecMapper.updateById(boxSpecDO);

        return boxSpecConvert.toBoxSpec(boxSpecDO);
    }

    @Override
    public BoxSpec show(BoxSpecShowQry boxSpecShowQry) {
        LambdaQueryWrapper<BoxSpecDO> queryWrapper = Wrappers.lambdaQuery(BoxSpecDO.class);
        queryWrapper.eq(BoxSpecDO::getSpecId, boxSpecShowQry.getSpecId());
        queryWrapper.last("limit 1");

        BoxSpecDO boxSpecDO = boxSpecMapper.selectOne(queryWrapper);
        return boxSpecConvert.toBoxSpec(boxSpecDO);
    }

    @Override
    public void destroy(BoxSpecDestroyCmd boxSpecDestroyCmd) {
        LambdaQueryWrapper<BoxSpecDO> queryWrapper = Wrappers.lambdaQuery(BoxSpecDO.class);
        queryWrapper.eq(BoxSpecDO::getSpecId, boxSpecDestroyCmd.getSpecId());
        queryWrapper.last("limit 1");

        BoxSpecDO boxSpecDO = boxSpecMapper.selectOne(queryWrapper);
        boxSpecDO.deleteById();
    }
}

