package com.yc.ship.module.resource.controller.admin.supplier;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.supplier.vo.*;
import com.yc.ship.module.resource.dal.dataobject.supplier.ProductSupplierDO;
import com.yc.ship.module.resource.enums.UseStatusEnum;
import com.yc.ship.module.resource.service.supplier.ProductSupplierService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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 org.springframework.web.multipart.MultipartFile;

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

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 资源供应商")
@RestController
@RequestMapping("/resource/supplier")
@Validated
public class ProductSupplierController {

    @Resource
    private ProductSupplierService productSupplierService;

    @PostMapping("/create")
    @Operation(summary = "创建资源供应商")
    @PreAuthorize("@ss.hasPermission('product:supplier:create')")
    public CommonResult<Long> createSupplier(@Valid @RequestBody SupplierSaveReqVO createReqVO) {
        return success(productSupplierService.createSupplier(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源供应商")
    @PreAuthorize("@ss.hasPermission('product:supplier:update')")
    public CommonResult<Boolean> updateSupplier(@Valid @RequestBody SupplierSaveReqVO updateReqVO) {
        productSupplierService.updateSupplier(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源供应商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:supplier:delete')")
    public CommonResult<Boolean> deleteSupplier(@RequestParam("id") Long id) {
        productSupplierService.deleteSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:supplier:query')")
    public CommonResult<SupplierRespVO> getSupplier(@RequestParam("id") Long id) {
        SupplierRespVO supplier = productSupplierService.getSupplierById(id);
        return success(supplier);
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源供应商分页")
    @PreAuthorize("@ss.hasPermission('product:supplier:query')")
    public CommonResult<PageResult<SupplierRespVO>> getSupplierPage(@Valid SupplierPageReqVO pageReqVO) {
        PageResult<SupplierRespVO> pageResult = productSupplierService.getSupplierPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源供应商 Excel")
    @PreAuthorize("@ss.hasPermission('product:supplier:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportSupplierExcel(@Valid SupplierPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierRespVO> list = productSupplierService.getSupplierPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源供应商.xls", "数据", SupplierRespVO.class,
                BeanUtils.toBean(list, SupplierRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改用户状态")
    @PreAuthorize("@ss.hasPermission('product:supplier:update')")
    public CommonResult<Boolean> updateUserStatus(@Valid @RequestBody SupplierUpdateStatusReqVO reqVO) {
        productSupplierService.updateUserStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入用户模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<SupplierImportExcelVO> list = Arrays.asList(
                SupplierImportExcelVO.builder().name("测试供应商").type("外部供应商").cooperateStartTime("2023-12-01").cooperateEndTime("2025-11-11")
                        .contactPhone("13112345563").contactMan("张三").contactAddress("xx省xx市").build(),
                SupplierImportExcelVO.builder().name("测试供应商-2").type("内部供应商").cooperateStartTime("2023-12-01").cooperateEndTime("2025-11-11")
                        .contactPhone("13112345563").contactMan("李四").contactAddress("xx省xx市").build()
        );
        // 输出
        ExcelUtils.write(response, "供应商导入模板.xls", "供应商信息", SupplierImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入供应商")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('resource:supplier:import')")
    public CommonResult<SupplierImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                          @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<SupplierImportExcelVO> list = ExcelUtils.read(file, SupplierImportExcelVO.class);
        return success(productSupplierService.importSupplierList(list, updateSupport));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取资源类型精简信息列表", description = "只包含被开启的类型，主要用于前端的下拉选项")
    public CommonResult<List<SupplierSimpleRespVO>> getSimpleList(SupplierListReqVO reqVO) {

        reqVO.setUseStatus(UseStatusEnum.ENABLE.getStatus());
        List<ProductSupplierDO> list= productSupplierService.getSimpleListByCategory(reqVO);

        return success(BeanUtils.toBean(list, SupplierSimpleRespVO.class));
    }

    @GetMapping({"/list-by-type"})
    @Operation(summary = "获取资源类型精简信息列表", description = "只包含被开启的类型，主要用于前端的下拉选项")
    public CommonResult<List<SupplierSimpleRespVO>> getSimpleListByType(SupplierListReqVO reqVO) {

        reqVO.setUseStatus(UseStatusEnum.ENABLE.getStatus());
        List<ProductSupplierDO> list= productSupplierService.getSimpleListByType(reqVO);

        return success(BeanUtils.toBean(list, SupplierSimpleRespVO.class));
    }

}