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.ProductionRecord;
import com.ruoyi.web.base.domain.ProductionWhiteStripPreCooling;
import com.ruoyi.web.base.domain.ProductionWhiteStripRefined;
import com.ruoyi.web.base.domain.param.WiteStripRequest;
import com.ruoyi.web.base.service.IProductionRecordService;
import com.ruoyi.web.base.service.IProductionWhiteStripPreCoolingService;
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-pre-cooling")
public class ProductionWhiteStripPreCoolingController {

    @Autowired
    private IProductionWhiteStripPreCoolingService productionWhiteStripPreCoolingService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IProductionRecordService productionRecordService;


    @ApiOperation("白条预冷添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionWhiteStripPreCooling preCooling, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionWhiteStripPreCoolingService.count(
                new QueryWrapper<ProductionWhiteStripPreCooling>()
                        .eq("org_id", loginOrgId)
                        .eq("production_record_number", preCooling.getProductionRecordNumber())
        ) > 0) {
            throw new Exception("该预冷编号已存在");
        }
        preCooling.setOrgId(loginOrgId);
        preCooling.setCreateBy(getUsername());
        ProductionRecord productionRecord = new ProductionRecord();
        BeanUtil.copyProperties(preCooling,productionRecord);
        productionRecordService.save(productionRecord);
        return success(productionWhiteStripPreCoolingService.save(preCooling));
    }

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

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

    @ApiOperation("白条预冷列表")
    @PostMapping("/list")
    public AjaxResult listAll(   @RequestBody WiteStripRequest planRequest,
                                 HttpServletRequest request) {
        QueryWrapper<ProductionWhiteStripPreCooling> wrapper = new QueryWrapper<ProductionWhiteStripPreCooling>()
                .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(productionWhiteStripPreCoolingService.list(
                wrapper));
    }

    @ApiOperation("白条预冷分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody WiteStripRequest planRequest,
            HttpServletRequest request
    ) {
        QueryWrapper<ProductionWhiteStripPreCooling> wrapper = new QueryWrapper<ProductionWhiteStripPreCooling>()
                .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(productionWhiteStripPreCoolingService.page(
                new Page<ProductionWhiteStripPreCooling>(planRequest.getPageNum(), planRequest.getPageSize()),
                wrapper
        ));
    }

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