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

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import io.swagger.v3.oas.annotations.Parameters;
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.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.*;

import cn.iocoder.yudao.module.visit.controller.admin.product.vo.*;
import cn.iocoder.yudao.module.visit.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.module.visit.service.product.ProductService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 产品")
@RestController
@RequestMapping("/visit/product")
@Validated
public class ProductController {

    @Resource
    private ProductService productService;


    @PostMapping("/create")
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('visit:product:create')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO) {
        return success(productService.createProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品")
    @PreAuthorize("@ss.hasPermission('visit:product:update')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('visit:product:delete')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Boolean> deleteProduct(@RequestParam("id") Long id) {
        productService.deleteProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('visit:product:query')")
    public CommonResult<ProductRespVO> getProduct(@RequestParam("id") Long id) {
        ProductDO product = productService.getProduct(id);
        return success(BeanUtils.toBean(product, ProductRespVO.class));
    }

    @GetMapping("/getListByIds")
    @Operation(summary = "获得产品")
    @Parameter(name = "ids", description = "编号", required = true, example = "1024,1023")
    @PreAuthorize("@ss.hasPermission('visit:product:query')")
    public CommonResult<List<ProductSimpleReqVO>> getProduct(@RequestParam("ids") String ids) {
        List<ProductSimpleReqVO> productByIds = productService.getProductByIds(ids);
        return success(productByIds);
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分页")
    @PreAuthorize("@ss.hasPermission('visit:product:query')")
    public CommonResult<PageResult<ProductRespVO>> getProductPage(@Valid ProductPageReqVO pageReqVO) {
        PageResult<ProductDO> pageResult = productService.getProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品 Excel")
    @PreAuthorize("@ss.hasPermission('visit:product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductExcel(@Valid ProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductDO> list = productService.getProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品.xls", "数据", ProductRespVO.class,
                        BeanUtils.toBean(list, ProductRespVO.class));
    }



    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入品种资料模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<ProductImportExcelVO> list = Arrays.asList(
                ProductImportExcelVO.builder().productName("阿莫西林胶囊").specification("20x20mg").packageName("盒装")
                       .status(CommonStatusEnum.ENABLE.getStatus()).build(),
                ProductImportExcelVO.builder().productName("阿莫西林胶囊").specification("10x50mg").packageName("袋装")
                       .status(CommonStatusEnum.DISABLE.getStatus()).build()
        );
        // 输出
        ExcelUtils.write(response, "品种资料导入模板.xls", "品种资料列表", ProductImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入品种资料")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('visit:product:import')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<ProductImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<ProductImportExcelVO> list = ExcelUtils.read(file, ProductImportExcelVO.class);
        return success(productService.importProductList(list, updateSupport));
    }

}