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.his.domain.TSpechiYlrq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.ruoyi.project.sems.mapper.TSpecdevYlrqMapper;
import com.ruoyi.project.sems.domain.TSpecdevYlrq;
import com.ruoyi.project.sems.service.ITSpecdevYlrqService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 特种设备压力容器台账Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-30
 */
@Service("tSpecdevYlrqService")
public class TSpecdevYlrqServiceImpl extends ServiceImpl<TSpecdevYlrqMapper, TSpecdevYlrq> implements ITSpecdevYlrqService
{
    @Resource
    private TSpecdevYlrqMapper tSpecdevYlrqMapper;
    @Resource
    private TCommonfileMapper tCommonfileMapper;

    protected final Logger logger = LoggerFactory.getLogger(TSpecdevYlrqServiceImpl.class);
    /**
     * 查询特种设备压力容器台账
     *
     * @param id 特种设备压力容器台账ID
     * @return 特种设备压力容器台账
     */
    @Override
    public TSpecdevYlrq selectTSpecdevYlrqById(Long id)
    {
        return tSpecdevYlrqMapper.selectTSpecdevYlrqById(id);
    }

    /**
     * 查询特种设备压力容器台账列表
     *
     * @param tSpecdevYlrq 特种设备压力容器台账
     * @return 特种设备压力容器台账
     */
    @Override
    public List<TSpecdevYlrq> selectTSpecdevYlrqList(TSpecdevYlrq tSpecdevYlrq)
    {
        return tSpecdevYlrqMapper.selectTSpecdevYlrqList(tSpecdevYlrq);
    }

    @Override
    public List<TSpecdevYlrq> selectTSpecdevYlrqListByYear(TSpecdevYlrq tSpecdevYlrq) {
        return tSpecdevYlrqMapper.selectTSpecdevYlrqListByYear(tSpecdevYlrq);
    }

    /**
     * 新增特种设备压力容器台账
     *
     * @param tSpecdevYlrq 特种设备压力容器台账
     * @return 结果
     */
    @Override
    public int insertTSpecdevYlrq(TSpecdevYlrq tSpecdevYlrq)
    {
        return tSpecdevYlrqMapper.insertTSpecdevYlrq(tSpecdevYlrq);
    }

    /**
     * 修改特种设备压力容器台账
     *
     * @param tSpecdevYlrq 特种设备压力容器台账
     * @return 结果
     */
    @Override
    public int updateTSpecdevYlrq(TSpecdevYlrq tSpecdevYlrq)
    {
        return tSpecdevYlrqMapper.updateTSpecdevYlrq(tSpecdevYlrq);
    }

    /**
     * 批量删除特种设备压力容器台账
     *
     * @param ids 需要删除的特种设备压力容器台账ID
     * @return 结果
     */
    @Override
    public int deleteTSpecdevYlrqByIds(Long[] ids)
    {
        return tSpecdevYlrqMapper.deleteTSpecdevYlrqByIds(ids);
    }

    /**
     * 删除特种设备压力容器台账信息
     *
     * @param id 特种设备压力容器台账ID
     * @return 结果
     */
    @Override
    public int deleteTSpecdevYlrqById(Long id)
    {
        return tSpecdevYlrqMapper.deleteTSpecdevYlrqById(id);
    }

    @Override
    public int updateTSpecdevYlrqByHi(TSpechiYlrq hientity) {
        return tSpecdevYlrqMapper.updateTSpecdevYlrqByHi(hientity);
    }

    @Override
    public void duplicateTSpecdevYlrq() {
        tSpecdevYlrqMapper.duplicateTSpecdevYlrq();
    }

    @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); // 输出: 位号
                TSpecdevYlrq param = new TSpecdevYlrq();
                param.setDevno(weiHao);
                param.setPlantCode(plantCode);
                List<TSpecdevYlrq> list = tSpecdevYlrqMapper.selectTSpecdevYlrqListByDevNo(param);
                if (list.size() > 0) {
                    for (TSpecdevYlrq t: list
                         ) {
                        Long id = t.getId();
                        // 获取当前日期
                        LocalDate currentDate = LocalDate.now();
                        // 获取年份
                        int year = currentDate.getYear();
                        String dir = "/sems-ylrq-" + 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-ylrq-"+fileType);
                        tCommonfile.setFileSize(String.valueOf(size));
                        tCommonfileMapper.insertTCommonfile(tCommonfile);
                    }
                }else {
                    throw new Exception( StringUtils.format(input +":无法找到对应设备"));
                }
            }
        }
        return 1;
    }
}
