package com.huimv.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.WriteFeeding;
import com.huimv.admin.service.IWriteFeedingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/write-feeding")
@CrossOrigin
public class WriteFeedingController {
    @Autowired
    private IWriteFeedingService writeFeedingService;

    @PostMapping("/list")
    public Result list(@RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String eartag = paramsMap.get("eartag");
        List<WriteFeeding> list = writeFeedingService.list(new QueryWrapper<WriteFeeding>().eq("farm_id", farmId)
                .eq(StringUtils.isNotBlank(eartag), "eartag", eartag).orderByAsc("day_num"));
        return new Result(ResultCode.SUCCESS,list);
    }

    @PostMapping("/add")
    public Result addFeeding(@RequestBody WriteFeeding writeFeeding) {
        writeFeeding.setCreateTime(new Date());
        writeFeedingService.save(writeFeeding);
        return new Result(10000,"添加成功",true);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody WriteFeeding writeFeeding) {
        writeFeedingService.updateById(writeFeeding);
        return new Result(10000,"修改成功",true);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        for (String s : split) {
            writeFeedingService.removeById(s);
        }
        return new Result(10000, "删除成功", true);
    }
}
