package com.sz.admin.system.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.system.service.ProductService;
import com.sz.admin.system.pojo.po.Product;
import com.sz.admin.system.mapper.ProductMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;

import java.io.Serializable;
import java.util.List;

import com.sz.admin.system.pojo.dto.ProductCreateDTO;
import com.sz.admin.system.pojo.dto.ProductUpdateDTO;
import com.sz.admin.system.pojo.dto.ProductListDTO;
import com.sz.admin.system.pojo.dto.ProductImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;

import java.io.OutputStream;

import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.core.datascope.SimpleDataScopeHelper;
import com.sz.admin.system.pojo.vo.ProductVO;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author Lz
 * @since 2026-01-22
 */
@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Override
    public void create(ProductCreateDTO dto) {
        Product sysProduct = BeanCopyUtils.copy(dto, Product.class);
        save(sysProduct);
    }

    @Override
    public void update(ProductUpdateDTO dto) {
        Product sysProduct = BeanCopyUtils.copy(dto, Product.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
                .eq(Product::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(sysProduct);
    }

    @Override
    public PageResult<ProductVO> page(ProductListDTO dto) {
        try {
            SimpleDataScopeHelper.start(Product.class);
            Page<ProductVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), ProductVO.class);
            return PageUtils.getPageResult(page);
        } finally {
            SimpleDataScopeHelper.clearDataScope();
        }
    }

    @Override
    public List<ProductVO> list(ProductListDTO dto) {
        try {
            SimpleDataScopeHelper.start(Product.class);
            return listAs(buildQueryWrapper(dto), ProductVO.class);
        } finally {
            SimpleDataScopeHelper.clearDataScope();
        }
    }

    @Override
    public void remove(SelectIdsDTO dto) {
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public ProductVO detail(Object id) {
        Product sysProduct = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(sysProduct);
        return BeanCopyUtils.copy(sysProduct, ProductVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<ProductImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), ProductImportDTO.class, true);
        List<ProductImportDTO> list = excelResult.getList();
        // 入库
        List<Product> importList = BeanCopyUtils.copyList(list, Product.class);
        saveBatch(importList);
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(ProductListDTO dto, HttpServletResponse response) {
        List<ProductVO> list = list(dto);
        String fileName = "商品信息表模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "商品信息表", ProductVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(ProductListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(Product.class);
        if (Utils.isNotNull(dto.getProductName())) {
            wrapper.like(Product::getProductName, dto.getProductName());
        }
        if (Utils.isNotNull(dto.getPrice())) {
            wrapper.eq(Product::getPrice, dto.getPrice());
        }
        if (Utils.isNotNull(dto.getStatus())) {
            wrapper.eq(Product::getStatus, dto.getStatus());
        }
        return wrapper;
    }
}