package com.zt.plat.module.qms.resource.device.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.core.constant.DataTypeConstant;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationSaveReqVO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceInfomationDO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceProductDO;
import com.zt.plat.module.qms.resource.device.service.DeviceInfomationService;
import com.zt.plat.module.qms.resource.device.service.DeviceProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设备-设备信息")
@RestController
@RequestMapping("/qms/resource/device-infomation")
@Validated
@FileUploadController(source = "resource.deviceinfomation")
public class DeviceInfomationController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = DeviceInfomationController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource private DeviceInfomationService deviceInfomationService;
    @Resource private DeviceProductService deviceProductService;

    @PostMapping("/create")
    @Operation(summary = "创建设备-设备信息")
    @PreAuthorize("@ss.hasPermission('resource:device-infomation:create')")
    public CommonResult<DeviceInfomationRespVO> createDeviceInfomation(@Valid @RequestBody DeviceInfomationSaveReqVO createReqVO) {
        return success(deviceInfomationService.createDeviceInfomation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备-设备信息")
    @PreAuthorize("@ss.hasPermission('resource:device-infomation:update')")
    public CommonResult<Boolean> updateDeviceInfomation(@Valid @RequestBody DeviceInfomationSaveReqVO updateReqVO) {
        deviceInfomationService.updateDeviceInfomation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备-设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:device-infomation:delete')")
    public CommonResult<Boolean> deleteDeviceInfomation(@RequestParam("id") Long id) {
        deviceInfomationService.deleteDeviceInfomation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备-设备信息")
                @PreAuthorize("@ss.hasPermission('resource:device-infomation:delete')")
    public CommonResult<Boolean> deleteDeviceInfomationList(@RequestBody BatchDeleteReqVO req) {
        deviceInfomationService.deleteDeviceInfomationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备-设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:device-infomation:query')")
    public CommonResult<DeviceInfomationRespVO> getDeviceInfomation(@RequestParam("id") Long id) {
        DeviceInfomationDO deviceInfomation = deviceInfomationService.getDeviceInfomation(id);
        return success(BeanUtils.toBean(deviceInfomation, DeviceInfomationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备-设备信息分页")
    @PreAuthorize("@ss.hasPermission('resource:device-infomation:query')")
    public CommonResult<PageResult<DeviceInfomationRespVO>> getDeviceInfomationPage(@Valid DeviceInfomationPageReqVO pageReqVO) {
        Long productId = pageReqVO.getProductId();
        if(productId != null){
            List<DeviceProductDO> productDOList = deviceProductService.listByIdPath(productId, DataTypeConstant.DATA_TYPE_DATA);
            List<Long> productIds = productDOList.stream().map(DeviceProductDO::getId).toList();
            pageReqVO.setProductIds(productIds);
            pageReqVO.setProductId(null);
        }
        PageResult<DeviceInfomationDO> pageResult = deviceInfomationService.getDeviceInfomationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceInfomationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备-设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('resource:device-infomation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceInfomationExcel(@Valid DeviceInfomationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceInfomationDO> list = deviceInfomationService.getDeviceInfomationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备-设备信息.xls", "数据", DeviceInfomationRespVO.class,
                        BeanUtils.toBean(list, DeviceInfomationRespVO.class));
    }

}