package com.steerinfo.ems.emsprodplanyear.service.impl;

import com.steerinfo.ems.Utils.ExcelToolUtils;
import com.steerinfo.ems.Utils.MarketSlmException;
import com.steerinfo.ems.emsprodplanyear.mapper.EmsProdplanYearMapper;
import com.steerinfo.ems.emsprodplanyear.model.EmsProdplanYear;
import com.steerinfo.ems.emsprodplanyear.service.IEmsProdplanYearService;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * EmsProdplanYear服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-06-26 08:40
 * 类描述
 * 修订历史：
 * 日期：2021-06-26
 * 作者：generator
 * 参考：
 * 描述：EmsProdplanYear服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "emsProdplanYearService")
public class EmsProdplanYearServiceImpl extends BaseServiceImpl<EmsProdplanYear, String> implements IEmsProdplanYearService {

    @Autowired
    private EmsProdplanYearMapper emsProdplanYearMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

    @Override
    protected IBaseMapper<EmsProdplanYear, String> getMapper() {
        return emsProdplanYearMapper;
    }

//    @Override
//    public PageList<Map<String, Object>> getDateEditForPage(HashMap<String, Object> parmas, Integer pageNum,
//                                                            Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<Map<String, Object>> rows = emsProdplanYearMapper.getDateEdit(parmas);
//        PageList<Map<String, Object>> pageInfo = new PageList<Map<String, Object>>(rows);
//        return pageInfo;
//    }

//    @Override
//    public PageList<Map<String, Object>> getDateEditForPage(HashMap<String, Object> parmas, Integer pageNum, Integer pageSize) {
//        return null;
//    }

    @Override
    public List<Map<String, Object>> getDateForProduct(HashMap<String, Object> parmas) {
        return emsProdplanYearMapper.getDateForProduct(parmas);
    }

    @Override
    public int getDaysOfMonth(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    @Override
    public int getDaysOfMonth(Date date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    @Override
    public int[] avgWeight(Integer sumWeight, Integer day, Double days){
        int[] weightArr = new int[day];
       Integer avgWeight =(int) (sumWeight / days);
        for(int i = 0;i<day-1;i++){
            weightArr[i] = avgWeight;
        }
        weightArr[day-1] =sumWeight - (avgWeight * ( day - 1));
        return weightArr;
    }
    @Override
    public RESTfulResult insertexcel(File file) throws Exception {
        RESTfulResult rs = new RESTfulResult("200", "数据导入成功，不规范数据已自动剔除");
        rs.setCode("200");
        List<EmsProdplanYear> lsp = null;
        try {
            lsp = createCheckCar(file);
            if (lsp.size()>0)
            {
                Map map = new HashMap(16);
                for (EmsProdplanYear model : lsp) {
                    map.put("line",model.getLine());
                    map.put("yearmonth",new SimpleDateFormat("yyyy-MM").format(model.getYearmonth()));
                    map.put("productid",model.getProductid());
                    List list = this.getDateForProduct((HashMap<String, Object>) map);
                    if (list.size()>0){
                        continue;
                    } else {
                        this.add(model);
                    }
                }
            } else
            {
                rs.setMessage("导入失败,导入数据已经存在！");
                rs.setCode("500");
            }
        } catch (Exception e) {
            e.printStackTrace();
            rs.setCode("500");
            rs.setMessage(e.getMessage());
        } finally {

        }
        return rs;
    }
    public  List<EmsProdplanYear> createCheckCar(File file ) throws Exception, IOException {
        UserPayload payload = UserPayload.getCurrUser();
        FileInputStream is = null;
        String fileName = file.getName();
        Calendar calendar1=Calendar.getInstance();
        // 解决fileName兼容性问题
        int lastindex = fileName.lastIndexOf("\\");
        fileName = fileName.substring(lastindex + 1);
        List<EmsProdplanYear> lists = new ArrayList<>();
        if (fileName != null && fileName.length() > 0) {
            is = new FileInputStream(file);
        }
        List<List<String[]>> list = ExcelToolUtils.parseExcels(is, fileName, 0);
        String year = fileName.substring(0,4);
        final Pattern pattern = Pattern.compile("[0-9]*");
        try {
            for (int i = 0; i < list.size(); i++) {
                String[] arr2 = list.get(i).get(1);
                for(int j = 1; j < list.get(i).size(); j++) {
                    String[] arr = list.get(i).get(j);
                    String[] arr1 = list.get(i).get(0);
                 if( "".equals(arr[0].trim()) || !pattern.matcher(arr[0]).matches()){
                     arr2 = list.get(i).get(1);
                     continue;
                 }
                    for(int k =1;k<arr1.length;k++) {
                        EmsProdplanYear spp = new EmsProdplanYear();
                        calendar1.set(Calendar.YEAR, Integer.parseInt(year));
                        // 月份 丛0开始 所以减一
                        calendar1.set(Calendar.MONTH, Integer.parseInt(arr[0].trim()) - 1 );
                        //天数 1号
                        calendar1.set(Calendar.DAY_OF_MONTH, 1);
                        // 时
                        calendar1.set(Calendar.HOUR_OF_DAY, 0);
                        // 分
                        calendar1.set(Calendar.MINUTE, 0);
                        // 秒
                        calendar1.set(Calendar.SECOND, 0);
                        // 毫秒
                        calendar1.set(Calendar.MILLISECOND, 0);
                        spp.setYearmonth(calendar1.getTime());
                        //if(arr1[k].trim().indexOf("月份") != -1) {
                        //    // 月份 丛0开始 所以减一
                        //    calendar1.set(Calendar.MONTH, Integer.parseInt(arr[k].trim()) - 1 );
                        //    //天数 1号
                        //    calendar1.set(Calendar.DAY_OF_MONTH, 1);
                        //    // 时
                        //    calendar1.set(Calendar.HOUR_OF_DAY, 0);
                        //    // 分
                        //    calendar1.set(Calendar.MINUTE, 0);
                        //    // 秒
                        //    calendar1.set(Calendar.SECOND, 0);
                        //    // 毫秒
                        //    calendar1.set(Calendar.MILLISECOND, 0);
                        //    spp.setYearmonth(calendar1.getTime());
                        //}
                        // 烧结else
                        if (arr1[k].trim().indexOf("1#烧结") != -1) {
                            spp.setParentid("SJ");
                            spp.setLine("AT3013");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("2#烧结") != -1) {
                            spp.setParentid("SJ");
                            spp.setLine("AT3014");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("3#烧结") != -1) {
                            spp.setParentid("SJ");
                            spp.setLine("AT3015");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        }
                        // 铁水
                        else if (arr1[k].trim().indexOf("1#高炉") != -1) {
                            spp.setParentid("LT");
                            spp.setLine("AT3001");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("2#高炉") != -1) {
                            spp.setParentid("LT");
                            spp.setLine("AT3002");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("3#高炉") != -1) {
                            spp.setParentid("LT");
                            spp.setLine("AT3003");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("4#高炉") != -1) {
                            spp.setParentid("LT");
                            spp.setLine("AT3004");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        }
                        // 钢坯
                        else if ("供高线一车间方坯".equals(arr1[k].trim())) {
                            spp.setParentid("LG");
                            spp.setLine("AT2005-1");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("供高线二车间方坯".equals(arr1[k].trim())) {
                            spp.setParentid("LG");
                            spp.setLine("AT2006-1");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("异形坯".equals(arr1[k].trim())) {
                            spp.setParentid("LG");
                            spp.setLine("AT2007-1");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("外销方坯".equals(arr1[k].trim())) {
                            spp.setParentid("LG");
                            spp.setLine("AT3008-1");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        }
                        // 钢轧厂
                        else if ("高线一车间".equals(arr1[k].trim())) {
                            spp.setParentid("ZG");
                            spp.setLine("AT2005");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("高线二车间".equals(arr1[k].trim())) {
                            spp.setParentid("ZG");
                            spp.setLine("AT2006");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("H型钢".equals(arr1[k].trim())) {
                            spp.setParentid("ZG");
                            spp.setLine("AT2007");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        }
                        // 机焦厂
                        else if (arr1[k].trim().indexOf("成品焦") != -1) {
                            spp.setProductid("成品焦");
                            spp.setParentid("JJZ");
                            spp.setUnit("吨");
                            spp.setWeight(BigDecimal.ZERO);
                            for (int m = 0;m <= 2; m++ ){
                                if ("高炉用量".equals(arr2[k+m].trim())) {
                                    spp.setWeight1(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                } else if ("外销数量".equals(arr2[k+m].trim())) {
                                    spp.setWeight2(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                }
                            }
                            k +=2;
                        } else if (arr1[k].trim().indexOf("小粒焦") != -1) {
                            spp.setProductid("小粒焦");
                            spp.setParentid("JJZ");
                            spp.setUnit("吨");
                            spp.setWeight(BigDecimal.ZERO);
                            for (int m = 0;m <= 2; m++ ) {
                                if ("高炉用量".equals(arr2[k+m].trim())) {
                                    spp.setWeight1(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                } else if ("外销数量".equals(arr2[k+m].trim())) {
                                    spp.setWeight2(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                }
                            }
                            k +=2;
                        } else if (arr1[k].trim().indexOf("除尘焦粉") != -1) {
                            spp.setProductid("除尘焦粉");
                            spp.setParentid("JJZ");
                            spp.setUnit("吨");
                            spp.setWeight(BigDecimal.ZERO);
                            for (int m = 0;m <= 2; m++ ) {
                                if ("高炉用量".equals(arr2[k+m].trim())) {
                                    spp.setWeight1(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                } else if ("外销数量".equals(arr2[k+m].trim())) {
                                    spp.setWeight2(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                }
                            }
                            k +=2;
                        } else if (arr1[k].trim().indexOf("焦粉") != -1) {
                            spp.setProductid("焦粉");
                            spp.setParentid("JJZ");
                            spp.setUnit("吨");
                            spp.setWeight2(BigDecimal.ZERO);
                            for (int m = 0;m <= 2; m++ ) {
                                if ("烧结用量".equals(arr2[k+m].trim())) {
                                    spp.setWeight1(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                } else if ("预计外购".equals(arr2[k+m].trim())) {
                                    spp.setWeight(new BigDecimal(arr[k+m].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                                }
                            }
                            k +=2;
                        }
                        // 发电厂
                        else if (arr1[k].trim().indexOf("40MW") != -1) {
                            spp.setParentid("FDC");
                            spp.setLine("AT4002");
                            spp.setUnit("Kwh");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("45MW") != -1) {
                            spp.setParentid("FDC");
                            spp.setLine("AT4003");
                            spp.setUnit("Kwh");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("25MW煤气") != -1) {
                            spp.setParentid("FDC");
                            spp.setLine("AT4001");
                            spp.setUnit("Kwh");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("15MW") != -1) {
                            spp.setParentid("FDC");
                            spp.setLine("AT4004");
                            spp.setUnit("Kwh");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("25MW干熄焦") != -1) {
                            spp.setParentid("FDC");
                            spp.setLine("AT4009");
                            spp.setUnit("Kwh");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("TRT") != -1) {
                            spp.setParentid("FDC");
                            spp.setLine("AT3022");
                            spp.setUnit("Kwh");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        }
                        // 机焦副产品
                        else if ("焦油".equals(arr1[k].trim())) {
                            spp.setParentid("JJF");
                            spp.setLine("AT3024");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("粗笨".equals(arr1[k].trim())) {
                            spp.setParentid("JJF");
                            spp.setLine("AT3009");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("硫铵".equals(arr1[k].trim())) {
                            spp.setParentid("JJF");
                            spp.setLine("AT3010");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("硫酸".equals(arr1[k].trim())) {
                            spp.setParentid("JJF");
                            spp.setLine("AT3025");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("洗煤".equals(arr1[k].trim())) {
                            spp.setParentid("JJH");
                            spp.setProductid("AT2012");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        }
                        // 建材
                        else if (arr1[k].trim().indexOf("1#回转窑") != -1) {
                            spp.setParentid("JCS");
                            spp.setLine("AT3011");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if (arr1[k].trim().indexOf("2#回转窑") != -1) {
                            spp.setParentid("JCS");
                            spp.setLine("AT3012");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else if ("矿渣粉".equals(arr1[k].trim())) {
                            spp.setParentid("JCW");
                            spp.setProductid("AT2014");
                            spp.setUnit("吨");
                            spp.setWeight(new BigDecimal(arr[k].trim()).setScale(0, BigDecimal.ROUND_HALF_UP));
                        } else {
                            continue;
                        }
                        spp.setCjr(payload.getUserName());
                        spp.setCjsj(new Date());
                        lists.add(spp);
                    }
                }
            }
        } catch (Exception e){
            throw new MarketSlmException(500,"导入失败，请检查文档中是否有错误数据。");
        }
        return lists;
    }
}
