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.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProductionRegular;
import com.ruoyi.web.base.domain.ProductionWhiteStripRefined;
import com.ruoyi.web.base.domain.param.ProductionRegularPlanRequest;
import com.ruoyi.web.base.domain.param.WiteStripRequest;
import com.ruoyi.web.base.service.IProductionWhiteStripRefinedService;
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-refined")
public class ProductionWhiteStripRefinedController {

    @Autowired
    private IProductionWhiteStripRefinedService productionWhiteStripRefinedService;
    @Autowired
    private TokenService tokenService;




    @ApiOperation("白条精加工添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionWhiteStripRefined refined, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionWhiteStripRefinedService.count(
                new QueryWrapper<ProductionWhiteStripRefined>()
                        .eq("org_id", loginOrgId)
                        .eq("production_plan_number", refined.getProductionPlanNumber())
                        .eq("white_strip_number", refined.getWhiteStripNumber())
        ) > 0) {
            throw new Exception("该精加工编号已存在");
        }
        refined.setOrgId(loginOrgId);
        refined.setCreateBy(getUsername());
        return success(productionWhiteStripRefinedService.save(refined));
    }

    @ApiOperation("白条精加工修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionWhiteStripRefined refined, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionWhiteStripRefinedService.count(
                new QueryWrapper<ProductionWhiteStripRefined>()
                        .ne("id", refined.getId())
                        .eq("org_id", loginOrgId)

                        .eq("production_plan_number", refined.getProductionPlanNumber())
                        .eq("white_strip_number", refined.getWhiteStripNumber())
        ) > 0) {
            throw new Exception("该精加工编号已存在");
        }
        refined.setUpdateBy(getUsername());
        return success(productionWhiteStripRefinedService.updateById(refined));
    }

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

    @ApiOperation("白条精加工列表")
    @PostMapping("/list")
    public AjaxResult listAll(@RequestBody WiteStripRequest planRequest, HttpServletRequest request) {
        QueryWrapper<ProductionWhiteStripRefined> wrapper = new QueryWrapper<ProductionWhiteStripRefined>()
                .eq("org_id", tokenService.getLoginOrgId(request));
        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionPlanNumber()),"production_plan_number",planRequest.getProductionPlanNumber())
                .like(StringUtils.isNotEmpty(planRequest.getProductionRecordNumber()),"production_record_number",planRequest.getProductionRecordNumber());
        return success(productionWhiteStripRefinedService.list(
                wrapper
        ));
    }



    @ApiOperation("白条精加工分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody WiteStripRequest planRequest,
            HttpServletRequest request

    ) {
        QueryWrapper<ProductionWhiteStripRefined> wrapper = new QueryWrapper<ProductionWhiteStripRefined>()
                .eq("org_id", tokenService.getLoginOrgId(request));
        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionPlanNumber()),"production_plan_number",planRequest.getProductionPlanNumber())
                .like(StringUtils.isNotEmpty(planRequest.getProductionRecordNumber()),"production_record_number",planRequest.getProductionRecordNumber());
        return success(productionWhiteStripRefinedService.page(
                new Page<ProductionWhiteStripRefined>(planRequest.getPageNum(), planRequest.getPageSize()),
                wrapper
        ));
    }

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