package com.storlead.trade.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.trade.dto.SopCreateDTO;
import com.storlead.trade.dto.SopResponseDTO;
import com.storlead.trade.dto.SopUpdateDTO;
import com.storlead.trade.service.SopService;
import com.storlead.framework.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * SOP管理 Controller
 *
 * @author Generated
 */
@Slf4j
@RestController
@RequestMapping("/api/sop")
@Api(tags = "营销: SOP管理")
@Validated
public class SopController {

    @Resource
    private SopService sopService;

    /**
     * 创建SOP（包含步骤明细）
     *
     * @param dto SOP创建请求
     * @return SOP ID
     */
    @PostMapping("/create")
    @ApiOperation(value = "创建SOP", notes = "创建SOP主表及步骤明细")
    public Result<Object> createSop(@Valid @RequestBody SopCreateDTO dto) {
        Result<Object> result = sopService.createSop(dto.getName(), dto.getDetails());
        return result;
    }

    /**
     * 根据ID查询SOP详情（包含步骤明细）
     *
     * @param id SOP ID
     * @return SOP详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询SOP详情", notes = "根据ID查询SOP主表及步骤明细")
    public Result<Object> getSopDetail(
            @ApiParam(value = "SOP ID", required = true)
            @PathVariable Long id) {
        try {
            SopResponseDTO detail = sopService.getSopDetail(id);
            return Result.result(detail);
        } catch (Exception e) {
            log.error("查询SOP详情失败, ID: {}", id, e);
            return Result.error("查询失败: " );
        }
    }

    /**
     * 分页查询SOP列表
     *
     * @RequestBody SopResponseDTO dto
     * @return SOP列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询SOP列表", notes = "支持按名称模糊查询")
    public Result<Object> listSop(@RequestBody SopResponseDTO dto) {
        try {
            IPage<SopResponseDTO> page = sopService.listSopPage(dto);
            return Result.result(page);
        } catch (Exception e) {
            log.error("查询SOP列表失败", e);
            return Result.error("查询失败");
        }
    }

    /**
     * 更新SOP（包含步骤明细）
     *
     * @param id  SOP ID
     * @param dto SOP更新请求
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新SOP", notes = "更新SOP主表及步骤明细（会删除旧明细，重新插入）")
    public Result<Object> updateSop(
            @ApiParam(value = "SOP ID", required = true)
            @PathVariable Long id,
            @Valid @RequestBody SopUpdateDTO dto) {
        try {
            sopService.updateSop(id, dto.getName(), dto.getDetails());
            return Result.ok();
        } catch (Exception e) {
            log.error("更新SOP失败, ID: {}", id, e);
            return Result.error("更新失败: " +e.getMessage());
        }
    }

    /**
     * 删除SOP（级联删除步骤明细）
     *
     * @param id SOP ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除SOP", notes = "删除SOP主表及关联的步骤明细")
    public Result<Object> deleteSop(
            @ApiParam(value = "SOP ID", required = true)
            @PathVariable Long id) {
        try {
            sopService.deleteSop(id);
            return Result.ok();
        } catch (Exception e) {
            log.error("删除SOP失败, ID: {}", id, e);
            return Result.error("删除失败: " +e.getMessage());
        }
    }

    /**
     * 批量删除SOP
     *
     * @param ids SOP ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除SOP", notes = "批量删除SOP主表及关联的步骤明细")
    public Result<Object> batchDeleteSop(
            @ApiParam(value = "SOP ID列表", required = true)
            @RequestBody List<Long> ids) {
        try {
            for (Long id : ids) {
                sopService.deleteSop(id);
            }
            return Result.ok();
        } catch (Exception e) {
            log.error("批量删除SOP失败, IDs: {}", ids, e);
            return  Result.error("批量删除SOP失败: {}", e.getMessage());
        }
    }


}
