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.ProductionWhiteStripRegular;
import com.ruoyi.web.base.service.IProductionWhiteStripRegularService;
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.Map;

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

@RestController
@RequestMapping("/production-white-strip-regular")
public class ProductionWhiteStripRegularController {

    @Autowired
    private IProductionWhiteStripRegularService productionWhiteStripRegularService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("白条常规添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionWhiteStripRegular regular, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionWhiteStripRegularService.count(
                new QueryWrapper<ProductionWhiteStripRegular>()
                        .eq("org_id", loginOrgId)
                        .eq("production_record_number", regular.getProductionRecordNumber())
        ) > 0) {
            throw new Exception("该常规编号已存在");
        }
        regular.setOrgId(loginOrgId);
        regular.setCreateBy(getUsername());
        return success(productionWhiteStripRegularService.save(regular));
    }

    @ApiOperation("白条常规修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionWhiteStripRegular regular, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionWhiteStripRegularService.count(
                new QueryWrapper<ProductionWhiteStripRegular>()
                        .ne("id", regular.getId())
                        .eq("org_id", loginOrgId)
                        .eq("production_record_number", regular.getProductionRecordNumber())
        ) > 0) {
            throw new Exception("该常规编号已存在");
        }
        regular.setUpdateBy(getUsername());
        return success(productionWhiteStripRegularService.updateById(regular));
    }

    @ApiOperation("白条常规删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionWhiteStripRegularService.removeById(id);
        }
        return success();
    }

    @ApiOperation("白条常规列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionWhiteStripRegularService.list(
                new QueryWrapper<ProductionWhiteStripRegular>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("白条常规分页")
    @GetMapping("/page")
    public AjaxResult page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            HttpServletRequest request
    ) {
        return success(productionWhiteStripRegularService.page(
                new Page<ProductionWhiteStripRegular>(pageNum, pageSize),
                new QueryWrapper<ProductionWhiteStripRegular>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("白条常规详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionWhiteStripRegularService.getById(id));
    }
}
