package com.zt.plat.module.qms.business.config.controller.admin;

import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigPermissionRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationSaveReqVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigPermissionDO;
import com.zt.plat.module.qms.business.config.service.ConfigPermissionService;
import com.zt.plat.module.qms.enums.QmsPermissionConstant;
import com.zt.plat.module.qms.enums.QmsWarehouseLocationConstant;
import com.zt.plat.module.system.api.permission.PermissionApi;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO;
import com.zt.plat.module.qms.business.config.service.ConfigWarehouseLocationService;

@Tag(name = "管理后台 - 存放位置")
@RestController
@RequestMapping("/qms/config-warehouse-location")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ConfigWarehouseLocationController implements BusinessControllerMarker {


    @Resource private ConfigWarehouseLocationService configWarehouseLocationService;
    @Resource private ConfigPermissionService configPermissionService;
    @Resource private PermissionApi permissionApi;

    @PostMapping("/save")
    @Operation(summary = "创建存放位置")
//    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:create')")
    public CommonResult<ConfigWarehouseLocationRespVO> save(@Valid @RequestBody ConfigWarehouseLocationSaveReqVO createReqVO) {
        return success(configWarehouseLocationService.save(createReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建存放位置")
//    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:create')")
    public CommonResult<ConfigWarehouseLocationRespVO> createConfigWarehouseLocation(@Valid @RequestBody ConfigWarehouseLocationSaveReqVO createReqVO) {
        return success(configWarehouseLocationService.createConfigWarehouseLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新存放位置")
//    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:update')")
    public CommonResult<Boolean> updateConfigWarehouseLocation(@Valid @RequestBody ConfigWarehouseLocationSaveReqVO updateReqVO) {
        configWarehouseLocationService.updateConfigWarehouseLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除存放位置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:delete')")
    public CommonResult<Boolean> deleteConfigWarehouseLocation(@RequestParam("id") Long id) {
        configWarehouseLocationService.deleteConfigWarehouseLocation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除存放位置")
                @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:delete')")
    public CommonResult<Boolean> deleteConfigWarehouseLocationList(@RequestBody BatchDeleteReqVO req) {
        configWarehouseLocationService.deleteConfigWarehouseLocationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得存放位置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:query')")
    public CommonResult<ConfigWarehouseLocationRespVO> getConfigWarehouseLocation(@RequestParam("id") Long id) {
        ConfigWarehouseLocationDO configWarehouseLocation = configWarehouseLocationService.getConfigWarehouseLocation(id);
        List<ConfigPermissionDO> permissionList = configPermissionService.getPermissionList(id, QmsPermissionConstant.SAMPLE_WAREHOUSE_ADMIN, "");
        List<ConfigPermissionRespVO> permissionRespVOList= BeanUtils.toBean(permissionList, ConfigPermissionRespVO.class);
        ConfigWarehouseLocationRespVO vo = BeanUtils.toBean(configWarehouseLocation, ConfigWarehouseLocationRespVO.class);
        vo.setPermissionList(permissionRespVOList);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得存放位置分页")
//    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:query')")
    public CommonResult<PageResult<ConfigWarehouseLocationRespVO>> getConfigWarehouseLocationPage(@Valid ConfigWarehouseLocationPageReqVO pageReqVO) {
        PageResult<ConfigWarehouseLocationRespVO> pageResult = configWarehouseLocationService.getConfigWarehouseLocationPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/selectListWithPermission")
    @Operation(summary = "安权限查询报告类型")
    public CommonResult<List<ConfigWarehouseLocationRespVO>> selectListWithPermission(@Valid ConfigWarehouseLocationPageReqVO pageReqVO) {
        pageReqVO.setPermissionFilterFlag("1");
        pageReqVO.setSrcPermissionType(QmsPermissionConstant.SAMPLE_WAREHOUSE_ADMIN);
        pageReqVO.setDataType(QmsWarehouseLocationConstant.DATA_TYPE_WAREHOUSE);
        //查询权限
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Long userId = loginUser.getId();
        CommonResult<Set<Long>> roleRet = permissionApi.getUserRoleIdListByUserId(userId);
        Set<Long> roleIds = roleRet.getData();
        pageReqVO.setRoleIds(roleIds);
        List<ConfigWarehouseLocationDO> list = configWarehouseLocationService.selectListWithPermission(pageReqVO);
        return success(BeanUtils.toBean(list, ConfigWarehouseLocationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出存放位置 Excel")
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigWarehouseLocationExcel(@Valid ConfigWarehouseLocationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigWarehouseLocationRespVO> list = configWarehouseLocationService.getConfigWarehouseLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "存放位置.xls", "数据", ConfigWarehouseLocationRespVO.class, list);
    }

}