package com.ruoyi.project.sems.service.impl;

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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.common.domain.TCommonfile;
import com.ruoyi.project.common.mapper.TCommonfileMapper;
import com.ruoyi.project.sems.domain.TSpecdevYlgd;
import com.ruoyi.project.sems.his.domain.TSpechiGl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.sems.mapper.TSpecdevGlMapper;
import com.ruoyi.project.sems.domain.TSpecdevGl;
import com.ruoyi.project.sems.service.ITSpecdevGlService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 特种设备锅炉台账Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-21
 */
@Service("tSpecdevGlService")
public class TSpecdevGlServiceImpl extends ServiceImpl<TSpecdevGlMapper, TSpecdevGl> implements ITSpecdevGlService
{
    @Resource
    private TSpecdevGlMapper tSpecdevGlMapper;
    @Resource
    private TCommonfileMapper tCommonfileMapper;
    /**
     * 查询特种设备锅炉台账
     *
     * @param id 特种设备锅炉台账ID
     * @return 特种设备锅炉台账
     */
    @Override
    public TSpecdevGl selectTSpecdevGlById(Long id)
    {
        return tSpecdevGlMapper.selectTSpecdevGlById(id);
    }

    /**
     * 查询特种设备锅炉台账列表
     *
     * @param tSpecdevGl 特种设备锅炉台账
     * @return 特种设备锅炉台账
     */
    @Override
    public List<TSpecdevGl> selectTSpecdevGlList(TSpecdevGl tSpecdevGl)
    {
        return tSpecdevGlMapper.selectTSpecdevGlList(tSpecdevGl);
    }

    @Override
    public List<TSpecdevGl> selectTSpecdevGlListByYear(TSpecdevGl tSpecdevGl) {
        return  tSpecdevGlMapper.selectTSpecdevGlListByYear(tSpecdevGl);
    }

    /**
     * 新增特种设备锅炉台账
     *
     * @param tSpecdevGl 特种设备锅炉台账
     * @return 结果
     */
    @Override
    public int insertTSpecdevGl(TSpecdevGl tSpecdevGl)
    {
        return tSpecdevGlMapper.insertTSpecdevGl(tSpecdevGl);
    }

    /**
     * 修改特种设备锅炉台账
     *
     * @param tSpecdevGl 特种设备锅炉台账
     * @return 结果
     */
    @Override
    public int updateTSpecdevGl(TSpecdevGl tSpecdevGl)
    {
        return tSpecdevGlMapper.updateTSpecdevGl(tSpecdevGl);
    }

    /**
     * 批量删除特种设备锅炉台账
     *
     * @param ids 需要删除的特种设备锅炉台账ID
     * @return 结果
     */
    @Override
    public int deleteTSpecdevGlByIds(Long[] ids)
    {
        return tSpecdevGlMapper.deleteTSpecdevGlByIds(ids);
    }

    /**
     * 删除特种设备锅炉台账信息
     *
     * @param id 特种设备锅炉台账ID
     * @return 结果
     */
    @Override
    public int deleteTSpecdevGlById(Long id)
    {
        return tSpecdevGlMapper.deleteTSpecdevGlById(id);
    }

    @Override
    public int updateTSpecdevGlByHi(TSpechiGl hientity) {
        return tSpecdevGlMapper.updateTSpecdevGlByHi(hientity);
    }

    @Override
    public void duplicateTSpecdevGl() {
        tSpecdevGlMapper.duplicateTSpecdevGl();
    }

    @Override
    @Transactional
    public int importFiles(List<MultipartFile> files, String plantCode, String fileType , String userId) throws Exception {
        for (MultipartFile file : files) {
            if (!file.isEmpty()) {
                String input = file.getOriginalFilename();
                int index = input.indexOf('_');
                String weiHao = (index != -1) ? input.substring(0, index) : input;
                System.out.println(weiHao); // 输出: 位号
                TSpecdevGl param = new TSpecdevGl();
                param.setDevno(weiHao);
                param.setPlantCode(plantCode);
                List<TSpecdevGl> list = tSpecdevGlMapper.selectTSpecdevGlListByDevNo(param);
                if (list.size() > 0) {
                    for (TSpecdevGl t: list
                    ) {
                        Long id = t.getId();
                        // 获取当前日期
                        LocalDate currentDate = LocalDate.now();
                        // 获取年份
                        int year = currentDate.getYear();
                        String dir = "/sems-gl-" + fileType + "/" + year;
                        String url = null;
                        try {
                            url = FileUploadUtils.upload(RuoYiConfig.getFilePath(dir), file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        long size = file.getSize()/1024;
                        TCommonfile tCommonfile = new TCommonfile();
                        tCommonfile.setFileUrl(url);
                        tCommonfile.setFileName(file.getOriginalFilename());
                        tCommonfile.setCreaterCode(userId);
                        tCommonfile.setpId(id);
                        tCommonfile.setpType("sems-gl-"+fileType);
                        tCommonfile.setFileSize(String.valueOf(size));
                        tCommonfileMapper.insertTCommonfile(tCommonfile);
                    }
                }else {
                    throw new Exception( StringUtils.format(input +":无法找到对应设备"));
                }
            }
        }
        return 1;
    }
}
