package com.wms_main.service.controller.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms_main.dao.ITAppImageService;
import com.wms_main.excel.easypoi.service.IExportExcelEasyPoi;
import com.wms_main.model.dto.query.DbsQuery;
import com.wms_main.model.dto.request.wms.InitKateWorkRequest;
import com.wms_main.model.po.TAppImage;
import com.wms_main.model.vo.wms.ImageVo;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.service.controller.ITestControllerService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.IOException;
import java.util.List;

/**
 * TestController的实现
 */
@Service
@RequiredArgsConstructor
public class TestControllerServiceImpl implements ITestControllerService {
    private final ITAppImageService tAppImageService;
    private final IExportExcelEasyPoi exportExcelEasyPoi;

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public WmsApiResponse<List<ImageVo>> testRequestImage(InitKateWorkRequest request) {
        WmsApiResponse<List<ImageVo>> response = new WmsApiResponse<>();
        try {
            response.setCode(0);
            response.setMessage("success");
            List<TAppImage> tAppImageList = tAppImageService.list(new LambdaQueryWrapper<TAppImage>()
                    .eq(StringUtils.isNotEmpty(request.getProductId()), TAppImage::getProductId, request.getProductId())
                    .eq(StringUtils.isNotEmpty(request.getBoxNo()), TAppImage::getBoxNo, request.getBoxNo()));
            List<ImageVo> imageVoList = BeanUtil.copyToList(tAppImageList, ImageVo.class);
            response.setData(imageVoList);
        } catch (Exception e) {
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            response.setCode(99);
            response.setMessage("error");
        }
        return response;
    }

    @Override
    public void testExport(DbsQuery dbsQuery, HttpServletResponse response) throws IOException {
        exportExcelEasyPoi.doExportDbs(dbsQuery, response);
    }
}
