package com.ruoyi.web.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProductionSlaughter;
import com.ruoyi.web.base.domain.param.WiteStripRequest;
import com.ruoyi.web.base.service.IProductionSlaughterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@RestController
@RequestMapping("/production-slaughter")
public class ProductionSlaughterController {

    @Autowired
    private IProductionSlaughterService productionSlaughterService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("生产屠宰添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionSlaughter slaughter, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        slaughter.setOrgId(loginOrgId);
        slaughter.setCreateBy(getUsername());
        slaughter.setCreateTime(new Date());

        return success(productionSlaughterService.save(slaughter));
    }

    @ApiOperation("生产屠宰修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionSlaughter slaughter, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        slaughter.setUpdateBy(getUsername());
        slaughter.setUpdateTime(new Date());
        return success(productionSlaughterService.updateById(slaughter));
    }

    @ApiOperation("生产屠宰删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionSlaughterService.removeById(id);
        }
        return success();
    }

    @ApiOperation("生产屠宰列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionSlaughterService.list(
                new QueryWrapper<ProductionSlaughter>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("生产屠宰分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody WiteStripRequest planRequest,
            HttpServletRequest request
    ) {
        return success(productionSlaughterService.page(
                new Page<ProductionSlaughter>(planRequest.getPageNum(), planRequest.getPageSize()),
                new QueryWrapper<ProductionSlaughter>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("生产屠宰详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionSlaughterService.getById(id));
    }
}
