package com.fjhx.victoriatourist.service.jd.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.item.util.excel.util.ExcelUtil;
import com.fjhx.victoriatourist.entity.jd.bo.JdBackQualityWaitExcelBo;
import com.fjhx.victoriatourist.entity.jd.dto.JdBackQualityWaitSelectDto;
import com.fjhx.victoriatourist.entity.jd.po.JdBackQualityWait;
import com.fjhx.victoriatourist.entity.jd.vo.JdBackQualityWaitVo;
import com.fjhx.victoriatourist.mapper.jd.JdBackQualityWaitMapper;
import com.fjhx.victoriatourist.service.jd.JdBackQualityDetailsService;
import com.fjhx.victoriatourist.service.jd.JdBackQualityWaitService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 * 京东退货待质检 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-19
 */
@Service
public class JdBackQualityWaitServiceImpl extends ServiceImpl<JdBackQualityWaitMapper, JdBackQualityWait> implements JdBackQualityWaitService {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private JdBackQualityDetailsService jdBackQualityDetailsService;

    @Override
    public Page<JdBackQualityWaitVo> getPage(JdBackQualityWaitSelectDto dto) {
        IWrapper<JdBackQualityWait> wrapper = getWrapper();
        wrapper.ne(JdBackQualityWait::getQuantity, 0);
        wrapper.orderByDesc("jbqw", JdBackQualityWait::getId);
        Page<JdBackQualityWaitVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        //赋值产品信息
        productInfoService.attributeAssign(page.getRecords(), JdBackQualityWaitVo::getProductId, (item, productinfo) -> {
            item.setProductType(productinfo.getType());
            item.setProductCode(productinfo.getCode());
            item.setProductName(productinfo.getName());
            item.setProductSpec(productinfo.getSpec());
            item.setProductUnit(productinfo.getUnit());
        });
        return page;
    }

    @Override
    public void exportExcel(HttpServletResponse httpServletResponse) {
        List<JdBackQualityWait> list = list();
        List<JdBackQualityWaitVo> jdBackQualityWaitVos = BeanUtil.copyToList(list, JdBackQualityWaitVo.class);
        //赋值产品信息
        productInfoService.attributeAssign(jdBackQualityWaitVos, JdBackQualityWaitVo::getProductId, (item, productinfo) -> {
            item.setProductType(productinfo.getType());
            item.setProductCode(productinfo.getCode());
            item.setProductName(productinfo.getName());
            item.setProductSpec(productinfo.getSpec());
            item.setProductUnit(productinfo.getUnit());
        });
        //导出Excel
        List<JdBackQualityWaitExcelBo> jdBackQualityWaitExcelBos = BeanUtil.copyToList(jdBackQualityWaitVos, JdBackQualityWaitExcelBo.class);
        ExcelUtil.export(httpServletResponse, jdBackQualityWaitExcelBos, JdBackQualityWaitExcelBo.class);
    }

    @Override
    public List<JdBackQualityWaitExcelBo> importExcel(MultipartFile file) {
        List<JdBackQualityWaitExcelBo> read = ExcelUtil.read(file, JdBackQualityWaitExcelBo.class);
        //重新赋值待质检数量
        List<JdBackQualityWaitVo> jdBackQualityWaitVos = BeanUtil.copyToList(read, JdBackQualityWaitVo.class);
        this.attributeAssign(jdBackQualityWaitVos, JdBackQualityWaitVo::getProductId, (item, qualityWait) -> {
            item.setQuantity(qualityWait.getQuantity());
        });
        return read;
    }

}
