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

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.productcategory.vo.ProductCategoryPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.productcategory.vo.ProductCategoryRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.productcategory.vo.ProductCategorySaveReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.productcategoryproperty.vo.ProductCategoryPropertyRespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.productcategory.ProductCategoryDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.productcategoryproperty.ProductCategoryPropertyDO;
import cn.iocoder.yudao.module.wms.service.productcategory.ProductCategoryService;
import cn.iocoder.yudao.module.wms.service.productcategoryproperty.ProductCategoryPropertyService;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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/product-category")
@Validated
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private ProductCategoryPropertyService productCategoryPropertyService;

    @PostMapping("/create")
    @Operation(summary = "创建产品分类")
    @PreAuthorize("@ss.hasPermission('wms:product-category:create')")
    public CommonResult<Long> createProductCategory(@Valid @RequestBody ProductCategorySaveReqVO createReqVO) {
        return success(productCategoryService.createProductCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品分类")
    @PreAuthorize("@ss.hasPermission('wms:product-category:update')")
    public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody ProductCategorySaveReqVO updateReqVO) {
        productCategoryService.updateProductCategory(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得产品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:product-category:query')")
    public CommonResult<ProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
        ProductCategoryDO productCategory = productCategoryService.getProductCategory(id);
        ProductCategoryRespVO vo = BeanUtils.toBean(productCategory, ProductCategoryRespVO.class);
        List<ProductCategoryPropertyDO> productCategoryPropertyDOS = productCategoryPropertyService.getByProductCategoryId(id);
        List<ProductCategoryPropertyRespVO> propertyRespVOS = BeanUtils.toBean(productCategoryPropertyDOS, ProductCategoryPropertyRespVO.class);
        vo.setItems(propertyRespVOS);
        return success(vo);
    }


    @GetMapping("/getByIndustryCategoryId")
    @Operation(summary = "根据行业分类id获得产品分类列表")
    @Parameter(name = "industryCategoryId", description = "行业分类id", required = true, example = "1024")
    public CommonResult<List<ProductCategoryRespVO>> getByIndustryCategoryId(@RequestParam("industryCategoryId") Long industryCategoryId) {
        List<ProductCategoryDO> productCategoryDOList = productCategoryService.getByIndustryCategoryId(industryCategoryId);
        List<ProductCategoryRespVO> vos = BeanUtils.toBean(productCategoryDOList, ProductCategoryRespVO.class);
        List<Long> productCategoryIds = new ArrayList<>();
        if (!vos.isEmpty()) {
            productCategoryIds = vos.stream().map(ProductCategoryRespVO::getId).collect(Collectors.toList());
        }
        List<ProductCategoryPropertyDO> productCategoryPropertyDOS = productCategoryPropertyService.getByProductCategoryIds(productCategoryIds);
        if (!productCategoryPropertyDOS.isEmpty()) {
            Map<Long, List<ProductCategoryPropertyDO>> map = productCategoryPropertyDOS.stream().collect(Collectors.groupingBy(ProductCategoryPropertyDO::getProductCategoryId, Collectors.toList()));
            for (ProductCategoryRespVO vo : vos) {
                List<ProductCategoryPropertyDO> items = map.get(vo.getId());
                List<ProductCategoryPropertyRespVO> propertyRespVOS = BeanUtils.toBean(items, ProductCategoryPropertyRespVO.class);
                vo.setItems(propertyRespVOS);
            }
        }
        return success(vos);
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分类分页")
    @PreAuthorize("@ss.hasPermission('wms:product-category:query')")
    public CommonResult<PageResult<ProductCategoryRespVO>> getProductCategoryPage(@Valid ProductCategoryPageReqVO pageReqVO) {
        PageResult<ProductCategoryDO> pageResult = productCategoryService.getProductCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品分类 Excel")
    @PreAuthorize("@ss.hasPermission('wms:product-category:export')")
    @OperateLog(type = EXPORT)
    public void exportProductCategoryExcel(@Valid ProductCategoryPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductCategoryDO> list = productCategoryService.getProductCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品分类.xls", "数据", ProductCategoryRespVO.class,
                BeanUtils.toBean(list, ProductCategoryRespVO.class));
    }

}
