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.ProductionSlaughter;
import com.ruoyi.web.base.domain.ProductionWhiteStripAllocation;
import com.ruoyi.web.base.domain.param.WiteStripAllRequest;
import com.ruoyi.web.base.service.IProductionWhiteStripAllocationService;
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-allocation")
public class ProductionWhiteStripAllocationController {

    @Autowired
    private IProductionWhiteStripAllocationService productionWhiteStripAllocationService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("白条分配添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionWhiteStripAllocation allocation, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        allocation.setOrgId(loginOrgId);
        allocation.setCreateBy(getUsername());
        return success(productionWhiteStripAllocationService.save(allocation));
    }

    @ApiOperation("白条分配修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionWhiteStripAllocation allocation, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        allocation.setUpdateBy(getUsername());
        return success(productionWhiteStripAllocationService.updateById(allocation));
    }

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

    @ApiOperation("白条分配列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionWhiteStripAllocationService.list(
                new QueryWrapper<ProductionWhiteStripAllocation>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }
    @ApiOperation("根据saleNum查询")
    @PostMapping("/listBySaleNum")
    public AjaxResult listBySaleNum(@RequestBody WiteStripAllRequest witeStripAllRequest,
                                    HttpServletRequest request
    ) {
        QueryWrapper<ProductionWhiteStripAllocation> wrapper = new QueryWrapper<ProductionWhiteStripAllocation>()
                .eq("org_id", tokenService.getLoginOrgId(request));
        wrapper
                .eq(StringUtils.isNotEmpty(witeStripAllRequest.getMaterialCode()), "material_code", witeStripAllRequest.getMaterialCode())
                .eq(StringUtils.isNotEmpty(witeStripAllRequest.getSaleNum()), "sale_num", witeStripAllRequest.getSaleNum())
                .eq(StringUtils.isNotEmpty(witeStripAllRequest.getProductionDate()), "production_date", witeStripAllRequest.getProductionDate());

        return success(productionWhiteStripAllocationService.list(
                wrapper
        ));
    }
    @ApiOperation("批量修改状态")
    @PostMapping("/editAllStatus")
    public AjaxResult editAllStatus(@RequestBody Map<String, String> paramsMap) throws Exception {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            ProductionWhiteStripAllocation productionWhiteStripAllocation = new ProductionWhiteStripAllocation();
            productionWhiteStripAllocation.setId(Integer.parseInt(id));
            productionWhiteStripAllocation.setCompletionStatus(1);
            productionWhiteStripAllocationService.updateById(productionWhiteStripAllocation);
        }
        return success();
    }

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

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