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

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

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.DevData;
import com.ruoyi.project.sems.domain.TSpecdevDt;
import com.ruoyi.project.sems.his.domain.TSpechiCc;
import org.springframework.stereotype.Service;
import com.ruoyi.project.sems.mapper.TSpecdevCcMapper;
import com.ruoyi.project.sems.domain.TSpecdevCc;
import com.ruoyi.project.sems.service.ITSpecdevCcService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 特种设备叉车台账Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-26
 */
@Service("tSpecdevCcService")
public class TSpecdevCcServiceImpl extends ServiceImpl<TSpecdevCcMapper, TSpecdevCc> implements ITSpecdevCcService
{
    @Resource
    private TSpecdevCcMapper tSpecdevCcMapper;
    @Resource
    private TCommonfileMapper tCommonfileMapper;
    /**
     * 查询特种设备叉车台账
     *
     * @param id 特种设备叉车台账ID
     * @return 特种设备叉车台账
     */
    @Override
    public TSpecdevCc selectTSpecdevCcById(Long id)
    {
        return tSpecdevCcMapper.selectTSpecdevCcById(id);
    }

    /**
     * 查询特种设备叉车台账列表
     *
     * @param tSpecdevCc 特种设备叉车台账
     * @return 特种设备叉车台账
     */
    @Override
    public List<TSpecdevCc> selectTSpecdevCcList(TSpecdevCc tSpecdevCc)
    {
        return tSpecdevCcMapper.selectTSpecdevCcList(tSpecdevCc);
    }

    @Override
    public List<TSpecdevCc> selectTSpecdevCcListByYear(TSpecdevCc tSpecdevCc) {
        return tSpecdevCcMapper.selectTSpecdevCcListByYear(tSpecdevCc);
    }

    /**
     * 新增特种设备叉车台账
     *
     * @param tSpecdevCc 特种设备叉车台账
     * @return 结果
     */
    @Override
    public int insertTSpecdevCc(TSpecdevCc tSpecdevCc)
    {
        return tSpecdevCcMapper.insertTSpecdevCc(tSpecdevCc);
    }

    /**
     * 修改特种设备叉车台账
     *
     * @param tSpecdevCc 特种设备叉车台账
     * @return 结果
     */
    @Override
    public int updateTSpecdevCc(TSpecdevCc tSpecdevCc)
    {
        return tSpecdevCcMapper.updateTSpecdevCc(tSpecdevCc);
    }

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

    /**
     * 删除特种设备叉车台账信息
     *
     * @param id 特种设备叉车台账ID
     * @return 结果
     */
    @Override
    public int deleteTSpecdevCcById(Long id)
    {
        return tSpecdevCcMapper.deleteTSpecdevCcById(id);
    }

    @Override
    public List<DevData> otherDevCount(Map<String, Object> params) {
        return tSpecdevCcMapper.otherDevCount(params);
    }

    @Override
    public int updateTSpecdevCcByHi(TSpechiCc hientity) {
        return tSpecdevCcMapper.updateTSpecdevCcByHi(hientity);
    }

    @Override
    public void duplicateTSpecdevCc() {
        tSpecdevCcMapper.duplicateTSpecdevCc();
    }

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