package com.bonus.material.devchange.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.contract.domain.BmContract;
import com.bonus.material.devchange.domain.*;
import com.bonus.material.devchange.service.MaDevInfoService;
import com.bonus.material.devchange.service.MaDevInfoServiceImpl;
import com.bonus.material.device.domain.vo.DevInfoPropertyVo;
import com.bonus.material.device.domain.vo.DevMergeVo;
import com.bonus.system.api.domain.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 设备台账-总览表
 *
 * @author bonus
 */
@RestController
@RequestMapping("/device")
public class MaDevInfoController extends BaseController {

    @Resource
    private MaDevInfoService service;

    /**
     * 设备台账
     */
    @ApiOperation(value = "设备台账")
    @GetMapping("/list")
    public AjaxResult list(MaDevInfo o) {
        try {
            startPage();
            List<MaDevInfo> list = service.list(o);
            return AjaxResult.success(getDataTable(list));
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }


    @ApiOperation(value = "设备台账-上下架")
    @PostMapping("/updateDeviceStatus")
    public AjaxResult updateDeviceStatus(@RequestBody DevMergeVo o) {
        try {
            Integer i = service.updateDeviceStatus(o);
            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }

    @ApiOperation(value = "设备台账-修改")
    @PostMapping("/updateDeviceApi")
    public AjaxResult updateDeviceApi(@RequestBody MaDevInfo o) {
        try {
            Integer i = service.updateDeviceApi(o);
            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }

    @ApiOperation(value = "设备台账-获取单个设备详情")
    @GetMapping("/getDevice/{maId}")
    public AjaxResult getDevice(@PathVariable Integer maId) {
        return AjaxResult.success(service.getDevice(maId));
    }


    @ApiOperation(value = "设备台账-获取单个设备详情")
    @GetMapping("/getDeviceByMaIds/{maIds}")
    public AjaxResult getDeviceByMaIds(@PathVariable String maIds) {
        return service.getDeviceByMaIds(maIds);
    }


    @ApiOperation(value = "设备台账-删除")
    @PostMapping("/delDevice/{ids}")
    public AjaxResult delDevice(@PathVariable("ids") String[] ids) {
        try {
            Integer i = service.delDevice(ids);
            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }

    @ApiOperation(value = "设备类型树")
    @PostMapping("/deviceTree")
    public AjaxResult deviceTree(String id) {
        try {
            DeviceTreeBean list = service.deviceTree(id);
            return AjaxResult.success("修改成功", list);
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }

    @ApiOperation(value = "设备统计数据")
    @PostMapping("/deviceCount")
    public AjaxResult deviceCount(@RequestBody MaDevInfo o) {
        try {
            DeviceCountBean list = service.deviceCount(o);
            return AjaxResult.success("修改成功", list);
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }

    @ApiOperation(value = "设备范围-所在地")
    @PostMapping("/deviceLocationSel")
    public AjaxResult deviceLocationSel(String id) {
        try {
            List<MapBean> list = service.deviceLocationSel(id);
            return AjaxResult.success("修改成功", list);
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }

    @ApiOperation(value = "公司-地市公司")
    @PostMapping("/companySel")
    public AjaxResult companySel(String id) {
        try {
            List<MapBean> list = service.companySel(id);
            return AjaxResult.success("修改成功", list);
        } catch (Exception e) {
            logger.error("报错啦", e);
            return AjaxResult.error("调用异常，请联系管理员");
        }
    }


    /**
     * 导出设备信息为Excel
     *
     * @param o        查询条件（同列表接口）
     * @param response 响应对象
     */
    @ApiOperation("导出设备信息Excel")
    @PostMapping("/export")
    public void export(MaDevInfo o, HttpServletResponse response) throws IOException {
        // 1. 查询所有符合条件的数据（忽略分页）
        List<MaDevInfoXlsx> dataList = service.export(o);
        com.bonus.common.core.utils.poi.ExcelUtil<MaDevInfoXlsx> util = new ExcelUtil<MaDevInfoXlsx>(MaDevInfoXlsx.class);
        util.exportExcel(response, dataList, "装备台账");
    }



}
