package com.nantian.cloud.dbcapp.carbon.contorller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.*;
import com.nantian.cloud.dbcapp.carbon.service.InstrumentService;
import com.nantian.cloud.dbcapp.tool.ExcelWriteUtil;
import com.nantian.cloud.dbcapp.tool.HeaderAliasVo;
import com.nantian.cloud.dbcapp.tool.ReflectionUtils;
import com.nantian.cloud.dbcapp.unit.DateUtil;
import com.nantian.sc.cloud.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@RestController
@Api(value = "InstrumentController", tags = "碳计量-计量设备管理")
@RequestMapping(value = "/api/accountCarbon")
public class InstrumentController {


    @Autowired
    private InstrumentService instrumentService;

//    @Autowired
//    private ISysDeptService iSysDeptService;


    @ApiOperation("企业计量器具列表")
    @PostMapping("/getInstrumentList")
    public R getInstrumentList(@RequestParam(required = false) String entName,
                                           @RequestParam(required = false) String industryCode,
                                           @RequestParam(required = false) Long level,
                                           @RequestParam(required = false) String regionCode,
                                           @RequestParam int pageNum,
                                           @RequestParam int pageSize) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        IPage<EntInstrumentVo> selectDeptList = instrumentService.selectEntList(page,entName, industryCode, level, regionCode);
        //level
        List<InstrumentLevel> instrumentLevelList = instrumentService.selectLevelList();
        Map<Long, String> mapLevel = instrumentLevelList.stream().collect(Collectors.toMap(InstrumentLevel::getLevelCode, InstrumentLevel::getLevelName));
        //category
        List<InstrumentCategory> instrumentCategoryList = instrumentService.selectCategoryList();
        Map<Long, String> mapCat = instrumentCategoryList.stream().collect(Collectors.toMap(InstrumentCategory::getCategoryCode, InstrumentCategory::getCategoryName));
        //entConsumeType
        List<EntConsumeType> entConsumeTypeList = instrumentService.selectEntConsumeTypeList();
        Map<Long, String> mapentConsumeType = entConsumeTypeList.stream().collect(Collectors.toMap(EntConsumeType::getCode, EntConsumeType::getName));
        for (EntInstrumentVo entInstrumentVo : selectDeptList.getRecords()) {
            if (entInstrumentVo.getLevel() != null) {
                entInstrumentVo.setLevelName(mapLevel.get(entInstrumentVo.getLevel()));
            }
            if (entInstrumentVo.getCategory() != null) {
                entInstrumentVo.setCategoryName(mapCat.get(entInstrumentVo.getCategory()));
            }
            if (entInstrumentVo.getEntConsumeType() != null) {
                entInstrumentVo.setEntConsumeTypeName(mapentConsumeType.get(entInstrumentVo.getEntConsumeType()));
            }
        }
        return R.ok(selectDeptList);
    }

    @ApiOperation("统计卡片")
    @PostMapping("/getSumList")
    public EntEquTotal getSumList(@RequestParam(required = false) String entName,
                                  @RequestParam(required = false) String industryCode,
                                  @RequestParam(required = false) Long level,
                                  @RequestParam(required = false) String regionCode) {
        List<EntInstrumentVo> selectDeptList = instrumentService.selectEntListBySumList(entName, industryCode, level, regionCode);
        Integer entNum = selectDeptList.size();
        int requireNum = 0;
        int actualNum = 0;
        for (EntInstrumentVo e : selectDeptList) {
            requireNum += e.getRequire();
            actualNum += e.getActual();
        }
        BigDecimal rateToal = new BigDecimal(0);
        if (actualNum != 0 && requireNum != 0) {
            rateToal = BigDecimal.valueOf(actualNum).divide(BigDecimal.valueOf(requireNum), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.HALF_UP);
        }
        EntEquTotal entEquTotal = new EntEquTotal();
        entEquTotal.setRateTotal(rateToal);
        entEquTotal.setEntNum(entNum);
        entEquTotal.setActualNum(actualNum);
        entEquTotal.setRequireNum(requireNum);

        return entEquTotal;
    }

    @ApiOperation("企业下详细计量器具信息表")
    @PostMapping("/getMeasurementList")
    public R getMeasurementList(@RequestParam String entId) {
        //重写分页
//        startPage();
        return R.ok(instrumentService.getMeasurementList(entId));
    }

    @ApiOperation(value = "导出碳计量-计量器具管理信息表")
    @PostMapping("/export_excel")
    public void exportExcel(HttpServletResponse response,
                            @RequestParam(required = false) String entName,
                            @RequestParam(required = false) String industryCode,
                            @RequestParam(required = false) Long level,
                            @RequestParam(required = false) String regionCode,
                            @RequestParam int pageNum,
                            @RequestParam int pageSize) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        IPage<EntInstrumentVo> selectDeptList = instrumentService.selectEntList(page, entName, industryCode, level, regionCode);
        //level
        List<InstrumentLevel> instrumentLevelList = instrumentService.selectLevelList();
        Map<Long, String> mapLevel = instrumentLevelList.stream().collect(Collectors.toMap(InstrumentLevel::getLevelCode, InstrumentLevel::getLevelName));
        //category
        List<InstrumentCategory> instrumentCategoryList = instrumentService.selectCategoryList();
        Map<Long, String> mapCat = instrumentCategoryList.stream().collect(Collectors.toMap(InstrumentCategory::getCategoryCode, InstrumentCategory::getCategoryName));
        //entConsumeType
        List<EntConsumeType> entConsumeTypeList = instrumentService.selectEntConsumeTypeList();
        Map<Long, String> mapentConsumeType = entConsumeTypeList.stream().collect(Collectors.toMap(EntConsumeType::getCode, EntConsumeType::getName));

        for (EntInstrumentVo entInstrumentVo : selectDeptList.getRecords()) {
            if (entInstrumentVo.getLevel() != null) {
                entInstrumentVo.setLevelName(mapLevel.get(entInstrumentVo.getLevel()));
            }
            if (entInstrumentVo.getCategory() != null) {
                entInstrumentVo.setCategoryName(mapCat.get(entInstrumentVo.getCategory()));
            }
            if (entInstrumentVo.getEntConsumeType() != null) {
                entInstrumentVo.setEntConsumeTypeName(mapentConsumeType.get(entInstrumentVo.getEntConsumeType()));
            }
        }
//        ExcelWriteUtil.fileEasyExport(response, selectDeptList,EntInstrumentVo.class, "计量器具管理信息表", "计量器具管理信息表");
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat DateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String title =  DateFormat.format(calendar.getTime());
        List<HeaderAliasVo> headerAliasVoList = ReflectionUtils.getHeaderAliasVoList(EntInstrumentVo.class);
        ExcelWriteUtil.excelWrite(title,selectDeptList.getRecords(),headerAliasVoList,response);
    }

    @ApiOperation(value = "计量器具层级")
    @PostMapping("/level")
    public R getLevel(){
        return R.ok(instrumentService.selectLevelList());
    }

    @ApiOperation(value = "导出企业-计量器具")
    @PostMapping("/exportDetail")
    public void exportDetail(HttpServletResponse response,@RequestParam String entId) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat DateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String title =  DateFormat.format(calendar.getTime());
        List<InstrumentDetailVo> list = instrumentService.getMeasurementList(entId);
        List<HeaderAliasVo> headerAliasVoList = ReflectionUtils.getHeaderAliasVoList(InstrumentDetailVo.class);
        ExcelWriteUtil.excelWrite(title, list, headerAliasVoList, response);
//        ExcelWriteUtil.fileEasyExport(response, list,InstrumentDetailVo.class, "计量器具信息", "计量器具信息");
//        ExcelUtil<InstrumentDetailVo> util = new ExcelUtil<InstrumentDetailVo>(InstrumentDetailVo.class);
//        util.exportExcel(response, list, "计量器具详情");

    }


}
