package com.ruoyi.web.v2.v1.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.v2.v1.model.JsDrugCheck;
import com.ruoyi.web.v2.v1.service.IJsDrugCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * <p>
 * v2.0违禁药物登记 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-22
 */
@RestController
@RequestMapping("/js-drug-check")
@Api("2.0违禁药物登记")
public class JsDrugCheckController {

    @Autowired
    private IJsDrugCheckService drugCheckService;


    @ApiOperation("违禁药物登记添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody JsDrugCheck drugCheck) {
        String allResult = "合格";
        for (HashMap<String, String> result:drugCheck.getExamineList()){
            for (Map.Entry<String, String> entry : result.entrySet()){
                if (Objects.equals(entry.getKey(), "value") && (!Objects.equals(entry.getValue(), "阴性") && !Objects.equals(entry.getValue(), "合格"))) {
                    allResult = "不合格";
                    break;
                }
            }
        }
        drugCheck.setResult(allResult);
        return drugCheckService.add(drugCheck);
    }

    @ApiOperation("违禁药物登记修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody JsDrugCheck drugCheck) {
        String allResult = "合格";
        for (HashMap<String, String> result:drugCheck.getExamineList()){
            for (Map.Entry<String, String> entry : result.entrySet()){
                if (Objects.equals(entry.getKey(), "value") && (!Objects.equals(entry.getValue(), "阴性") && !Objects.equals(entry.getValue(), "合格"))) {
                    allResult = "不合格";
                    break;
                }
            }
        }
        drugCheck.setResult(allResult);
        return drugCheckService.edit(drugCheck);
    }

    @ApiOperation("违禁药物登记删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        return drugCheckService.delete(ids);
    }

    @ApiOperation("违禁药物登记列表")
    @PostMapping("/list")
    public AjaxResult listAll(@RequestBody Map<String, String> param) {
        if(!ObjectUtils.isEmpty(param) && !ObjectUtils.isEmpty(param.getOrDefault("checkType", null))) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("check_type", param.get("checkType"));
            return success(drugCheckService.list(wrapper));
        }
        return drugCheckService.listAll();
    }

    @ApiOperation("违禁药物登记分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startTime", required = false) String startTime
            , @RequestParam(value = "endTime", required = false) String endTime
            , @RequestParam(value = "animalCertNo", required = false) String animalCertNo
            , @RequestParam(value = "checkType", required = false) String checkType) {

        return drugCheckService.page(pageNum, pageSize, startTime, endTime, animalCertNo, checkType);
    }

    @ApiOperation("违禁药物登记详情")
    @GetMapping("/listById")
    public AjaxResult listById(JsDrugCheck drugCheck) {
        return success(drugCheckService.getDetail(drugCheck));
    }

    @ApiOperation("违禁药物检测及屠宰检验情况登记表")
    @Anonymous
    @GetMapping("/drugAndProduce")
    public AjaxResult drugAndProduce(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startTime", required = false) String startTime
            , @RequestParam(value = "endTime", required = false) String endTime
            , @RequestParam(value = "cargoOwner", required = false) String cargoOwner) {
        return success(drugCheckService.drugAndProduce(pageNum, pageSize,startTime,endTime,cargoOwner));
    }

    @ApiOperation("无害化处理登记表")
    @Anonymous
    @GetMapping("/harmlessTreatment")
    public AjaxResult harmlessTreatment(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startTime", required = false) String startTime
            , @RequestParam(value = "endTime", required = false) String endTime
            , @RequestParam(value = "cargoOwner", required = false) String cargoOwner) {
        return success(drugCheckService.harmlessTreatment(pageNum, pageSize,startTime,endTime,cargoOwner));
    }

        @ApiOperation("生猪入场查验情况登记表")
    @Anonymous
    @GetMapping("/Admission")
    public AjaxResult Admission(@RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize
                , @RequestParam(value = "startTime", required = false) String startTime
                , @RequestParam(value = "endTime", required = false) String endTime
                , @RequestParam(value = "animalCertNo", required = false) String animalCertNo
                , @RequestParam(value = "cargoOwner", required = false) String cargoOwner) {
        return success(drugCheckService.Admission(pageNum, pageSize,startTime,endTime,animalCertNo,cargoOwner));
    }


    @ApiOperation("生猪产品出场情况登记表")
    @Anonymous
    @GetMapping("/outPlace")
    public AjaxResult outPlace(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startTime", required = false) String startTime
            , @RequestParam(value = "endTime", required = false) String endTime
            , @RequestParam(value = "cargoOwner", required = false) String cargoOwner) {
        return success(drugCheckService.outPlace(pageNum, pageSize,startTime,endTime,cargoOwner));
    }

    @ApiOperation("生猪屠宰情况日汇总等级表")
    @Anonymous
    @GetMapping("/SummaryVo")
    public AjaxResult SummaryVo(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startTime", required = false) String startTime
            , @RequestParam(value = "endTime", required = false) String endTime) {
        return success(drugCheckService.SummaryVo(pageNum, pageSize, startTime, endTime));
    }

}
