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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.productapi.api.product.dto.ProdPropDO;
import com.tashow.cloud.productapi.api.product.dto.ProdPropValueDO;
import com.tashow.cloud.product.service.ProdPropService;
import com.tashow.cloud.product.service.ProdPropValueService;
import com.tashow.cloud.productapi.api.product.vo.prodprop.ProdPropRespVO;
import com.tashow.cloud.productapi.enums.ProdPropRule;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

@Tag(name = "管理后台 - 商品属性")
@RestController
@RequestMapping("/tz/prod-prop")
@Validated
public class ProdPropController {

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

  /*  @PostMapping("/create")
    @Operation(summary = "创建商品属性")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop:create')")
    public CommonResult<Boolean> createProdProp(@Valid @RequestBody ProdPropSaveReqVO createReqVO) {
        prodPropService.saveProdPropAndValues(createReqVO);
        return success(true);
    }*/


    @GetMapping("/getProdPropList")
    @Operation(summary = "获得商品属性列表")
    public CommonResult<List<ProdPropDO>> getProdPropList(@Valid ProdPropRespVO pageReqVO) {
        LambdaQueryWrapper<ProdPropDO> wrapper = new LambdaQueryWrapper<>();
        // 处理规格名称模糊查询
        if (StrUtil.isNotBlank(pageReqVO.getPropName())) {
            wrapper.like(ProdPropDO::getPropName, pageReqVO.getPropName());
        }
        wrapper.eq(ProdPropDO::getRule, ProdPropRule.SPEC.value());
        //TODO 获取当前登录用户
        wrapper.eq(ProdPropDO::getShopId, 1L);
        /*// 获取当前登录用户
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);

        if (sysUser.getShopId() != 100) {
            wrapper.eq(ProdProp::getShopId, sysUser.getShopId());
        }*/
        List<ProdPropDO> list = prodPropService.list(wrapper);
        list.forEach(prop -> {
            List<ProdPropValueDO> values = prodPropValueService.list(
                    new LambdaQueryWrapper<ProdPropValueDO>()
                            .eq(ProdPropValueDO::getPropId, prop.getPropId())
            );
            prop.setProdPropValues(values);
        });
        return success(list);
    }

/*    @PutMapping("/update")
    @Operation(summary = "更新商品属性")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop:update')")
    public CommonResult<Boolean> updateProdProp(@Valid @RequestBody ProdPropSaveReqVO updateReqVO) {
        prodPropService.updateProdProp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-prop:delete')")
    public CommonResult<Boolean> deleteProdProp(@RequestParam("id") Long id) {
        prodPropService.deleteProdProp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop:query')")
    public CommonResult<ProdPropRespVO> getProdProp(@RequestParam("id") Long id) {
        ProdPropDO prodProp = prodPropService.getProdProp(id);
        return success(BeanUtils.toBean(prodProp, ProdPropRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品属性分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop:query')")
    public CommonResult<PageResult<ProdPropRespVO>> getProdPropPage(@Valid ProdPropPageReqVO pageReqVO) {
        PageResult<ProdPropDO> pageResult = prodPropService.getProdPropPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdPropRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品属性 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdPropExcel(@Valid ProdPropPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdPropDO> list = prodPropService.getProdPropPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品属性.xls", "数据", ProdPropRespVO.class,
                        BeanUtils.toBean(list, ProdPropRespVO.class));
    }*/

}