package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.domain.MaPartType;
import com.bonus.sgzb.base.mapper.MaPartTypeMapper;
import com.bonus.sgzb.base.service.IPartTypeService;
import com.bonus.sgzb.common.core.constant.UserConstants;
import com.bonus.sgzb.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 配件类型管理ma_part_type(MaPartType)表服务实现类
 *
 * @author makejava
 * @since 2023-11-27 16:44:20
 */
@Slf4j
@Service("maPartTypeService")
public class MaPartTypeServiceImpl implements IPartTypeService {

    @Autowired
    private MaPartTypeMapper maPartTypeMapper;

    /**
     * 校验配件名称唯一性
     *
     * @param maPartType
     * @return
     */
    @Override
    public boolean checkPaNameUnique(MaPartType maPartType) {
        Long paId = StringUtils.isNull(maPartType.getPaId()) ? -1L : maPartType.getPaId();
        MaPartType info = maPartTypeMapper.checkPartNameUnique(maPartType.getPaId());
        if (StringUtils.isNotNull(info) && info.getPaId().longValue() != paId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增配件管理
     *
     * @param maPartType
     * @return
     */
    @Override
    public int insertMaPart(MaPartType maPartType) {

        return maPartTypeMapper.insertMaPartType(maPartType);
    }

    /**
     * 查询配件类型列表
     *
     * @param maPartType
     * @return
     */
    @Override
    public List<MaPartType> selectMaPartList(MaPartType maPartType) {
        return maPartTypeMapper.selectMaPartType(maPartType);
    }

    /**
     * 导出配件管理类型
     *
     * @param maPartType
     * @return
     */
    @Override
    public List<MaPartType> selectMyPartTypeList(MaPartType maPartType) {
        return maPartTypeMapper.selectMaPartType(maPartType);
    }

    /**
     * 查询是否含有子集
     *
     * @param paId
     * @return
     */
    @Override
    public boolean hasChildBypaId(Long paId) {
        int result = maPartTypeMapper.hasChildByPaId(paId);
        return result > 0;
    }

    /**
     * 删除配件类型
     *
     * @param paId
     * @return
     */
    @Override
    public int deletePaById(Long paId) {
        return maPartTypeMapper.deletePaById(paId);
    }

    @Override
    public MaPartType getById(Long paId) {
        return maPartTypeMapper.getById(paId);
    }

    @Override
    public int updateById(MaPartType maPartType) {
        return maPartTypeMapper.updateById(maPartType);
    }

    @Override
    public void downLoadTemplate(HttpServletResponse response) throws IOException {
        //模板名称

        String templateName = "template.xlsx";
        OutputStream out = null;
        InputStream input =null;
        try {
//            ApplicationHome h = new ApplicationHome(getClass());
//            String dirPath = h.getSource().toString();
//            String fileName = dirPath+"/template/"+templateName;
//            File outFile = new File(fileName);
//            input = new BufferedInputStream(new FileInputStream(outFile));
            input = this.getClass().getClassLoader().getResourceAsStream("template/template.xlsx");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String((templateName).getBytes(), "iso-8859-1"));
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            out = response.getOutputStream();
            byte[] buffer = new byte[1024]; // 缓冲区
            int bytesToRead = -1;
            // 通过循环将读入内容输出到浏览器中
            while ((bytesToRead = input.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
//            throw new IOException("模板下载失败");
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(out);
        }
    }


}
