package com.nantian.cloud.jljc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.*;
import com.nantian.cloud.jljc.service.*;
import com.nantian.cloud.jljc.vo.Query;
import com.nantian.cloud.jljc.vo.TSuperviseTaskOrganizationVo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 节能监察任务汇总表
 *
 * @date 2020-09-21
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSuperviseTaskSummary")
@Api(value = "tSuperviseTaskSummary", tags = "节能监察任务汇总表")
public class TSuperviseTaskSummaryController {
    @Autowired
    private TSuperviseTaskService tSuperviseTaskService;
    @Autowired
    private EnterpriseInfoService enterpriseInfoService;
    @Autowired
    private TSupervisionUnitEnergyService tSupervisionUnitEnergyService;
    @Autowired
    private TSupervisionUnitItemService tSupervisionUnitItemService;


    /**
     * 节能监察任务汇总表
     * 返回值-是否达标（1：达标/未超额，2不达标/超额）
     */
    @GetMapping("/summary")
    @SysLog("节能监察任务汇总表")
    @ApiOperation(value = "节能监察任务汇总表", notes = "节能监察任务汇总表")
    @ApiImplicitParam(name = "Query", value = "查询条件（企业名称name、企业信用编码code、地区area、年份year、汇总表产品item" +
            "监察类型(1书面监察,2现场监察)type 、任务来源(年度计划,抽查,复查,移交,举报投诉)source 、特征码字符集 num、单个特征码1 id、单个特征码2 id2）",
            required = false, dataType = "json", paramType = "path")
    public R getSuperviseTaskSummary(Query query) {

        if (query.getNum() == null || "".equals(query.getNum())) {
            return R.failed("特征码字符集num，不能为空");
        }
        if (query.getItem() == null || "".equals(query.getItem())) {
            query.setItem("1"); //默认 汇总工业硅
        }
        if (query.getYear() == null || 0 == query.getYear()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date date = new Date();
            query.setYear(Integer.parseInt(sdf.format(date)));  //默认 系统当前年份
        }
        if (query.getType() == null || "".equals(query.getType())) {
            query.setType("2");  //默认 现场监察
        }
        if (query.getArea() != null && !"".equals(query.getArea())) {
            if (query.getArea().contains("0000")) {
                query.setArea(StrUtil.subWithLength(query.getArea(), 0, 2)); //截取前2位
            } else if (query.getArea().contains("00")) {
                query.setArea(StrUtil.subWithLength(query.getArea(), 0, 4)); //截取前4位
            }
        }

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        String[] split = query.getNum().split(","); //特征码数组

        if(query.getYear()!=0 && query.getYear()<=2019){ //查询历史数据
            return R.ok(tSuperviseTaskService.getHistoryData(query));
        }

        if ("1".equals(query.getItem())) {  //工业硅
            result = tSuperviseTaskService.getSuperviseTaskSummary1(query);
        }else if ("4".equals(query.getItem())) {  //电解铝，特征码有多个
            result = tSuperviseTaskService.getSuperviseTaskSummary5(query);
        }else{
            for (String s : split) {
                List<Map<String, Object>> r = new ArrayList<Map<String, Object>>();
                query.setId(s);  //放入单个特征码
                if ("24".equals(query.getItem())) {  //水泥，特征码有多个
                    if ("323".equals(query.getId())) {
                        //特征码为323的熟料 才取熟料设计产能
                        query.setId2("323");
                    } else {
                        query.setId2("0");
                    }
                    r = tSuperviseTaskService.getSuperviseTaskSummary2(query);
                } else if ("39".equals(query.getItem())) {  //陶瓷，特征码有多个
                    r = tSuperviseTaskService.getSuperviseTaskSummary3(query);
                } else if ("17".equals(query.getItem())) {  //黄磷，特征码有多个
                    r = tSuperviseTaskService.getSuperviseTaskSummary4_1(query);
                } else if ("41".equals(query.getItem())) {  //磷化工（磷酸一铵、磷酸二铵），特征码有多个
                    r = tSuperviseTaskService.getSuperviseTaskSummary4_2(query);
                } else if ("6".equals(query.getItem())) {  //钢铁，特征码有多个
                    r = tSuperviseTaskService.getSuperviseTaskSummary6(query);
                } else if ("40".equals(query.getItem())) {  //再生铅，特征码有多个
                    r = tSuperviseTaskService.getSuperviseTaskSummary7(query);
                } else if ("30".equals(query.getItem())) {  //铁合金，特征码有多个
                    //能耗等级 一级：先进值；二级：准入值；三级：限定值
                    r = tSuperviseTaskService.getSuperviseTaskSummary8(query);
                } else if ("21".equals(query.getItem())) {  //铅冶炼，特征码有多个
                    if ("666".equals(query.getId())) {
                        //特征码为666的产品产量 从571读取
                        query.setId2("571");
                    } else {
                        query.setId2(query.getId());
                    }
                    r = tSuperviseTaskService.getSuperviseTaskSummary9(query);
                } else if ("32".equals(query.getItem())) {  //铜冶炼，特征码有多个
                    //铜冶炼特征码：546,547,549,550,554,555,557（548不对标）
                    if ("555".equals(query.getId())) {
                        //555同一个监察数据，有多条汇总数据
                        r = tSuperviseTaskService.getSuperviseTaskSummary10_1(query);
                    }else if ("557".equals(query.getId())) {
                        //557同一个监察数据，有多条汇总数据
                        r = tSuperviseTaskService.getSuperviseTaskSummary10_2(query);
                    }else if ("549".equals(query.getId())) {
                        //549对标信息需计算，从548获取数据计算
                        r = tSuperviseTaskService.getSuperviseTaskSummary10_3(query);
                    }else if ("546".equals(query.getId())) {
                        //546对标信息需计算，从549或550获取数据计算
                        r = tSuperviseTaskService.getSuperviseTaskSummary10_4(query);
                    }else if ("547".equals(query.getId())) {
                        //547同一个监察数据，有多条汇总数据
                        //547对标信息需计算，从546获取数据计算
                        r = tSuperviseTaskService.getSuperviseTaskSummary10_5(query);
                    }else{
                        //550、554
                        r = tSuperviseTaskService.getSuperviseTaskSummary10_6(query);
                    }
                } else if ("34".equals(query.getItem())) {  //锌冶炼，特征码有多个
                    r = tSuperviseTaskService.getSuperviseTaskSummary11(query);
                }
                if (r.size() > 0) {
                    result.addAll(r);
                }
            }
        }

        if (result.size() > 0) { //查询的结果集大于0，进行排序
            result.sort(new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    String v1 = o1.get("epcName").toString();
                    String v2 = o2.get("epcName").toString();
                    if (v1 != null) {
                        return v1.compareTo(v2);
                    }
                    return 0;
                }
            });
        }

        Map<String, Object> data = new HashMap<>();
        data.put("type","1"); //系统当前数据
        data.put("data",result);
        return R.ok(data);
    }

}
