package com.nailart.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nailart.dataobj.SampleCollectionDO;
import com.nailart.enums.RespCodeEnum;
import com.nailart.service.SampleCollectionService;
import com.nailart.vo.RespVO;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname SampleCollectionController
 * @Description 样品集控制类
 * @Date 2025/7/10 18:48
 * @Created by 21616
 */
@RestController
@RequestMapping("/sampleCollection")
public class SampleCollectionController {
    private final SampleCollectionService SampleCollectionService;

    public SampleCollectionController(SampleCollectionService SampleCollectionService) {
        this.SampleCollectionService = SampleCollectionService;
    }

    /**
     * 获取样品集列表
     *
     * @param page 页码
     * @param size 每页数量
     * @return RespVO
     */
    @GetMapping("/list")
    public RespVO getSampleCollectionList(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        RespVO respVO = new RespVO();
        if (page <= 0 || size <= 0) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            IPage<SampleCollectionDO> SampleCollectionDOIPage = SampleCollectionService.listByPage(page, size);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(SampleCollectionDOIPage);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;

    }

    /**
     * 添加样品集
     *
     * @param SampleCollectionDO 样品集信息
     * @return RespVO
     */
    @PostMapping("/add")
    public RespVO addSampleCollection(@RequestBody SampleCollectionDO SampleCollectionDO) {
        RespVO respVO = new RespVO();
        if (SampleCollectionDO == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = SampleCollectionService.addSampleCollection(SampleCollectionDO);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;


    }

    /**
     * 更新样品集
     *
     * @param SampleCollectionDO 样品集信息
     * @return RespVO
     */
    @PostMapping("/update")
    public RespVO updateSampleCollection(@RequestBody SampleCollectionDO SampleCollectionDO) {
        RespVO respVO = new RespVO();
        if (SampleCollectionDO == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = SampleCollectionService.updateSampleCollection(SampleCollectionDO);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }

    /**
     * 删除样品集
     *
     * @param id 样品集id
     * @return RespVO
     */
    @PostMapping("/delete")
    public RespVO deleteSampleCollection(@RequestParam("id") Integer id) {
        RespVO respVO = new RespVO();
        if (id == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = SampleCollectionService.deleteSampleCollection(id);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }

    @GetMapping("/getSampleCollectionByTag")
    public RespVO getSampleCollectionByTag(@RequestParam("tagID") Long tagID,
                                    @RequestParam("page") Integer page,
                                    @RequestParam("size") Integer size) {
        RespVO respVO = new RespVO();
        if (tagID == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            IPage<SampleCollectionDO> SampleCollectionByTag = SampleCollectionService.getSampleCollectionByTag(tagID, page, size);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(SampleCollectionByTag);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }


}
