package com.ruoyi.hywz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxZhptInvoiceD;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxZhptInvoiceItemD;
import com.ruoyi.hywz.entity.po.IdsPO;
import com.ruoyi.hywz.listener.ImportExcelHelper;
import com.ruoyi.hywz.mapper.AdsMatHyxtStampTaxZhptInvoiceItemDMapper;
import com.ruoyi.hywz.service.IAdsMatHyxtStampTaxZhptInvoiceDService;
import com.ruoyi.hywz.service.IAdsMatHyxtStampTaxZhptInvoiceItemDService;
import com.ruoyi.hywz.utils.IdGen;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 【印花税】【智慧平台】科目明细表 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-08-23 11:15:50
 */
@Service
public class AdsMatHyxtStampTaxZhptInvoiceItemDServiceImpl extends ServiceImpl<AdsMatHyxtStampTaxZhptInvoiceItemDMapper, AdsMatHyxtStampTaxZhptInvoiceItemD> implements IAdsMatHyxtStampTaxZhptInvoiceItemDService {

    @Resource
    private IAdsMatHyxtStampTaxZhptInvoiceDService adsMatHyxtStampTaxZhptInvoiceDService;

    @Override
    public Page<AdsMatHyxtStampTaxZhptInvoiceItemD> queryPage(Long pageSize, Long pageNo, AdsMatHyxtStampTaxZhptInvoiceItemD param) {
        Page<AdsMatHyxtStampTaxZhptInvoiceItemD> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        LambdaQueryWrapper<AdsMatHyxtStampTaxZhptInvoiceItemD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxZhptInvoiceItemD::getInvoiceId, param.getInvoiceId());

        queryWrapper.orderByDesc(AdsMatHyxtStampTaxZhptInvoiceItemD::getCreateTime);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<AdsMatHyxtStampTaxZhptInvoiceItemD> queryList(AdsMatHyxtStampTaxZhptInvoiceItemD param) {
        LambdaQueryWrapper<AdsMatHyxtStampTaxZhptInvoiceItemD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxZhptInvoiceItemD::getInvoiceId, param.getInvoiceId());
        queryWrapper.orderByDesc(AdsMatHyxtStampTaxZhptInvoiceItemD::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<AdsMatHyxtStampTaxZhptInvoiceItemD> queryList(String invoiceNumber) {
        LambdaQueryWrapper<AdsMatHyxtStampTaxZhptInvoiceItemD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxZhptInvoiceItemD::getInvoiceNumber, invoiceNumber);
        return this.list(queryWrapper);
    }

    @Override
    public void downloadTemplate(HttpServletResponse response) {
        String filePath = "excelTemplate/stampTax/stampTaxZhptInvoiceItemImport.xlsx";
        byte[] buffer = new byte[1024];
        InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
        BufferedInputStream bis = null;
        try {
            response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(UUID.fastUUID().toString(), "UTF-8"));
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                fis.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    @Override
    public void batchDelete(IdsPO param) {
        LambdaQueryWrapper<AdsMatHyxtStampTaxZhptInvoiceItemD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(AdsMatHyxtStampTaxZhptInvoiceItemD::getId, param.getIds());
        baseMapper.delete(queryWrapper);
    }

    @Override
    public R importData(MultipartFile file, String invoiceId) throws IOException {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ImportExcelHelper<AdsMatHyxtStampTaxZhptInvoiceItemD> helper = new ImportExcelHelper<>();
        List<AdsMatHyxtStampTaxZhptInvoiceItemD> list = helper.getList(file.getInputStream(), AdsMatHyxtStampTaxZhptInvoiceItemD.class, 0, 1);
        if (list.size() == 0) {
            return R.fail("导入失败，未解析到正确数据!");
        }
        Date date = new Date();
        AdsMatHyxtStampTaxZhptInvoiceD byId = adsMatHyxtStampTaxZhptInvoiceDService.getById(invoiceId);
        for (AdsMatHyxtStampTaxZhptInvoiceItemD invoiceD : list) {
            invoiceD.setId(IdGen.get32UUID());
            if (ObjectUtils.isNotEmpty(byId)) {
                invoiceD.setInvoiceCode(byId.getInvoiceCode());
                invoiceD.setInvoiceNumber(byId.getInvoiceNumber());
            }
            invoiceD.setInvoiceId(invoiceId);
            invoiceD.setIsDeleted(0);
            invoiceD.setCreateUser(loginUser.getUsername());
            invoiceD.setCreateUserId(String.valueOf(loginUser.getUserId()));
            invoiceD.setCreateUserName(loginUser.getUsername());
            invoiceD.setCreateTime(date);
            invoiceD.setCreateUserOrgNo(loginUser.getOrgNo());
            invoiceD.setCreateUserOrgName(loginUser.getOrgName());
        }
        this.saveBatch(list);
        return R.ok();
    }
}
