package com.nokia.tsl_data.controller;

import com.nokia.tsl_data.entity.vo.R;
import com.nokia.tsl_data.service.DataWarehouseService;
import com.nokia.tsl_data.service.HighQualityDataService;
import com.nokia.tsl_data.service.TaskService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("tsl_data/source")
public class DataWarehouseController {

    @Autowired
    private DataWarehouseService dataWarehouseService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HighQualityDataService highQualityDataService;

    @PostMapping("highQualityData/generate")
    public R generateHighQualityData(@RequestBody String day) {
        try {
            highQualityDataService.generateHighQualityData(day);
            return R.ok().message(String.format("HighQualityData 账期 %s 生成成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("HighQualityData 账期 %s 生成失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("highQualityData/delete")
    public R deleteHighQualityData(@RequestBody String day) {
        try {
            highQualityDataService.deleteHighQualityData(day);
            return R.ok().message(String.format("HighQualityData 账期 %s 删除成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("HighQualityData 账期 %s 删除失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("warehouse/mobile_complaint")
    public R warehouseMobileComplaint(@RequestBody String day) {
        try {
            dataWarehouseService.warehouseMobileComplaintDay(day);
            return R.ok().message(String.format("管理端详表 MobileComplaint 数据账期 %s 入库成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("管理端详表 MobileComplaint 数据账期 %s 入库失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("delete/mobile_complaint")
    public R deleteMobileComplaint(@RequestBody String day) {
        try {
            int count = dataWarehouseService.deleteMobileComplaintDay(day);
            return R.ok().message(String.format("管理端详表 MobileComplaint 数据账期 %s 删除 %s 条", day, count));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("管理端详表 MobileComplaint 数据账期 %s 删除失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("warehouse/high_quality_count")
    public R warehouseHighQualityCount(@RequestBody String day) {
        try {
            dataWarehouseService.warehouseHighQualityCountDay(day);
            return R.ok().message(String.format("客户端统计 HighQualityCount 数据账期 %s 入库成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("客户端统计 HighQualityCount 数据账期 %s 入库失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("delete/high_quality_count")
    public R deleteHighQualityCount(@RequestBody String day) {
        try {
            int count = dataWarehouseService.deleteHighQualityCountDay(day);
            return R.ok().message(String.format("客户端统计 HighQualityCount 数据账期 %s 删除 %s 条", day, count));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("客户端统计 HighQualityCount 数据账期 %s 删除失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("warehouse/high_quality_list")
    public R warehouseHighQualityList(@RequestBody String day) {
        try {
            dataWarehouseService.warehouseHighQualityListDay(day);
            return R.ok().message(String.format("客户端详表 HighQualityList 数据账期 %s 入库成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("客户端详表 HighQualityList 数据账期 %s 入库失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("delete/high_quality_list")
    public R deleteHighQualityList(@RequestBody String day) {
        try {
            int count = dataWarehouseService.deleteHighQualityListDay(day);
            return R.ok().message(String.format("客户端详表 HighQualityList 数据账期 %s 删除 %s 条", day, count));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("客户端详表 HighQualityList 数据账期 %s 删除失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("update/work_flow_basic_data")
    public R updateWorkFlowBasicData() {
        try {
            taskService.updateWorkFlowBasicDataTask();
            return R.ok().message("工作流基础数据更新成功...");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("工作流基础数据更新失败: %s", e.getMessage()));
        }
    }

    @PostMapping("warehouse/complaint_details_fix_ywd")
    public R warehouseComplaintDetailsFixYwdDay(@RequestBody String day) {
        try {
            dataWarehouseService.warehouseComplaintDetailsFixYwdDay(day);
            return R.ok().message(String.format("管理端详表 ComplaintDetailsFix(河北客户体验管理智能定责投诉明细月累计接口日) 数据账期 %s 入库成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("管理端详表 ComplaintDetailsFix(河北客户体验管理智能定责投诉明细月累计接口日) 数据账期 %s 入库失败: %s",
                    day, e.getMessage()));
        }
    }

    @PostMapping("delete/complaint_details_fix_ywd")
    public R deleteComplaintDetailsFixYwdDay(@RequestBody String day) {
        try {
            int count = dataWarehouseService.deleteComplaintDetailsFixYwdDay(day);
            return R.ok().message(
                    String.format("客户端详表 ComplaintDetailsFix(河北客户体验管理智能定责投诉明细月累计接口日) 数据账期 %s 删除 %s 条", day, count));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("客户端详表 ComplaintDetailsFix(河北客户体验管理智能定责投诉明细月累计接口日) 数据账期 %s 删除失败: %s",
                    day, e.getMessage()));
        }
    }

    @PostMapping("warehouse/cem_mobile_experience_list")
    public R warehouseCemMobileExperienceList(@RequestBody String day) {
        try {
            dataWarehouseService.warehouseCemMobileExperienceList(day);
            return R.ok().message(String.format("管理端详表 ComplaintDetailsFix(河北客户体验管理智能定责投诉明细月累计接口日) 数据账期 %s 入库成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("管理端详表 ComplaintDetailsFix(河北客户体验管理智能定责投诉明细月累计接口日) 数据账期 %s 入库失败: %s",
                    day, e.getMessage()));
        }
    }

    @PostMapping("delete/cem_mobile_experience_list")
    public R deleteCemMobileExperienceList(@RequestBody String day) {
        try {
            int count = dataWarehouseService.deleteCemMobileExperienceList(day);
            return R.ok().message(
                    String.format("CemMobileExperienceList(河北客户体验管理系统移网网络体验明细日信息) 数据账期 %s 删除 %s 条", day, count));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("CemMobileExperienceList(河北客户体验管理系统移网网络体验明细日信息) 数据账期 %s 删除失败: %s",
                    day, e.getMessage()));
        }
    }

    @PostMapping("update/management_detail")
    public R updateManagementDetail(@RequestBody String day) {
        try {
            dataWarehouseService.updateManagementDetail(day);
            return R.ok().message(String.format("ManagementDetail 帐期 %s 更新成功", day));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("ManagementDetail 帐期 %s 更新失败: %s", day, e.getMessage()));
        }
    }

    @PostMapping("delete/management_detail")
    public R deleteManagementDetail(String day) {
        try {
            int count = dataWarehouseService.deleteManagementDetail(day);
            return R.ok().message(
                    String.format("客户端详表 ManagementDetail 数据账期 %s 删除 %s 条", day, count));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error().message(String.format("客户端详表 ManagementDetail 数据账期 %s 删除失败: %s",
                    day, e.getMessage()));
        }
    }
}
