package net.ferrum.web.controller.business;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import net.ferrum.business.domain.QsBatch;
import net.ferrum.business.domain.QsSpecAttributeTypes;
import net.ferrum.business.domain.QsSpecAttributeValues;
import net.ferrum.business.service.IQsSpecAttributeTypesService;
import net.ferrum.common.core.domain.entity.SysDept;
import net.ferrum.common.core.domain.entity.SysUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import net.ferrum.common.annotation.RepeatSubmit;
import net.ferrum.common.annotation.Log;
import net.ferrum.common.core.controller.BaseController;
import net.ferrum.common.core.domain.PageQuery;
import net.ferrum.common.core.domain.R;
import net.ferrum.common.core.validate.AddGroup;
import net.ferrum.common.core.validate.EditGroup;
import net.ferrum.common.core.validate.QueryGroup;
import net.ferrum.common.enums.BusinessType;
import net.ferrum.common.utils.poi.ExcelUtil;
import net.ferrum.business.domain.vo.QsSpecAttributeValuesVo;
import net.ferrum.business.domain.bo.QsSpecAttributeValuesBo;
import net.ferrum.business.service.IQsSpecAttributeValuesService;
import net.ferrum.common.core.page.TableDataInfo;

/**
 * 规格参数值
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/specAttributeValues")
public class QsSpecAttributeValuesController extends BaseController {

    private final IQsSpecAttributeValuesService iQsSpecAttributeValuesService;
    private final IQsSpecAttributeTypesService iQsSpecAttributeTypesService;

    /**
     * 查询规格参数值列表
     */
    @SaCheckPermission("business:specAttributeValues:list")
    @GetMapping("/list")
    public TableDataInfo<QsSpecAttributeValues> list(QsSpecAttributeValues specAttributeValues, PageQuery pageQuery) {
        return iQsSpecAttributeValuesService.selectPageSpecAttributeValuesList(specAttributeValues, pageQuery);
    }

    /**
     * 导出规格参数值列表
     */
    @SaCheckPermission("business:specAttributeValues:export")
    @Log(title = "规格参数值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QsSpecAttributeValuesBo bo, HttpServletResponse response) {
        List<QsSpecAttributeValuesVo> list = iQsSpecAttributeValuesService.queryList(bo);
        ExcelUtil.exportExcel(list, "规格参数值", QsSpecAttributeValuesVo.class, response);
    }

    /**
     * 获取规格参数值详细信息
     *
     * @param valueId 主键
     */
    @SaCheckPermission("business:specAttributeValues:query")
    @GetMapping("/{valueId}")
    public R<QsSpecAttributeValuesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long valueId) {
        return R.ok(iQsSpecAttributeValuesService.queryById(valueId));
    }

    /**
     * 新增规格参数值
     */
    @SaCheckPermission("business:specAttributeValues:add")
    @Log(title = "规格参数值", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QsSpecAttributeValuesBo bo) {
        return toAjax(iQsSpecAttributeValuesService.insertByBo(bo));
    }

    /**
     * 修改规格参数值
     */
    @SaCheckPermission("business:specAttributeValues:edit")
    @Log(title = "规格参数值", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QsSpecAttributeValuesBo bo) {
        return toAjax(iQsSpecAttributeValuesService.updateByBo(bo));
    }

    /**
     * 删除规格参数值
     *
     * @param valueIds 主键串
     */
    @SaCheckPermission("business:specAttributeValues:remove")
    @Log(title = "规格参数值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{valueIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] valueIds) {
        return toAjax(iQsSpecAttributeValuesService.deleteWithValidByIds(Arrays.asList(valueIds), true));
    }

    /**
     * 获取规格参数类型列表
     */
    @SaCheckPermission("business:specAttributeValues:list")
    @GetMapping("/specAttributeTypeList")
    public R< List<QsSpecAttributeTypes>> selectSpecAttributeTypesList(QsSpecAttributeTypes type) {
        return R.ok(iQsSpecAttributeTypesService.selectSpecAttributeTypesList(type));
    }

    /**
     * 根据规格参数类型，获取这类参数下面的所有规格参数
     */
    @SaCheckPermission("business:specAttributeValues:list")
    @GetMapping("/specAttributeValueListByTypeId")
    public R< List<QsSpecAttributeValues>> selectSpecAttributeValuesByTypesId(Long typeId) {
        return R.ok(iQsSpecAttributeValuesService.selectSpecAttributeValuesByTypesId(typeId));
    }

    /**
     * 得到所有据规格参数类型，每个参数类型包含本类中的所有规格参数
     */
    @SaCheckPermission("business:specAttributeValues:list")
    @GetMapping("/specAttributeTypesWithValues")
    public R< List<QsSpecAttributeTypes>> selectSpecAttributeTypesWithValues(){
        List<QsSpecAttributeTypes> typeList = iQsSpecAttributeTypesService.selectSpecAttributeTypesList(null);
        for(QsSpecAttributeTypes type : typeList){
            type.setSpecValues(iQsSpecAttributeValuesService.selectSpecAttributeValuesByTypesId(type.getTypeId()));
        }
        return R.ok(typeList);
    }
}
