package com.tashow.cloud.product.controller.admin;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.product.mapper.ProdPropMapper;
import com.tashow.cloud.product.mapper.ProdPropValueMapper;
import com.tashow.cloud.productapi.api.product.dto.*;
import com.tashow.cloud.product.mapper.SkuMapper;
import com.tashow.cloud.product.service.ProdExtendService;
import com.tashow.cloud.product.service.ProdPropService;
import com.tashow.cloud.product.service.ProdPropValueService;
import com.tashow.cloud.product.service.SkuService;
import com.tashow.cloud.productapi.api.product.vo.prodpropvalue.ProPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodpropvalue.ProPropRecycleBinVO;
import com.tashow.cloud.productapi.api.product.vo.sku.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.tashow.cloud.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 单品SKU")
@RestController
@RequestMapping("/product/sku")
@Validated
public class SkuController {

    @Resource
    private SkuService skuService;

    @Resource
    private ProdPropService prodPropService;
    @Resource
    private ProdPropValueService prodPropValueService;

    @Resource
    private ProdExtendService prodExtendService;


    @Resource
    private ProdPropValueMapper prodPropValueMapper;
    @Resource
    private ProdPropMapper prodPropMapper;

    @Resource
    private SkuMapper skuMapper;

/*    @PostMapping("/create")
    @Operation(summary = "创建单品SKU")
    @PreAuthorize("@ss.hasPermission('tz:sku:create')")
    public CommonResult<Long> createSku(@Valid @RequestBody SkuSaveReqVO createReqVO) {
        return success(skuService.createSku(createReqVO));
    }*/

    @PutMapping("/update")
    @Operation(summary = "更新单品SKU")
    @PermitAll
    public CommonResult<Boolean> updateSku(@RequestBody SkuSaveReqVO updateReqVO) {
        skuService.updateSku(updateReqVO);
        return success(true);
    }


    @PutMapping("/updateProp")
    @Operation(summary = "新增统一保存sku规格")
    @PermitAll
    public CommonResult<Boolean> updateProp(@Valid @RequestBody SkuPropVO skuPropVO) {
        skuService.updateProp(skuPropVO);
        return success(true);
    }

    @PutMapping("/updatePropValue")
    @Operation(summary = "修改属性下面规格值")
    @PermitAll
    public CommonResult<Boolean> updatePropValue(@RequestParam("id") Long id,@RequestParam("propValue") String propValue) {
        skuService.updatePropVal(id,propValue);
        return success(true);
    }

    @PutMapping("/updateProdProp")
    @Operation(summary = "修改属性规格值")
    @PermitAll
    public CommonResult<Boolean> updateProdProp(@RequestParam("id") Long id,@RequestParam("propName") String propName) {
        ProdPropDO prodPropDO  = new ProdPropDO();
        prodPropDO.setId( id);
        prodPropDO.setPropName(propName);
        prodPropMapper.updateById(prodPropDO);
        return success(true);
    }


    @GetMapping("/getSKuPropList")
    @Operation(summary = "获取sku规格")
    @PermitAll
    @Parameter(name = "prodId", description = "商品id", required = true)
    public CommonResult<SkuPropInfoVO> getSKuPropList(@RequestParam("prodId") Long prodId) {
        return success(skuService.getSKuPropList(prodId));
    }


    @PermitAll
    @GetMapping("/getPropRecycleBinList")
    @Operation(summary = "获取规格回收站")
    public CommonResult<PageResult<ProPropRecycleBinVO>> getSKuPropRecycleBinList(ProPageReqVO proPageReqVO) {
        PageResult<ProPropRecycleBinVO> pageResult = skuService.getSKuPropRecycleBinList(proPageReqVO);
        return success(pageResult);
    }

    @PostMapping("/restorePropList")
    @Operation(summary = "恢复规格")
    @Parameter(name = "ids", description = "规格id集合", required = true)
    @PermitAll
    public CommonResult<Boolean> restorePropList(@RequestParam("ids") List<Long> ids) {
        skuService.restorePropList(ids);
        return success(true);
    }


    @PutMapping("/deleteProp")
    @Operation(summary = "删除规格值")
    @PermitAll
    public CommonResult<Boolean> deleteProp(@RequestParam("id") Long id) {
        skuService.deleteProp(id);
        return success(true);
    }


    @PutMapping("/disableProp")
    @Operation(summary = "禁用或者启用规格值")
    @Parameter(name = "id", description = "规格id")
    @Parameter(name = "state", description = "状态0禁用1启用")
    @PermitAll
    public CommonResult<Boolean> disableProp(@RequestParam("id") Long id,@RequestParam("state") Integer state) {
        skuService.disableProp(id,state);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除单品SKU")
    @Parameter(name = "id", description = "编号", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteSku(@RequestParam("id") Long id) {
        skuService.deleteSku(id);
        return success(true);
    }

    @DeleteMapping("/deleteSkuList")
    @Operation(summary = "批量删除SKU")
    @Parameter(name = "ids", description = "编号", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteSkuList(@RequestParam("ids") List<Long> ids) {
        skuService.deleteSkus(ids);
        return success(true);
    }


    @PutMapping("/updateSkuShelf")
    @Operation(summary = "修改单品上下架")
    @Parameter(name = "id", description = "编号", required = true)
    @Parameter(name = "isShelf", description = "是否上下架（0下架 1上架）", required = true)
    @PermitAll
    public CommonResult<Boolean> updateSkuShelf(@RequestParam("id") Long id,@RequestParam("isShelf") Integer isShelf) {
        skuService.updatSkuIsShelf(id,isShelf);
        return success(true);
    }

    @PutMapping("/updateSkuShelfList")
    @Operation(summary = "批量上下架")
    @Parameter(name = "ids", description = "编号", required = true)
    @Parameter(name = "isShelf", description = "是否上下架（0下架 1上架）", required = true)
    @PermitAll
    public CommonResult<Boolean> updateSkuShelfList(@RequestParam("ids") List<Long> ids,@RequestParam("isShelf") Integer isShelf) {
        for(Long id:ids){
            SkuDO sku = new SkuDO();
            sku.setSkuId(id);
            sku.setIsShelf(isShelf);
            skuMapper.updateById(sku);
        }
        skuService.updatSkuIsShelfs(ids,isShelf);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得单品SKU")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<SkuRespVO> getSku(@RequestParam("id") Long id) {
        SkuDO sku = skuService.getSku(id);
        return success(BeanUtils.toBean(sku, SkuRespVO.class));
    }

    @PermitAll
    @GetMapping("/getSkuRecycleBinPageList")
    @Operation(summary = "获得SKU回收站分页列表")
    public CommonResult<PageResult<SkuRecycleBinVO>> getSkuRecycleBinPageList(@Valid SkuPageReqVO pageReqVO) {
        PageResult<SkuRecycleBinVO> pageResult = skuService.getSkuRecycleBinPageList(pageReqVO);
        return success(pageResult);
    }


    @PostMapping("/restoreSkuList")
    @Operation(summary = "恢复SKU")
    @Parameter(name = "ids", description = "skuids", required = true)
    @PermitAll
    public CommonResult<Boolean> restoreSkuList(@RequestParam("ids") List<Long> ids) {
        skuService.restoreSkuList(ids);
        return success(true);
    }



    @PermitAll
    @GetMapping("/getSkuPageList")
    @Operation(summary = "获得SKU分页列表")
    public CommonResult<PageResult<SkuDO>> getSkuPageList(@Valid SkuPageReqVO pageReqVO) {
        PageResult<SkuDO> pageResult = skuService.getSkuPageList(pageReqVO);
        return success(pageResult);
    }



/*  @PermitAll
    @GetMapping("/getSkuRecycleBinPageList")
    @Operation(summary = "获得SKU回收站分页列表")
    public CommonResult<PageResult<SkuRecycleBinVO>> getSkuRecycleBinPageList(@Valid SkuPageReqVO pageReqVO) {
        PageResult<SkuRecycleBinVO> pageResult = skuService.getSkuRecycleBinPageList(pageReqVO);
        return success(pageResult);
    }*/


    @PostMapping("/createSkuExtend")
    @Operation(summary = "创建sku扩展服务配置")
    @PermitAll
    public CommonResult<Boolean> createSkuExtend(@Valid @RequestBody SkuExtendVO skuExtendVO) {
        skuService.createSkuExtend(skuExtendVO);
        return success(true);
    }


    @PostMapping("/getSkuExtend")
    @Operation(summary = "获取sku扩展服务配置信息")
    @Parameter(name = "formId", description = "表单id", required = true, example = "1")
    @PermitAll
    public CommonResult<SkuExtendVO> getSkuExtend(@RequestParam("formId") Integer formId) {
        SkuExtendVO skuExtendVO =skuService.getSkuExtend(formId);
        return success(skuExtendVO);
    }


    @PostMapping("/updateServiceDetails")
    @Operation(summary = "修改扩展服务信息配置（遗体接运扩展服务，遗体清洁配置，追思告别配置，骨灰处理配置......）")
    @PermitAll
    public CommonResult<Boolean> updateServiceDetails(@RequestBody List<SkuServiceDetailsDO> skuServiceDetailsList) {
        skuService.updateServiceDetails(skuServiceDetailsList);
        return success(true);
    }

    @PostMapping("/updateMaterial")
    @Operation(summary = "修物料配置")
    @PermitAll
    public CommonResult<Boolean> updateMaterial(@RequestBody List<SkuServiceMaterialDO> skuServiceMaterialList) {
        skuService.updateMaterial(skuServiceMaterialList);
        return success(true);
    }

    @PostMapping("/updateTransportAdress")
    @Operation(summary = "修改接运地址配置")
    @PermitAll
    public CommonResult<Boolean> updateTransportAdress(@RequestBody List<SkuServiceTransportDO> skuServiceTransportDOList) {
        skuService.updateTransportAdress(skuServiceTransportDOList);
        return success(true);
    }

    @PostMapping("/updateDeliver")
    @Operation(summary = "修改配送方式")
    @PermitAll
    public CommonResult<Boolean> updateDeliver(@RequestBody List<SkuServiceDeliverDO> skuServiceDeliverList) {
        skuService.updateDeliver(skuServiceDeliverList);
        return success(true);
    }


  /*  @GetMapping("/page")
    @Operation(summary = "获得单品SKU分页")
    public CommonResult<PageResult<SkuRespVO>> getSkuPage(@Valid SkuPageReqVO pageReqVO) {
        PageResult<SkuDO> pageResult = skuService.getSkuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuRespVO.class));
    }*/
/*
    @GetMapping("/export-excel")
    @Operation(summary = "导出单品SKU Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuExcel(@Valid SkuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuDO> list = skuService.getSkuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单品SKU.xls", "数据", SkuRespVO.class,
                        BeanUtils.toBean(list, SkuRespVO.class));
    }*/

}