package com.zt.plat.module.base.controller.admin.materialproperties;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.common.exception.ServiceException;
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.excel.core.util.ExcelUtils;
import com.zt.plat.module.base.controller.admin.departmentmaterial.vo.DepartmentMaterialRespVO;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesPageReqVO;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesRespVO;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesSaveReqVO;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesSimplePageReqVO;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesSimpleRespVO;
import com.zt.plat.module.base.service.departmentmaterial.DepartmentMaterialService;
import com.zt.plat.module.base.service.materialproperties.MaterialPropertiesService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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("/base/material-properties-dept")
@Validated
public class MaterialPropertiesDeptController {


    @Resource
    private MaterialPropertiesService materialPropertiesService;

    @Resource
    private DepartmentMaterialService departmentMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建物料属性")
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:create')")
    public CommonResult<MaterialPropertiesRespVO> createMaterialProperties(@Valid @RequestBody MaterialPropertiesSaveReqVO createReqVO) {
        if (createReqVO.getDeptId() == null) {
            throw new ServiceException(401, "部门ID不能为空");
        }
        return success(materialPropertiesService.createMaterialProperties(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料属性")
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:update')")
    public CommonResult<Boolean> updateMaterialProperties(@Valid @RequestBody MaterialPropertiesSaveReqVO updateReqVO) {
        if (updateReqVO.getDeptId() == null) {
            throw new ServiceException(401, "部门ID不能为空");
        }
        materialPropertiesService.updateMaterialProperties(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:delete')")
    public CommonResult<Boolean> deleteMaterialProperties(@RequestParam("id") Long id) {
        materialPropertiesService.deleteMaterialProperties(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料属性")
                @PreAuthorize("@ss.hasPermission('base:material-properties-dept:delete')")
    public CommonResult<Boolean> deleteMaterialPropertiesList(@RequestBody BatchDeleteReqVO req) {
        materialPropertiesService.deleteMaterialPropertiesListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:query')")
    public CommonResult<MaterialPropertiesRespVO> getMaterialProperties(@RequestParam("id") Long id) {
        MaterialPropertiesRespVO materialProperties = materialPropertiesService.getMaterialProperties(id);
        if (materialProperties.getDeptId() == null) {
            throw new ServiceException(500, "没有权限");
        }
        return success(materialProperties);
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料属性分页")
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:query')")
    public CommonResult<PageResult<MaterialPropertiesRespVO>> getMaterialPropertiesPage(@Valid MaterialPropertiesPageReqVO pageReqVO) {
        if (pageReqVO.getDeptId() == null) {
            throw new ServiceException(500, "部门ID不能为空");
        }
        PageResult<MaterialPropertiesRespVO> pageResult = materialPropertiesService.getMaterialPropertiesPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/simple-page")
    @Operation(summary = "获得物料属性精简分页")
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:query')")
    public CommonResult<PageResult<MaterialPropertiesSimpleRespVO>> getMaterialPropertiesSimplePage(@Valid MaterialPropertiesSimplePageReqVO pageReqVO) {
        if (pageReqVO.getDeptId() == null) {
            throw new ServiceException(500, "部门ID不能为空");
        }
        return success(materialPropertiesService.getMaterialPropertiesSimplePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料属性 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-properties-dept:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialPropertiesExcel(@Valid MaterialPropertiesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        if (pageReqVO.getDeptId() == null) {
            throw new ServiceException(500, "部门ID不能为空");
        }
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialPropertiesRespVO> list = materialPropertiesService.getMaterialPropertiesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料属性.xls", "数据", MaterialPropertiesRespVO.class,
            list);
    }

}