package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.domain.PcLabel;
import cn.cecep.talroad.domain.PcLabelFactory;
import cn.cecep.talroad.domain.PcLabelValue;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.service.IPcLabelFactoryService;
import cn.cecep.talroad.service.IPcLabelService;
import cn.cecep.talroad.service.IPcLabelValueService;
import cn.cecep.talroad.vo.PcLabelVo;
import cn.cecep.talroad.vo.query.PcLabelQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;


/**
 * * 开发公司：中节能天融科技有限公司
 * 特征标签Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-10*/
@Api(tags = "基本档案--特征标签")
@RestController
@RequestMapping("/pcLabel")
public class PcLabelController extends BasesController
{
    @Autowired
    private IPcLabelService pcLabelService;

    @Autowired
    private IPcLabelValueService pcLabelValueService;

    @Autowired
    private IPcLabelFactoryService pcLabelFactoryService;
    @Autowired
    private IPcFactoryService pcFactoryService;
    /**
     * 获取父级特征标签
     */
    @ApiOperation(value = "树结构", notes = "适用于首页未选中状态")
    @GetMapping("/tree/{id}")
    public AjaxResults<List<PcLabelVo>> tree(@PathVariable String id, String factoryId)
    {
        List<PcLabelVo> tree = pcLabelService.tree(id, factoryId);
        List<PcLabelFactory> amongs = pcLabelFactoryService.list(new QueryWrapper<PcLabelFactory>().eq("factory_id", factoryId));
        tree.stream().forEach(item->{
            Optional<PcLabelFactory> any = amongs.stream().filter(am -> am.getLabelId().equals(item.getId())).findFirst();
            if(any.isPresent()){
                PcLabelFactory pcLabelFactory = any.get();
                if(StringUtils.isNotEmpty(pcLabelFactory.getLabelValue())){
                    item.setSelectedIds(pcLabelFactory.getLabelValue().split(","));
                }
            }
        });
        return AjaxResults.success(tree);
    }

    @ApiOperation(value = "获取指定父标签下的子标签")
    @GetMapping("/children")
    public AjaxResults<List<PcLabelVo>> getChildrenLabel(@RequestParam(value = "id", required = false) String id)
    {
        return AjaxResults.success(pcLabelService.getChildrenLabel(id));
    }

    /**
     * 查询特征标签列表
     */
    @ApiOperation(value = "查询特征标签")
    @GetMapping("/list")
    public AjaxResults<IPage<PcLabel>> list(PcLabelQuery pcLabel)
    {
        Page<PcLabel> page = new Page<>(pcLabel.getPageNum(), pcLabel.getPageSize());
        IPage<PcLabel> list = pcLabelService.page(page, PcLabelQuery.createLambdaQueryWrapper(pcLabel));
        return AjaxResults.success(list);
    }


    /**
     * 导出特征标签列表
     */
    @ApiOperation(value = "导出特征标签")
    @Log(title = "特征标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcLabel pcLabel)
    {
        List<PcLabel> list = pcLabelService.selectPcLabelList(pcLabel);
        ExcelUtil<PcLabel> util = new ExcelUtil<PcLabel>(PcLabel.class);
        util.exportExcel(response, list, "特征标签数据");
    }

    /**
     * 获取特征标签详细信息
     */
    @ApiOperation(value = "获取特征标签详细信息")
    @PostMapping(value = "/getById/{id}")
    public AjaxResults<PcLabel> getInfo(@PathVariable("id") String id) {
        return AjaxResults.success(pcLabelService.getById(id));
    }
    /**
     * 新增特征标签
     */
    @ApiOperation(value = "新增特征标签")
    @Log(title = "特征标签", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults add(@RequestBody PcLabel pcLabel) {
        pcLabelService.add(pcLabel);
        return AjaxResults.success(true);
    }
    /**
     * 修改特征标签
     */
    @ApiOperation(value = "修改特征标签")
    @Log(title = "特征标签", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody PcLabelVo pcLabelVo) {
        pcLabelService.edit(pcLabelVo);
        return AjaxResults.success(true);
    }

    /**
     * 删除特征标签
     */
    @ApiOperation(value = "删除特征标签")
    @RequiresPermissions("talroad:label:remove")
    @Log(title = "特征标签", businessType = BusinessType.DELETE)
    @PostMapping("/{id}")
    public AjaxResults<Boolean> remove(@PathVariable String id) {
        pcLabelService.delete(id);
        return AjaxResults.success(true);
    }

    /**
     * 统计企业标签：许可管理类别，许可是否整改，管控级别，生态环境信用级别，所在地是否属于大气重点控制区，所在地是否属于总磷控制区，
     * 所在地是否属于总氮控制区，所在地是否属于重金属污染物特别排放限值实施区域，是否位于工业园区定时任务
     */
    @ApiOperation(value = "统计企业标签基础信息")
    @GetMapping("/updateLabelStatus")
    public void updateLabelStatus(){
        pcLabelFactoryService.updateLabelStatus();
    }

    @ApiOperation(value = "统计企业标签：大气主要污染物种类，废水主要污染物种类")
    @GetMapping("/statisticsAirWastewater")
    public void statisticsAirWastewater(){
        pcLabelFactoryService.statisticsAirWastewater();
    }

    @ApiOperation(value = "统计企业标签：有许可总量要求的污染物")
    @GetMapping("/statisticsLicensed")
    public void statisticsLicensed(){
        pcLabelFactoryService.statisticsLicensed();
    }

    @ApiOperation(value = "统计企业标签：承诺更加严格排放浓度限值")
    @GetMapping("/statisticseMissions")
    public void statisticseMissions(){
        pcLabelFactoryService.statisticseMissions();
    }
    @ApiOperation(value = "统计企业标签：运输车辆类型")
    @GetMapping("/transportVehicleType")
    public void transportVehicleType(){
        pcLabelFactoryService.transportVehicleType();
    }
    @ApiOperation(value = "统计企业标签：废水排放方式")
    @GetMapping("/wastewaterDischarge")
    public void wastewaterDischarge(){
        pcLabelFactoryService.wastewaterDischarge();
    }
    @ApiOperation(value = "统计企业标签：受纳水体功能目标")
    @GetMapping("/receivingWaterBody")
    public void receivingWaterBody(){
        pcLabelFactoryService.receivingWaterBody();
    }

    @ApiOperation(value = "统计企业标签：排污许可证是否在有效期内")
    @GetMapping("/efficientlyDischargePollutants")
    public void efficientlyDischargePollutants(){
        pcLabelFactoryService.efficientlyDischargePollutants(1);
    }

    @ApiOperation(value = "统计企业标签：许可证有效期不足1个月")
    @GetMapping("/efficientlyDischargePollutantsMonth")
    public void efficientlyDischargePollutantsMonth(){
        pcLabelFactoryService.efficientlyDischargePollutants(2);
    }
    @ApiOperation(value = "统计企业标签：受纳自然水体名称")
    @GetMapping("/acceptNature")
    public void acceptNature(){
        pcLabelFactoryService.acceptNature();
    }

    @ApiOperation(value = "统计企业标签：是否为直排海企业")
    @GetMapping("isDirectDischargeEnterprises")
    public void isDirectDischargeEnterprises(){
        pcLabelFactoryService.isDirectDischargeEnterprises();
    }
    @ApiOperation(value = "统计企业标签：在线监测设备安装情况是否符合许可证要求")
    @GetMapping("monitoringEquipment")
    public void monitoringEquipment(){
        pcLabelFactoryService.monitoringEquipment();
    }
    @ApiOperation(value = "统计企业标签：包含数据类型")
    @GetMapping("selectAdslFactory")
    public void selectAdslFactory(){
        pcLabelFactoryService.selectAdslFactory();
    }
    @ApiOperation(value = "统计企业标签：定时同步企业表数据与标签数据")
    @GetMapping("batchCreateLabel")
    public void batchCreateLabel(){
        pcFactoryService.batchCreateLabel();
    }
    @ApiOperation(value = "统计企业标签：行业类别")
    @GetMapping("industryCategory")
    public void industryCategory(){
        pcLabelFactoryService.industryCategory();
    }
    @ApiOperation(value = "统计企业标签：定时同步企业表数据与标签数据")
    @GetMapping("totalVideo")
    public void totalVideo(){
        pcLabelFactoryService.totalVideo();
    }
    @ApiOperation(value = "固定源关联分析：管控浓度超标预警")
    @GetMapping("earlyWarning")
    public void earlyWarning(){
        pcLabelFactoryService.earlyWarning();
    }
    @ApiOperation(value = "统计企业标签：大气管控监管企业")
    @GetMapping("testindustryCategory")
    public void testindustryCategory(){
        pcLabelFactoryService.testindustryCategory();
    }
    @ApiOperation(value = "统计企业标签：大气企业监管行业")
    @GetMapping("testRegulateBusinesses")
    public void testRegulateBusinesses(){
        pcLabelFactoryService.testRegulateBusinesses();
    }

    @ApiOperation(value = "统计企业标签：主要风险源类别")
    @PostMapping("/updateMajorPollutantCategory")
    public void updateMajorPollutantCategory(){
        pcLabelFactoryService.updateMajorPollutantCategory();
    }
    @ApiOperation(value = "统计企业标签：是否有排污许可证")
    @PostMapping("/pollutantLicenseLabel")
    public void pollutantLicenseLabel(){
        pcLabelFactoryService.pollutantLicenseLabel();
    }

    @ApiOperation(value = "统计企业标签：危险废物单位监管类型")
    @PostMapping("/licenseTypeLabel")
    public void licenseTypeLabel(){
        pcLabelFactoryService.licenseTypeLabel();
    }

    @ApiOperation(value = "统计企业标签：危险废物单位类型")
    @PostMapping("/pollutantTypeLabel")
    public void pollutantTypeLabel(){
        pcLabelFactoryService.pollutantTypeLabel();
    }

    @ApiOperation(value = "统计企业标签：是否安装分表计电")
    @PostMapping("/isInstallFbjdLabel")
    public void isInstallFbjdLabel(){
        pcLabelFactoryService.isInstallFbjdLabel();
    }

    @ApiOperation(value = "统计企业标签：是否安装无组织监控点位")
    @PostMapping("/isInstallWzzjkdwLabel")
    public void isInstallWzzjkdwLabel(){
        pcLabelFactoryService.isInstallWzzjkdwLabel();
    }

    @ApiOperation(value = "统计企业标签：1月内治理设施是否存在异常运行")
    @PostMapping("/isZlssExceptionInMonthLabel")
    public void isZlssExceptionInMonthLabel(){
        pcLabelFactoryService.isZlssExceptionInMonthLabel();
    }

    @ApiOperation(value = "统计企业标签：1月内是否有无组织超标排放情况")
    @PostMapping("/isWzzcbpfExceptionInMonthLabel")
    public void isWzzcbpfExceptionInMonthLabel(){
        pcLabelFactoryService.isWzzcbpfExceptionInMonthLabel();
    }
}
