package com.ruoyi.web.base.controller;

import cn.hutool.core.bean.BeanUtil;
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.ProductionProduct;
import com.ruoyi.web.base.domain.ProductionRecord;
import com.ruoyi.web.base.domain.param.ProductionProductRequest;
import com.ruoyi.web.base.domain.param.ProductionRegularPlanRequest;
import com.ruoyi.web.base.service.IProductionProductService;
import com.ruoyi.web.base.service.IProductionRecordService;
import io.swagger.annotations.Api;
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-product")
@Api("分割品副产品")
public class ProductionProductController {

    @Autowired
    private IProductionProductService productionProductService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IProductionRecordService productionRecordService;

    @ApiOperation("分割品副产品添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionProduct product, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionProductService.count(
                new QueryWrapper<ProductionProduct>()
                        .eq("org_id", loginOrgId)
                        .eq("production_record_number", product.getProductionRecordNumber())
        ) > 0) {
            throw new Exception("该产品编号已存在");
        }
        product.setOrgId(loginOrgId);
        product.setCreateBy(getUsername());


        ProductionRecord productionRecord = new ProductionRecord();
        BeanUtil.copyProperties(product, productionRecord);
        productionRecordService.save(productionRecord);
        return success(productionProductService.save(product));
    }

    @ApiOperation("分割品副产品修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionProduct product, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionProductService.count(
                new QueryWrapper<ProductionProduct>()
                        .ne("id", product.getId())
                        .eq("org_id", loginOrgId)
                        .eq("production_record_number", product.getProductionRecordNumber())
        ) > 0) {
            throw new Exception("该产品编号已存在");
        }
        product.setUpdateBy(getUsername());
        return success(productionProductService.updateById(product));
    }

    @ApiOperation("分割品副产品删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionProductService.removeById(id);
        }
        return success();
    }

    @ApiOperation("分割品副产品列表")
    @PostMapping("/list")
    public AjaxResult listAll(   @RequestBody ProductionProductRequest planRequest,HttpServletRequest request) {
        QueryWrapper<ProductionProduct> wrapper = new QueryWrapper<ProductionProduct>()
                .eq("org_id", tokenService.getLoginOrgId(request));
        wrapper.eq(StringUtils.isNotEmpty(planRequest.getProductionDate()),"production_date",planRequest.getProductionDate());
        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionPlanNumber()),"production_plan_number",planRequest.getProductionPlanNumber());
        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionRecordNumber()),"production_record_number",planRequest.getProductionRecordNumber());
        wrapper.eq(StringUtils.isNotEmpty(planRequest.getProductionType()),"production_date",planRequest.getProductionDate());
        return success(productionProductService.list(
                wrapper
        ));
    }

    @ApiOperation("分割品副产品分页")
    @GetMapping("/page")
    public AjaxResult page(
            @RequestBody ProductionProductRequest planRequest,
            HttpServletRequest request
    ) {
        QueryWrapper<ProductionProduct> wrapper = new QueryWrapper<ProductionProduct>()
                .eq("org_id", tokenService.getLoginOrgId(request));
        wrapper.eq(StringUtils.isNotEmpty(planRequest.getProductionDate()),"production_date",planRequest.getProductionDate());
        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionPlanNumber()),"production_plan_number",planRequest.getProductionPlanNumber());
        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionRecordNumber()),"production_record_number",planRequest.getProductionRecordNumber());
        wrapper.eq(StringUtils.isNotEmpty(planRequest.getProductionType()),"production_date",planRequest.getProductionDate());
        return success(productionProductService.page(
                new Page<ProductionProduct>(planRequest.getPageNum(), planRequest.getPageSize()),
                wrapper
        ));
    }

    @ApiOperation("分割品副产品详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionProductService.getById(id));
    }
}