package cn.iocoder.yudao.module.wms.controller.admin.industrycategory;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.wms.controller.admin.industrycategory.vo.IndustryCategoryPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.industrycategory.vo.IndustryCategoryRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.industrycategory.vo.IndustryCategorySaveReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.industrycategoryproperty.vo.IndustryCategoryPropertyRespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.industrycategory.IndustryCategoryDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.industrycategoryproperty.IndustryCategoryPropertyDO;
import cn.iocoder.yudao.module.wms.service.industrycategory.IndustryCategoryService;
import cn.iocoder.yudao.module.wms.service.industrycategoryproperty.IndustryCategoryPropertyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 行业分类")
@RestController
@RequestMapping("/wms/industry-category")
@Validated
public class IndustryCategoryController {

    @Resource
    private IndustryCategoryService industryCategoryService;
    @Resource
    private IndustryCategoryPropertyService industryCategoryPropertyService;


    @PostMapping("/create")
    @Operation(summary = "创建行业分类")
    @PreAuthorize("@ss.hasPermission('wms:industry-category:create')")
    public CommonResult<Long> createIndustryCategory(@Valid @RequestBody IndustryCategorySaveReqVO createReqVO) {
        return success(industryCategoryService.createIndustryCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新行业分类")
    @PreAuthorize("@ss.hasPermission('wms:industry-category:update')")
    public CommonResult<Boolean> updateIndustryCategory(@Valid @RequestBody IndustryCategorySaveReqVO updateReqVO) {
        industryCategoryService.updateIndustryCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除行业分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:industry-category:delete')")
    public CommonResult<Boolean> deleteIndustryCategory(@RequestParam("id") Long id) {
        industryCategoryService.deleteIndustryCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得行业分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:industry-category:query')")
    public CommonResult<IndustryCategoryRespVO> getIndustryCategory(@RequestParam("id") Long id) {
        IndustryCategoryDO industryCategory = industryCategoryService.getIndustryCategory(id);
        IndustryCategoryRespVO vo = BeanUtils.toBean(industryCategory, IndustryCategoryRespVO.class);
        List<IndustryCategoryPropertyDO> industryCategoryPropertyList = industryCategoryPropertyService.getByIndustryCategoryId(id);
        List<IndustryCategoryPropertyRespVO> propertyRespVOS = BeanUtils.toBean(industryCategoryPropertyList, IndustryCategoryPropertyRespVO.class);
        vo.setItems(propertyRespVOS);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得行业分类分页")
    @PreAuthorize("@ss.hasPermission('wms:industry-category:query')")
    public CommonResult<PageResult<IndustryCategoryRespVO>> getIndustryCategoryPage(@Valid IndustryCategoryPageReqVO pageReqVO) {
        PageResult<IndustryCategoryDO> pageResult = industryCategoryService.getIndustryCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IndustryCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出行业分类 Excel")
    @PreAuthorize("@ss.hasPermission('wms:industry-category:export')")
    @OperateLog(type = EXPORT)
    public void exportIndustryCategoryExcel(@Valid IndustryCategoryPageReqVO pageReqVO,
                                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IndustryCategoryDO> list = industryCategoryService.getIndustryCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "行业分类.xls", "数据", IndustryCategoryRespVO.class,
                BeanUtils.toBean(list, IndustryCategoryRespVO.class));
    }

}
