package cn.iocoder.yudao.module.supplier.controller.admin.product;

import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CoreProductPageReqVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CoreProductRespVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CoreProductSaveReqVO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.CoreProductDO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.module.supplier.service.product.CoreProductService;
import cn.iocoder.yudao.module.supplier.service.product.ProductServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 供应商核心产品")
@RestController
@RequestMapping("/supplier/core-product")
@Validated
public class CoreProductController {

    @Resource
    private CoreProductService coreProductService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商核心产品")
    @PreAuthorize("@ss.hasPermission('supplier:core-product:create')")
    public CommonResult<Long> createCoreProduct(@Valid @RequestBody CoreProductSaveReqVO createReqVO) {
        return success(coreProductService.createCoreProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商核心产品")
    @PreAuthorize("@ss.hasPermission('supplier:core-product:update')")
    public CommonResult<Boolean> updateCoreProduct(@Valid @RequestBody CoreProductSaveReqVO updateReqVO) {
        coreProductService.updateCoreProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商核心产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('supplier:core-product:delete')")
    public CommonResult<Boolean> deleteCoreProduct(@RequestParam("id") Long id) {
        coreProductService.deleteCoreProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商核心产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:core-product:query')")
    public CommonResult<CoreProductRespVO> getCoreProduct(@RequestParam("id") Long id) {
        CoreProductDO coreProduct = coreProductService.getCoreProduct(id);
        return success(BeanUtils.toBean(coreProduct, CoreProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商核心产品分页")
    @PreAuthorize("@ss.hasPermission('supplier:core-product:query')")
    public CommonResult<PageResult<CoreProductRespVO>> getCoreProductPage(@Valid CoreProductPageReqVO pageReqVO) {
        PageResult<CoreProductDO> pageResult = coreProductService.getCoreProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CoreProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商核心产品 Excel")
    @PreAuthorize("@ss.hasPermission('supplier:core-product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoreProductExcel(@Valid CoreProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoreProductDO> list = coreProductService.getCoreProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商核心产品.xls", "数据", CoreProductRespVO.class,
                        BeanUtils.toBean(list, CoreProductRespVO.class));
    }


    @Resource
    private ProductServiceImpl productService;
    @GetMapping("/init")
    @PermitAll
    public void productAll() {
        List<ProductDO> all = productService.getAll();
        List<CoreProductDO> allCoreProduct = coreProductService.getAllCoreProduct();
        allCoreProduct.forEach(productDO -> {
            if (StringUtils.isNotBlank(productDO.getProductName())){
                ProductDO productDO1 = all.stream().filter(e -> e.getName().equals(productDO.getProductName())).findFirst().orElseGet(null);
                if (Objects.nonNull(productDO1)) {
                    productDO.setProductId(productDO1.getId());
                    CoreProductSaveReqVO reqVO = new CoreProductSaveReqVO();
                    BeanUtils.copyProperties(productDO, reqVO);
                    coreProductService.updateCoreProduct(reqVO);
                }
            }
        });
    }
}
