package com.pj.project.tb_disinfect;

import java.io.File;
import java.io.InputStream;
import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.current.config.MyConfig;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessMapper;
import com.pj.project.tb_fee_details.ExportFeeDetailDTO;
import com.pj.project4sp.uploadfile.UploadUtil;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: tb_disinfect -- 消毒申请单
 * @author qzy 
 */
@Service
@Transactional
public class TbDisinfectService extends ServiceImpl<TbDisinfectMapper, TbDisinfect> implements IService<TbDisinfect>  {

	/** 底层 Mapper 对象 */
	@Autowired
	TbDisinfectMapper tbDisinfectMapper;
	@Resource
    MyConfig myConfig;

	/** 增 */
	void add(TbDisinfect t){
		this.save(t);
	}

	/** 删 */
	void delete(Long id){
		this.removeById(id);
	}

	/** 改 */
	void update(TbDisinfect t){
		this.updateById(t);
	}

	/** 查 */
	TbDisinfect getById(Long id){
		return super.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	List<TbDisinfect> getList(SoMap so) {
		return tbDisinfectMapper.getList(so);	
	}

	public String exportDisinfect(SoMap so) throws Exception{
        Date now = new Date();
        String nowStr = DateUtil.format(now, "yyyy-MM-dd HH:mm");
        String beginTime = so.getString("beginTime");
        String endTime = so.getString("endTime");
        so.put("beginTime", beginTime + " 00:00:00");
        so.put("endTime", endTime + " 23:59:59");
        String time = beginTime + "至" + endTime;
        Map<String, String> head = new HashMap<>();
        head.put("time", time);
        head.put("exportTime", nowStr);
        List<ExportDisnfectDTO>  exportList = new ArrayList<>();
        List<TbDisinfect> list = this.getList(so);
        Integer index = 0;
        for (TbDisinfect disinfect : list) {
            ExportDisnfectDTO dto = new ExportDisnfectDTO();
            BeanUtil.copyProperties(disinfect, dto);
            Double grossWeight = disinfect.getGrossWeight()*1000;
            dto.setIndex(++index).setGrossWeight(grossWeight.toString());
            exportList.add(dto);
        }
        String separator = File.separator;
        String today = DateUtil.today();
        String rootPath = UploadUtil.uploadConfig.rootFolder + separator + UploadUtil.uploadConfig.httpPrefix;
        String prefix = myConfig.getDomain() + UploadUtil.uploadConfig.httpPrefix;
        String extPath = "disinfect" + separator + today + separator;
        String fileName = "消毒申请记录_" + time + ".xlsx";
        String savePath = rootPath + separator + extPath;
        File saveFIle = new File(savePath);
        if (!saveFIle.exists()) {
            saveFIle.mkdirs();
        }
        ClassPathResource classPathResource = new ClassPathResource("static/disinfect.xlsx");
        InputStream tempInputStream =classPathResource.getInputStream();
        ExcelWriter excelWriter = EasyExcel.write(savePath + fileName, ExportFeeDetailDTO.class)
                .withTemplate(tempInputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();//换行
        excelWriter.fill(head, writeSheet);
        excelWriter.fill(exportList, fillConfig, writeSheet);
        excelWriter.finish();
        return prefix + "/disinfect/" + today + "/消毒申请记录_" + time + ".xlsx";

    }
	

}
