package cn.iocoder.yudao.module.museum.controller.admin.museuminfo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.museum.controller.admin.museuminfo.vo.MuseumInfoPageReqVO;
import cn.iocoder.yudao.module.museum.controller.admin.museuminfo.vo.MuseumInfoRespVO;
import cn.iocoder.yudao.module.museum.controller.admin.museuminfo.vo.MuseumInfoSaveVO;
import cn.iocoder.yudao.module.museum.dal.database.museuminfo.MuseumInfoDO;
import cn.iocoder.yudao.module.museum.service.museuminfo.MuseumInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "博物馆-标本库")
@RestController
@RequestMapping("/museum/info")
@Validated
public class MuseumInfoController {

    @Resource
    private MuseumInfoService museumInfoService;

    //TODO 判断标本编号是否重复
    @PostMapping("/create")
    @Operation(summary = "新增标本")
    public CommonResult<Integer> createMuseumInfo(@RequestBody MuseumInfoSaveVO saveVO) {
        String sampleId = saveVO.getSampleId();
        MuseumInfoDO result = museumInfoService.getMuseumInfoBySampleId(sampleId);
        if (result != null) {
            return CommonResult.error(1, "标本编号已存在");
        } else {
            return CommonResult.success(museumInfoService.createMuseumInfo(saveVO));
        }
    }

    @Operation(summary = "更新标本")
    public CommonResult<Boolean> updateMuseumInfo(@RequestBody MuseumInfoSaveVO saveVO){
        museumInfoService.updateMuseumInfo(saveVO);
        return CommonResult.success(true);


    }

    //TODO 需要提供原因，调用更新方法
    @DeleteMapping("/delete")
    @Operation(summary = "删除标本")
    public CommonResult<Boolean> deleteMuseumInfo(@RequestParam("id") Integer id,@RequestParam("reason") String reason){
        museumInfoService.deleteMuseumInfo(id,reason);
//        result.put("删除原因：",);
        return CommonResult.success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "id查找")
    public CommonResult<MuseumInfoRespVO> selectMuseumInfo(@RequestParam("id") Integer id){
        MuseumInfoDO result = museumInfoService.selectMuseumInfo(id);
        MuseumInfoRespVO result1 = BeanUtils.toBean(result,MuseumInfoRespVO.class);
      return CommonResult.success(result1);
    }

    @GetMapping("/selectMuseumInfoList")
    @Operation(summary = "标本类型查找")
    public CommonResult<List<MuseumInfoRespVO>> getMuseumInfoList(@RequestParam("sampleType") Integer sampleType){
        List<MuseumInfoDO> result = museumInfoService.selectMuseumInfoListBySampleType(sampleType);
        List<MuseumInfoRespVO> result1 = BeanUtils.toBean(result,MuseumInfoRespVO.class);
        return CommonResult.success(result1);
    }

    @GetMapping("/page")
    @Operation(summary = "获取分页")
    public PageResult<MuseumInfoDO> getMuseumInfoPage(@Valid MuseumInfoPageReqVO pageReqVO){
        return  museumInfoService.getMuseumInfoPage(pageReqVO);
    }


    //TODO LIST Map
//    TODO     1.2.3.代表哪种标本类型
//            返回某种标本类型的List数组
//            Integer
//    Map<> 键值对
    @GetMapping("/sampleTypeCount")
    @Operation(summary = "标本类型计数")
    public CommonResult<Map<MuseumInfoRespVO,Integer>> getMap(@RequestParam("sampleType") Integer sampleType){
          Map<MuseumInfoRespVO,Integer> result =museumInfoService.MuseumInfoBySampleType(sampleType);
//          标本类型：矿石-1 岩石-2 矿物-3 陨石-4
//          Integer Type = sampleType;
//          result.put(Type);
//        "标本类型:"
          return CommonResult.success(result);
    }

    @GetMapping("/saveCount")
    @Operation(summary = "标本出入库状态计数")
    public CommonResult<Map<String,Integer>> getSave(@RequestParam("save") Integer save){
        Map<String,Integer> result =museumInfoService.MuseumInfoBySave(save);
//        状态：在库-1 出库-0
        Integer Condition = save;
        result.put("状态",Condition);
        return CommonResult.success(result);
    }



//   public CommonResult<MuseumTextRespVO> getdo(@RequestParam("id") Integer id){
//          MuseumTextRespVO DO =new MuseumTextRespVO();
////          Integer Ynmber =  selectMuseumInfoBySampleType(1)
////          Integer  hs =  selectMuseumInfoBySampleType(2)
////          DO.setYnumber(Ynmber);
//   }



}
